From bcaf584bfe30229cca8d76e70ffc483e6362b54e Mon Sep 17 00:00:00 2001 From: shijian <2954700422@qq.com> Date: Sun, 3 Sep 2023 02:15:24 +0800 Subject: [PATCH] =?UTF-8?q?lib.onload=E6=94=AF=E6=8C=81=E4=BC=A0=E5=85=A5G?= =?UTF-8?q?eneratorFunction=E4=BB=A5=E8=A7=A3=E5=86=B3=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E5=BC=82=E6=AD=A5=E5=87=BD=E6=95=B0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/game.js | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/game/game.js b/game/game.js index 16ba86692..9d7c6e3f8 100644 --- a/game/game.js +++ b/game/game.js @@ -29,6 +29,36 @@ } } } + 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); + } + } + 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); + }); + }; + } + const GeneratorFunction=(function*(){}).constructor; const _status={ paused:false, paused2:false, @@ -8791,11 +8821,17 @@ } } }, - onload:function(){ + //lib.onload支持传入GeneratorFunction以解决异步函数的问题 by诗笺 + onload:_asyncToGenerator(function*(){ const libOnload=lib.onload; delete lib.onload; - while(libOnload.length){ - libOnload.shift()(); + while(Array.isArray(libOnload)&&libOnload.length){ + const fun=libOnload.shift(); + if(fun instanceof GeneratorFunction){ + yield _asyncToGenerator(fun)(); + }else{ + fun(); + } } ui.updated(); game.documentZoom=game.deviceZoom; @@ -9554,7 +9590,7 @@ } localStorage.removeItem(lib.configprefix+'directstart'); delete lib.init.init; - }, + }), startOnline:function(){ 'step 0' event._resultid=null;