From 9807f5f714fe2060cbed31cb55c68cdea82dc0fe Mon Sep 17 00:00:00 2001 From: Rintim Date: Mon, 4 Sep 2023 13:12:09 +0800 Subject: [PATCH 1/4] unite indentation. --- game/game.js | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/game/game.js b/game/game.js index 3ada66ad7..5919f400c 100644 --- a/game/game.js +++ b/game/game.js @@ -30,33 +30,33 @@ } } function asyncGeneratorStep(gen,resolve,reject,_next,_throw,key,arg){ - try{ - var info=gen[key](arg); - var value=info.value; - }catch(error){ - reject(error); - return; - } - if(info.done){ - resolve(value); - }else{ - Promise.resolve(value).then(_next,_throw); - } + try{ + var info=gen[key](arg); + var value=info.value; + }catch(error){ + reject(error); + return; + } + if(info.done){ + resolve(value); + }else{ + Promise.resolve(value).then(_next,_throw); + } } function _asyncToGenerator(fn){ - return function(){ - var self=this,args=arguments; - return new Promise(function(resolve, reject){ - var gen=fn.apply(self,args); - function _next(value){ - asyncGeneratorStep(gen,resolve,reject,_next,_throw,"next",value); - } - function _throw(err){ - asyncGeneratorStep(gen,resolve,reject,_next,_throw,"throw",err); - } - _next(undefined); - }); - }; + return function(){ + var self=this,args=arguments; + return new Promise(function(resolve, reject){ + var gen=fn.apply(self,args); + function _next(value){ + asyncGeneratorStep(gen,resolve,reject,_next,_throw,"next",value); + } + function _throw(err){ + asyncGeneratorStep(gen,resolve,reject,_next,_throw,"throw",err); + } + _next(undefined); + }); + }; } const GeneratorFunction=(function*(){}).constructor; const _status={ From bf746cf07dc739c16a593c911e5ab7802183d525 Mon Sep 17 00:00:00 2001 From: Rintim Date: Mon, 4 Sep 2023 18:54:41 +0800 Subject: [PATCH 2/4] maybe determine genCoroutine. --- game/game.js | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/game/game.js b/game/game.js index c466774a0..63798be02 100644 --- a/game/game.js +++ b/game/game.js @@ -29,7 +29,7 @@ } } } - function asyncGeneratorStep(gen,resolve,reject,_next,_throw,key,arg){ + function _genNext(gen,resolve,reject,_next,_throw,key,arg){ try{ var info=gen[key](arg); var value=info.value; @@ -43,18 +43,26 @@ Promise.resolve(value).then(_next,_throw); } } - function _asyncToGenerator(fn){ - return function(){ + function genAwait(gen){ + return new Promise((resolve,reject)=>{ + function _next(value){ + _genNext(gen,resolve,reject,_next,_throw,"next",value); + } + function _throw(err){ + _genNext(gen,resolve,reject,_next,_throw,"throw",err); + } + _next(undefined); + }) + } + function genAsync(fn){ + return function AsyncSimulator(){ var self=this,args=arguments; - return new Promise(function(resolve, reject){ + return new Promise((resolve,reject)=>{ var gen=fn.apply(self,args); - function _next(value){ - asyncGeneratorStep(gen,resolve,reject,_next,_throw,"next",value); - } - function _throw(err){ - asyncGeneratorStep(gen,resolve,reject,_next,_throw,"throw",err); - } - _next(undefined); + genAwait(gen).then((result, err)=>{ + if(err) reject(err); + else resolve(result); + }); }); }; } @@ -8842,16 +8850,13 @@ } }, //lib.onload支持传入GeneratorFunction以解决异步函数的问题 by诗笺 - onload:_asyncToGenerator(function*(){ + onload:genAsync(function*(){ const libOnload=lib.onload; delete lib.onload; while(Array.isArray(libOnload)&&libOnload.length){ const fun=libOnload.shift(); - if(fun instanceof GeneratorFunction){ - yield _asyncToGenerator(fun)(); - }else{ - fun(); - } + const result=fun(); + yield (fun instanceof GeneratorFunction)?genAwait(result):result; } ui.updated(); game.documentZoom=game.deviceZoom; From f6cf169e5334f4cef2c4262c8bea7c9d40047c4c Mon Sep 17 00:00:00 2001 From: Rintim Date: Tue, 5 Sep 2023 00:08:23 +0800 Subject: [PATCH 3/4] determine name && add lib.onload2 --- game/game.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/game/game.js b/game/game.js index 63798be02..32b0edcff 100644 --- a/game/game.js +++ b/game/game.js @@ -55,7 +55,7 @@ }) } function genAsync(fn){ - return function AsyncSimulator(){ + return function genCoroutine(){ var self=this,args=arguments; return new Promise((resolve,reject)=>{ var gen=fn.apply(self,args); @@ -150,6 +150,7 @@ card:{}, }, onload:[], + onload2:[], arenaReady:[], onfree:[], inpile:[], @@ -7152,6 +7153,8 @@ '无名杀 - 录像 - '+_status.videoToSave.name[0]+' - '+_status.videoToSave.name[1]); } }, + genAsync:genAsync, + genAwait:genAwait, init:{ init:function(){ if(typeof __dirname==='string'&&__dirname.length){ @@ -9615,6 +9618,13 @@ } localStorage.removeItem(lib.configprefix+'directstart'); delete lib.init.init; + const libOnload2=lib.onload2; + delete lib.onload2; + while(Array.isArray(libOnload2)&&libOnload2.length){ + const fun=libOnload2.shift(); + const result=fun(); + yield (fun instanceof GeneratorFunction)?genAwait(result):result; + } }), startOnline:function(){ 'step 0' From 0d68c26f86631099525d294ae90d2a576745c115 Mon Sep 17 00:00:00 2001 From: Rintim Date: Tue, 5 Sep 2023 00:09:32 +0800 Subject: [PATCH 4/4] add one transform to make no one can modify origin genCoroutine. --- game/game.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/game/game.js b/game/game.js index 32b0edcff..90762f505 100644 --- a/game/game.js +++ b/game/game.js @@ -7153,8 +7153,8 @@ '无名杀 - 录像 - '+_status.videoToSave.name[0]+' - '+_status.videoToSave.name[1]); } }, - genAsync:genAsync, - genAwait:genAwait, + genAsync:fn=>genAsync(fn), + genAwait:gen=>genAwait(gen), init:{ init:function(){ if(typeof __dirname==='string'&&__dirname.length){