Skip to content

Commit

Permalink
修复lockUtils在wx上报错问题
Browse files Browse the repository at this point in the history
  • Loading branch information
esengine committed Aug 6, 2020
1 parent d0199e3 commit ab3f38c
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/demo/bin-release
/.idea
/.vscode
/demo_wxgame
6 changes: 3 additions & 3 deletions demo/libs/framework/framework.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1644,13 +1644,13 @@ declare module es {
}
}
declare const THREAD_ID: string;
declare const setItem: any;
declare const getItem: any;
declare const removeItem: any;
declare const nextTick: (fn: any) => void;
declare class LockUtils {
private _keyX;
private _keyY;
private setItem;
private getItem;
private removeItem;
constructor(key: any);
lock(): Promise<{}>;
}
Expand Down
20 changes: 10 additions & 10 deletions demo/libs/framework/framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -7884,39 +7884,39 @@ var es;
es.ListPool = ListPool;
})(es || (es = {}));
var THREAD_ID = Math.floor(Math.random() * 1000) + "-" + Date.now();
var setItem = egret.localStorage.setItem.bind(localStorage);
var getItem = egret.localStorage.getItem.bind(localStorage);
var removeItem = egret.localStorage.removeItem.bind(localStorage);
var nextTick = function (fn) {
setTimeout(fn, 0);
};
var LockUtils = (function () {
function LockUtils(key) {
this._keyX = "mutex_key_" + key + "_X";
this._keyY = "mutex_key_" + key + "_Y";
this.setItem = egret.localStorage.setItem.bind(localStorage);
this.getItem = egret.localStorage.getItem.bind(localStorage);
this.removeItem = egret.localStorage.removeItem.bind(localStorage);
}
LockUtils.prototype.lock = function () {
var _this = this;
return new Promise(function (resolve, reject) {
var fn = function () {
setItem(_this._keyX, THREAD_ID);
if (!getItem(_this._keyY) === null) {
_this.setItem(_this._keyX, THREAD_ID);
if (!_this.getItem(_this._keyY) === null) {
nextTick(fn);
}
setItem(_this._keyY, THREAD_ID);
if (getItem(_this._keyX) !== THREAD_ID) {
_this.setItem(_this._keyY, THREAD_ID);
if (_this.getItem(_this._keyX) !== THREAD_ID) {
setTimeout(function () {
if (getItem(_this._keyY) !== THREAD_ID) {
if (_this.getItem(_this._keyY) !== THREAD_ID) {
nextTick(fn);
return;
}
resolve();
removeItem(_this._keyY);
_this.removeItem(_this._keyY);
}, 10);
}
else {
resolve();
removeItem(_this._keyY);
_this.removeItem(_this._keyY);
}
};
fn();
Expand Down
2 changes: 1 addition & 1 deletion demo/libs/framework/framework.min.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions demo/scripts/wxgame/wxgame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export class WxgamePlugin implements plugins.Command {
if (filename == 'main.js') {
content += "\n;window.Main = Main;"
}
if (filename == 'libs/long/long.js' || filename == 'libs/long/long.min.js'){
content += "window.Long = long;"
}
this.md5Obj[path.basename(filename)] = this.md5(content)
file.contents = new Buffer(content);
}
Expand Down
6 changes: 3 additions & 3 deletions source/bin/framework.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1644,13 +1644,13 @@ declare module es {
}
}
declare const THREAD_ID: string;
declare const setItem: any;
declare const getItem: any;
declare const removeItem: any;
declare const nextTick: (fn: any) => void;
declare class LockUtils {
private _keyX;
private _keyY;
private setItem;
private getItem;
private removeItem;
constructor(key: any);
lock(): Promise<{}>;
}
Expand Down
20 changes: 10 additions & 10 deletions source/bin/framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -7884,39 +7884,39 @@ var es;
es.ListPool = ListPool;
})(es || (es = {}));
var THREAD_ID = Math.floor(Math.random() * 1000) + "-" + Date.now();
var setItem = egret.localStorage.setItem.bind(localStorage);
var getItem = egret.localStorage.getItem.bind(localStorage);
var removeItem = egret.localStorage.removeItem.bind(localStorage);
var nextTick = function (fn) {
setTimeout(fn, 0);
};
var LockUtils = (function () {
function LockUtils(key) {
this._keyX = "mutex_key_" + key + "_X";
this._keyY = "mutex_key_" + key + "_Y";
this.setItem = egret.localStorage.setItem.bind(localStorage);
this.getItem = egret.localStorage.getItem.bind(localStorage);
this.removeItem = egret.localStorage.removeItem.bind(localStorage);
}
LockUtils.prototype.lock = function () {
var _this = this;
return new Promise(function (resolve, reject) {
var fn = function () {
setItem(_this._keyX, THREAD_ID);
if (!getItem(_this._keyY) === null) {
_this.setItem(_this._keyX, THREAD_ID);
if (!_this.getItem(_this._keyY) === null) {
nextTick(fn);
}
setItem(_this._keyY, THREAD_ID);
if (getItem(_this._keyX) !== THREAD_ID) {
_this.setItem(_this._keyY, THREAD_ID);
if (_this.getItem(_this._keyX) !== THREAD_ID) {
setTimeout(function () {
if (getItem(_this._keyY) !== THREAD_ID) {
if (_this.getItem(_this._keyY) !== THREAD_ID) {
nextTick(fn);
return;
}
resolve();
removeItem(_this._keyY);
_this.removeItem(_this._keyY);
}, 10);
}
else {
resolve();
removeItem(_this._keyY);
_this.removeItem(_this._keyY);
}
};
fn();
Expand Down
2 changes: 1 addition & 1 deletion source/bin/framework.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions source/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const tsProject = ts.createProject('tsconfig.json');
gulp.task('buildJs', () => {
return tsProject.src()
.pipe(tsProject())
.js.pipe(inject.replace('var framework;', ''))
.pipe(inject.prepend('window.framework = {};\n'))
.js.pipe(inject.replace('var es;', ''))
.pipe(inject.prepend('window.es = {};\n'))
.pipe(inject.replace('var __extends =', 'window.__extends ='))
.pipe(minify({ext: {min: ".min.js"}}))
.pipe(gulp.dest('./bin'));
Expand Down
23 changes: 13 additions & 10 deletions source/src/Utils/LockUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const THREAD_ID = `${Math.floor(Math.random() * 1000)}-${Date.now()}`;
const setItem = egret.localStorage.setItem.bind(localStorage);
const getItem = egret.localStorage.getItem.bind(localStorage);
const removeItem = egret.localStorage.removeItem.bind(localStorage);

const nextTick = fn => {
setTimeout(fn, 0);
Expand All @@ -13,36 +10,42 @@ const nextTick = fn => {
class LockUtils {
private _keyX: string;
private _keyY: string;
private setItem;
private getItem;
private removeItem;

constructor(key) {
this._keyX = `mutex_key_${key}_X`;
this._keyY = `mutex_key_${key}_Y`;
this.setItem = egret.localStorage.setItem.bind(localStorage);
this.getItem = egret.localStorage.getItem.bind(localStorage);
this.removeItem = egret.localStorage.removeItem.bind(localStorage);
}

public lock() {
return new Promise((resolve, reject) => {
const fn = () => {
setItem(this._keyX, THREAD_ID);
if (!getItem(this._keyY) === null) {
this.setItem(this._keyX, THREAD_ID);
if (!this.getItem(this._keyY) === null) {
// restart
nextTick(fn);
}
setItem(this._keyY, THREAD_ID);
if (getItem(this._keyX) !== THREAD_ID) {
this.setItem(this._keyY, THREAD_ID);
if (this.getItem(this._keyX) !== THREAD_ID) {
// delay
setTimeout(() => {
if (getItem(this._keyY) !== THREAD_ID) {
if (this.getItem(this._keyY) !== THREAD_ID) {
// restart
nextTick(fn);
return;
}
// critical section
resolve();
removeItem(this._keyY);
this.removeItem(this._keyY);
}, 10);
} else {
resolve();
removeItem(this._keyY);
this.removeItem(this._keyY);
}
};

Expand Down

0 comments on commit ab3f38c

Please sign in to comment.