Merge pull request #267 from nofficalfs/PR-Experimental-coroutine

[Experimental] genCoroutine
This commit is contained in:
Spmario233 2023-09-05 08:42:24 +08:00 committed by GitHub
commit c9c61c0454
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 48 additions and 33 deletions

View File

@ -29,34 +29,42 @@
} }
} }
} }
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;
}catch(error){ }catch(error){
reject(error); reject(error);
return; return;
} }
if(info.done){ if(info.done){
resolve(value); resolve(value);
}else{ }else{
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; function _next(value){
return new Promise(function(resolve, reject){ _genNext(gen,resolve,reject,_next,_throw,"next",value);
var gen=fn.apply(self,args); }
function _next(value){ function _throw(err){
asyncGeneratorStep(gen,resolve,reject,_next,_throw,"next",value); _genNext(gen,resolve,reject,_next,_throw,"throw",err);
} }
function _throw(err){ _next(undefined);
asyncGeneratorStep(gen,resolve,reject,_next,_throw,"throw",err); })
} }
_next(undefined); function genAsync(fn){
}); return function genCoroutine(){
}; 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);
});
});
};
} }
const GeneratorFunction=(function*(){}).constructor; const GeneratorFunction=(function*(){}).constructor;
const _status={ const _status={
@ -142,6 +150,7 @@
card:{}, card:{},
}, },
onload:[], onload:[],
onload2:[],
arenaReady:[], arenaReady:[],
onfree:[], onfree:[],
inpile:[], inpile:[],
@ -7144,6 +7153,8 @@
'无名杀 - 录像 - '+_status.videoToSave.name[0]+' - '+_status.videoToSave.name[1]); '无名杀 - 录像 - '+_status.videoToSave.name[0]+' - '+_status.videoToSave.name[1]);
} }
}, },
genAsync:fn=>genAsync(fn),
genAwait:gen=>genAwait(gen),
init:{ init:{
init:function(){ init:function(){
if(typeof __dirname==='string'&&__dirname.length){ if(typeof __dirname==='string'&&__dirname.length){
@ -8842,16 +8853,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;
@ -9610,6 +9618,13 @@
} }
localStorage.removeItem(lib.configprefix+'directstart'); localStorage.removeItem(lib.configprefix+'directstart');
delete lib.init.init; 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(){ startOnline:function(){
'step 0' 'step 0'