修复lockUtils在wx上报错问题
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@
|
||||
/demo/bin-release
|
||||
/.idea
|
||||
/.vscode
|
||||
/demo_wxgame
|
||||
|
||||
6
demo/libs/framework/framework.d.ts
vendored
6
demo/libs/framework/framework.d.ts
vendored
@@ -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<{}>;
|
||||
}
|
||||
|
||||
@@ -7884,9 +7884,6 @@ 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);
|
||||
};
|
||||
@@ -7894,29 +7891,32 @@ 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();
|
||||
|
||||
2
demo/libs/framework/framework.min.js
vendored
2
demo/libs/framework/framework.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -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);
|
||||
}
|
||||
|
||||
6
source/bin/framework.d.ts
vendored
6
source/bin/framework.d.ts
vendored
@@ -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<{}>;
|
||||
}
|
||||
|
||||
@@ -7884,9 +7884,6 @@ 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);
|
||||
};
|
||||
@@ -7894,29 +7891,32 @@ 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();
|
||||
|
||||
2
source/bin/framework.min.js
vendored
2
source/bin/framework.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -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'));
|
||||
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user