maybe determine genCoroutine.

This commit is contained in:
Rintim 2023-09-04 18:54:41 +08:00
parent 653782d7f7
commit bf746cf07d
1 changed files with 22 additions and 17 deletions

View File

@ -29,7 +29,7 @@
} }
} }
} }
function asyncGeneratorStep(gen,resolve,reject,_next,_throw,key,arg){ function _genNext(gen,resolve,reject,_next,_throw,key,arg){
try{ try{
var info=gen[key](arg); var info=gen[key](arg);
var value=info.value; var value=info.value;
@ -43,18 +43,26 @@
Promise.resolve(value).then(_next,_throw); Promise.resolve(value).then(_next,_throw);
} }
} }
function _asyncToGenerator(fn){ function genAwait(gen){
return function(){ return new Promise((resolve,reject)=>{
var self=this,args=arguments;
return new Promise(function(resolve, reject){
var gen=fn.apply(self,args);
function _next(value){ function _next(value){
asyncGeneratorStep(gen,resolve,reject,_next,_throw,"next",value); _genNext(gen,resolve,reject,_next,_throw,"next",value);
} }
function _throw(err){ function _throw(err){
asyncGeneratorStep(gen,resolve,reject,_next,_throw,"throw",err); _genNext(gen,resolve,reject,_next,_throw,"throw",err);
} }
_next(undefined); _next(undefined);
})
}
function genAsync(fn){
return function AsyncSimulator(){
var self=this,args=arguments;
return new Promise((resolve,reject)=>{
var gen=fn.apply(self,args);
genAwait(gen).then((result, err)=>{
if(err) reject(err);
else resolve(result);
});
}); });
}; };
} }
@ -8842,16 +8850,13 @@
} }
}, },
//lib.onload支持传入GeneratorFunction以解决异步函数的问题 by诗笺 //lib.onload支持传入GeneratorFunction以解决异步函数的问题 by诗笺
onload:_asyncToGenerator(function*(){ onload:genAsync(function*(){
const libOnload=lib.onload; const libOnload=lib.onload;
delete lib.onload; delete lib.onload;
while(Array.isArray(libOnload)&&libOnload.length){ while(Array.isArray(libOnload)&&libOnload.length){
const fun=libOnload.shift(); const fun=libOnload.shift();
if(fun instanceof GeneratorFunction){ const result=fun();
yield _asyncToGenerator(fun)(); yield (fun instanceof GeneratorFunction)?genAwait(result):result;
}else{
fun();
}
} }
ui.updated(); ui.updated();
game.documentZoom=game.deviceZoom; game.documentZoom=game.deviceZoom;