From 4594fb82cb09a4bf08a4fd6c353ce6733860bc98 Mon Sep 17 00:00:00 2001 From: Rintim Date: Sun, 10 Sep 2023 15:58:51 +0800 Subject: [PATCH 1/5] purify lib.gnc --- game/game.js | 53 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/game/game.js b/game/game.js index c50d18f47..f32bfd68c 100644 --- a/game/game.js +++ b/game/game.js @@ -33,32 +33,43 @@ // gnc: GeNCoroutine const gnc={ async:fn=>function genCoroutine(){ - return gnc.await(fn.apply(this,arguments)) + let result=fn.apply(this,arguments); + result.name="genCoroutine"; + result.status="next"; + result.state=undefined; + return gnc.await(result); }, await:gen=>new Promise((resolve,reject)=>{ - const _next=value=>gnc.next(gen,resolve,reject,"next",value,_next,_throw); - const _throw=err=>gnc.next(gen,resolve,reject,"throw",err,_next,_throw); - _next(undefined); + let result=gen; + let nexts=resolve; + let throws=reject; + if(gnc.is.coroutine(gen)) { + try{ + result=gen[result.status](result.state); + }catch(error){ + reject(error); + return; + } + if(!result.done){ + nexts=(item)=>{ + gen.state=item; + gen.status="next"; + gnc.await(gen).then(resolve,reject); + } + throws=(err)=>{ + gen.state=err; + gen.status="throw"; + gnc.await(gen).then(resolve,reject); + } + } + result=result.value; + } + Promise.resolve(result).then(nexts,throws); }), is:{ - coroutine:item=>typeof item=="function"&&item.name=="genCoroutine", + coroutine:item=>(typeof item=="function"||gnc.is.generator(item))&&item.name=="genCoroutine", generatorFunc:item=>item instanceof GeneratorFunction, - generator:item=>item.constructor==GeneratorFunction - }, - next:(gen,resolve,reject,key,arg,_next,_throw)=>{ - let info,value; - try{ - info=gen[key](arg); - value=info.value; - }catch(error){ - reject(error); - return; - } - if(info.done){ - resolve(value); - }else{ - Promise.resolve(value).then(_next,_throw); - } + generator:item=>item.constructor.constructor==GeneratorFunction } }; const _status={ From 828b7c0ab2ac7f25c83caa322b91c38ee3f03c99 Mon Sep 17 00:00:00 2001 From: Rintim Date: Sun, 10 Sep 2023 16:04:59 +0800 Subject: [PATCH 2/5] fix some bugs. --- game/game.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/game/game.js b/game/game.js index f32bfd68c..07a817b83 100644 --- a/game/game.js +++ b/game/game.js @@ -69,7 +69,7 @@ is:{ coroutine:item=>(typeof item=="function"||gnc.is.generator(item))&&item.name=="genCoroutine", generatorFunc:item=>item instanceof GeneratorFunction, - generator:item=>item.constructor.constructor==GeneratorFunction + generator:item=>(typeof item=="object")&&("constructor" in item)&&item.constructor&&("constructor" in item.constructor)&&item.constructor.constructor===GeneratorFunction } }; const _status={ @@ -8304,8 +8304,8 @@ const loadPack=()=>{ if (Array.isArray(lib.onprepare)&&lib.onprepare.length){ _status.onprepare=Object.freeze(lib.onprepare.map(fn=>{ - const result=fn(); - return gnc.is.generatorFunc(fn)?gnc.await(result):result; + if(typeof fn!="function") return; + return gnc.await(fn()); })); } let toLoad=lib.config.all.cards.length+lib.config.all.characters.length+1; @@ -9096,8 +9096,8 @@ delete lib.onload; while(Array.isArray(libOnload)&&libOnload.length){ const fun=libOnload.shift(); - const result=fun(); - yield gnc.is.generatorFunc(fun)?gnc.await(result):result; + if(typeof fun!="function") continue; + yield gnc.await(fun()); } ui.updated(); game.documentZoom=game.deviceZoom; @@ -9870,8 +9870,8 @@ delete lib.onload2; while(Array.isArray(libOnload2)&&libOnload2.length){ const fun=libOnload2.shift(); - const result=fun(); - yield gnc.is.generatorFunc(fun)?gnc.await(result):result; + if(typeof fun!="function") continue; + yield gnc.await(fun()); } }), startOnline:function(){ From 0f00fc833213b36bd99b0adcbb479811f63ec9c6 Mon Sep 17 00:00:00 2001 From: Rintim Date: Sun, 10 Sep 2023 16:44:12 +0800 Subject: [PATCH 3/5] fix bugs. --- game/game.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game/game.js b/game/game.js index 07a817b83..c675dcf0d 100644 --- a/game/game.js +++ b/game/game.js @@ -43,7 +43,7 @@ let result=gen; let nexts=resolve; let throws=reject; - if(gnc.is.coroutine(gen)) { + if(gnc.is.coroutine(gen)||(gnc.is.generator(gen)&&!gnc.nocoroutine)) { try{ result=gen[result.status](result.state); }catch(error){ From 3b0edf0d2766c0a71e26e7565937a36593d1d038 Mon Sep 17 00:00:00 2001 From: Rintim Date: Sun, 10 Sep 2023 16:48:02 +0800 Subject: [PATCH 4/5] add default value. --- game/game.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/game/game.js b/game/game.js index c675dcf0d..8244ecbaa 100644 --- a/game/game.js +++ b/game/game.js @@ -44,6 +44,8 @@ let nexts=resolve; let throws=reject; if(gnc.is.coroutine(gen)||(gnc.is.generator(gen)&&!gnc.nocoroutine)) { + if(!gen.status)gen.status="next"; + if(!gen.state)gen.state=undefined; try{ result=gen[result.status](result.state); }catch(error){ From 37ec12c86630998cf4fb691125eddc4d71c629f7 Mon Sep 17 00:00:00 2001 From: Rintim Date: Sun, 10 Sep 2023 16:50:28 +0800 Subject: [PATCH 5/5] add `lib.gnc.escape` --- game/game.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/game/game.js b/game/game.js index 8244ecbaa..51904db36 100644 --- a/game/game.js +++ b/game/game.js @@ -43,7 +43,7 @@ let result=gen; let nexts=resolve; let throws=reject; - if(gnc.is.coroutine(gen)||(gnc.is.generator(gen)&&!gnc.nocoroutine)) { + if(gnc.is.coroutine(gen)||(gnc.is.generator(gen)&&!gen.nocoroutine)) { if(!gen.status)gen.status="next"; if(!gen.state)gen.state=undefined; try{ @@ -68,6 +68,10 @@ } Promise.resolve(result).then(nexts,throws); }), + escape:gen=>{ + gen.nocoroutine=true; + return gen; + }, is:{ coroutine:item=>(typeof item=="function"||gnc.is.generator(item))&&item.name=="genCoroutine", generatorFunc:item=>item instanceof GeneratorFunction, @@ -7269,6 +7273,7 @@ gnc:{ async:fn=>gnc.async(fn), await:gen=>gnc.await(gen), + escape:gen=>gnc.escape(gen), is:{ coroutine:item=>gnc.is.coroutine(item), generatorFunc:item=>gnc.is.generatorFunc(item),