修复lockUtils在wx上报错问题

This commit is contained in:
yhh
2020-08-06 12:01:20 +08:00
parent d0199e357b
commit ab3f38c6a8
10 changed files with 47 additions and 40 deletions
+1
View File
@@ -3,3 +3,4 @@
/demo/bin-release /demo/bin-release
/.idea /.idea
/.vscode /.vscode
/demo_wxgame
+3 -3
View File
@@ -1644,13 +1644,13 @@ declare module es {
} }
} }
declare const THREAD_ID: string; declare const THREAD_ID: string;
declare const setItem: any;
declare const getItem: any;
declare const removeItem: any;
declare const nextTick: (fn: any) => void; declare const nextTick: (fn: any) => void;
declare class LockUtils { declare class LockUtils {
private _keyX; private _keyX;
private _keyY; private _keyY;
private setItem;
private getItem;
private removeItem;
constructor(key: any); constructor(key: any);
lock(): Promise<{}>; lock(): Promise<{}>;
} }
+10 -10
View File
@@ -7884,9 +7884,6 @@ var es;
es.ListPool = ListPool; es.ListPool = ListPool;
})(es || (es = {})); })(es || (es = {}));
var THREAD_ID = Math.floor(Math.random() * 1000) + "-" + Date.now(); 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) { var nextTick = function (fn) {
setTimeout(fn, 0); setTimeout(fn, 0);
}; };
@@ -7894,29 +7891,32 @@ var LockUtils = (function () {
function LockUtils(key) { function LockUtils(key) {
this._keyX = "mutex_key_" + key + "_X"; this._keyX = "mutex_key_" + key + "_X";
this._keyY = "mutex_key_" + key + "_Y"; 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 () { LockUtils.prototype.lock = function () {
var _this = this; var _this = this;
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
var fn = function () { var fn = function () {
setItem(_this._keyX, THREAD_ID); _this.setItem(_this._keyX, THREAD_ID);
if (!getItem(_this._keyY) === null) { if (!_this.getItem(_this._keyY) === null) {
nextTick(fn); nextTick(fn);
} }
setItem(_this._keyY, THREAD_ID); _this.setItem(_this._keyY, THREAD_ID);
if (getItem(_this._keyX) !== THREAD_ID) { if (_this.getItem(_this._keyX) !== THREAD_ID) {
setTimeout(function () { setTimeout(function () {
if (getItem(_this._keyY) !== THREAD_ID) { if (_this.getItem(_this._keyY) !== THREAD_ID) {
nextTick(fn); nextTick(fn);
return; return;
} }
resolve(); resolve();
removeItem(_this._keyY); _this.removeItem(_this._keyY);
}, 10); }, 10);
} }
else { else {
resolve(); resolve();
removeItem(_this._keyY); _this.removeItem(_this._keyY);
} }
}; };
fn(); fn();
File diff suppressed because one or more lines are too long
+3
View File
@@ -44,6 +44,9 @@ export class WxgamePlugin implements plugins.Command {
if (filename == 'main.js') { if (filename == 'main.js') {
content += "\n;window.Main = Main;" 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) this.md5Obj[path.basename(filename)] = this.md5(content)
file.contents = new Buffer(content); file.contents = new Buffer(content);
} }
+3 -3
View File
@@ -1644,13 +1644,13 @@ declare module es {
} }
} }
declare const THREAD_ID: string; declare const THREAD_ID: string;
declare const setItem: any;
declare const getItem: any;
declare const removeItem: any;
declare const nextTick: (fn: any) => void; declare const nextTick: (fn: any) => void;
declare class LockUtils { declare class LockUtils {
private _keyX; private _keyX;
private _keyY; private _keyY;
private setItem;
private getItem;
private removeItem;
constructor(key: any); constructor(key: any);
lock(): Promise<{}>; lock(): Promise<{}>;
} }
+10 -10
View File
@@ -7884,9 +7884,6 @@ var es;
es.ListPool = ListPool; es.ListPool = ListPool;
})(es || (es = {})); })(es || (es = {}));
var THREAD_ID = Math.floor(Math.random() * 1000) + "-" + Date.now(); 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) { var nextTick = function (fn) {
setTimeout(fn, 0); setTimeout(fn, 0);
}; };
@@ -7894,29 +7891,32 @@ var LockUtils = (function () {
function LockUtils(key) { function LockUtils(key) {
this._keyX = "mutex_key_" + key + "_X"; this._keyX = "mutex_key_" + key + "_X";
this._keyY = "mutex_key_" + key + "_Y"; 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 () { LockUtils.prototype.lock = function () {
var _this = this; var _this = this;
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
var fn = function () { var fn = function () {
setItem(_this._keyX, THREAD_ID); _this.setItem(_this._keyX, THREAD_ID);
if (!getItem(_this._keyY) === null) { if (!_this.getItem(_this._keyY) === null) {
nextTick(fn); nextTick(fn);
} }
setItem(_this._keyY, THREAD_ID); _this.setItem(_this._keyY, THREAD_ID);
if (getItem(_this._keyX) !== THREAD_ID) { if (_this.getItem(_this._keyX) !== THREAD_ID) {
setTimeout(function () { setTimeout(function () {
if (getItem(_this._keyY) !== THREAD_ID) { if (_this.getItem(_this._keyY) !== THREAD_ID) {
nextTick(fn); nextTick(fn);
return; return;
} }
resolve(); resolve();
removeItem(_this._keyY); _this.removeItem(_this._keyY);
}, 10); }, 10);
} }
else { else {
resolve(); resolve();
removeItem(_this._keyY); _this.removeItem(_this._keyY);
} }
}; };
fn(); fn();
+1 -1
View File
File diff suppressed because one or more lines are too long
+2 -2
View File
@@ -8,8 +8,8 @@ const tsProject = ts.createProject('tsconfig.json');
gulp.task('buildJs', () => { gulp.task('buildJs', () => {
return tsProject.src() return tsProject.src()
.pipe(tsProject()) .pipe(tsProject())
.js.pipe(inject.replace('var framework;', '')) .js.pipe(inject.replace('var es;', ''))
.pipe(inject.prepend('window.framework = {};\n')) .pipe(inject.prepend('window.es = {};\n'))
.pipe(inject.replace('var __extends =', 'window.__extends =')) .pipe(inject.replace('var __extends =', 'window.__extends ='))
.pipe(minify({ext: {min: ".min.js"}})) .pipe(minify({ext: {min: ".min.js"}}))
.pipe(gulp.dest('./bin')); .pipe(gulp.dest('./bin'));
+13 -10
View File
@@ -1,7 +1,4 @@
const THREAD_ID = `${Math.floor(Math.random() * 1000)}-${Date.now()}`; 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 => { const nextTick = fn => {
setTimeout(fn, 0); setTimeout(fn, 0);
@@ -13,36 +10,42 @@ const nextTick = fn => {
class LockUtils { class LockUtils {
private _keyX: string; private _keyX: string;
private _keyY: string; private _keyY: string;
private setItem;
private getItem;
private removeItem;
constructor(key) { constructor(key) {
this._keyX = `mutex_key_${key}_X`; this._keyX = `mutex_key_${key}_X`;
this._keyY = `mutex_key_${key}_Y`; 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() { public lock() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const fn = () => { const fn = () => {
setItem(this._keyX, THREAD_ID); this.setItem(this._keyX, THREAD_ID);
if (!getItem(this._keyY) === null) { if (!this.getItem(this._keyY) === null) {
// restart // restart
nextTick(fn); nextTick(fn);
} }
setItem(this._keyY, THREAD_ID); this.setItem(this._keyY, THREAD_ID);
if (getItem(this._keyX) !== THREAD_ID) { if (this.getItem(this._keyX) !== THREAD_ID) {
// delay // delay
setTimeout(() => { setTimeout(() => {
if (getItem(this._keyY) !== THREAD_ID) { if (this.getItem(this._keyY) !== THREAD_ID) {
// restart // restart
nextTick(fn); nextTick(fn);
return; return;
} }
// critical section // critical section
resolve(); resolve();
removeItem(this._keyY); this.removeItem(this._keyY);
}, 10); }, 10);
} else { } else {
resolve(); resolve();
removeItem(this._keyY); this.removeItem(this._keyY);
} }
}; };