From 666fac9ad0e0521ecf32e817d7c018333858f892 Mon Sep 17 00:00:00 2001 From: Rintim Date: Tue, 5 Sep 2023 21:58:39 +0800 Subject: [PATCH 1/6] purity genCoroutine && add lib.onload3 and onload4. --- game/game.js | 133 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 89 insertions(+), 44 deletions(-) diff --git a/game/game.js b/game/game.js index 82a0bbbea..aa8320ff8 100644 --- a/game/game.js +++ b/game/game.js @@ -29,44 +29,37 @@ } } } - function _genNext(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 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 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; + // gnc: GeNCoroutine + const gnc={ + async:(fn)=>function genCoroutine(){ + return gnc.await(fn.apply(this,arguments)) + }, + 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); + }), + is:{ + coroutine:(item)=>typeof item=="function"&&item.name=="genCoroutine", + generator:(item)=>item instanceof 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); + } + } + }; const _status={ paused:false, paused2:false, @@ -151,6 +144,8 @@ }, onload:[], onload2:[], + onload3:[], + onload4:[], arenaReady:[], onfree:[], inpile:[], @@ -7161,8 +7156,8 @@ '无名杀 - 录像 - '+_status.videoToSave.name[0]+' - '+_status.videoToSave.name[1]); } }, - genAsync:fn=>genAsync(fn), - genAwait:gen=>genAwait(gen), + genAsync:fn=>gnc.async(fn), + genAwait:gen=>gnc.await(gen), init:{ init:function(){ if(typeof __dirname==='string'&&__dirname.length){ @@ -7657,6 +7652,40 @@ } }); } + if (!("allSettled" in Promise)){ + Object.defineProperty(Promise, "allSettled", { + configurable:true, + enumerable:false, + writable:true, + value:function allSettled(ary){ + const Promise = this; + return new Promise((resolve, reject) => { + if (Object.prototype.toString.call(arr) != "[object Array]") + return reject(new TypeError(`${typeof arr} ${ary} is not iterable(cannot read property Symbol(Symbol.iterator))`)); + let args = Array.prototype.slice.call(ary); + if (args.length == 0) return resolve([]); + let arrCount = args.length; + function resolvePromise(index, value) { + if (typeof value == "object") { + var then = value.then; + if (typeof then == "function") { + then.call(value, (val) => { + args[index] = { status: "fulfilled", value: val }; + if (--arrCount == 0) resolve(args); + }, (e) => { + args[index] = { status: "rejected", reason: e }; + if (--arrCount == 0) resolve(args); + }); + } + } + } + + for (let i = 0; i < args.length; ++i) + resolvePromise(i, args[i]); + }); + } + }); + } if(!Object.values){ Object.defineProperty(Object, 'values', { configurable:true, @@ -8861,14 +8890,22 @@ } }, //lib.onload支持传入GeneratorFunction以解决异步函数的问题 by诗笺 - onload:genAsync(function*(){ - const libOnload=lib.onload; + onload:gnc.async(function*(){ + const libOnload=lib.onload,libOnload3=lib.onload3; delete lib.onload; + delete lib.onload3; + let onload3=[]; + while(Array.isArray(libOnload3)&&libOnload3.length){ + const fun=libOnload3.shift(); + const result=fun(); + onload3.add(gnc.is.generator(fun)?gnc.await(result):result); + } while(Array.isArray(libOnload)&&libOnload.length){ const fun=libOnload.shift(); const result=fun(); - yield (fun instanceof GeneratorFunction)?genAwait(result):result; + yield gnc.is.generator(fun)?gnc.await(result):result; } + yield Promise.allSettled(onload3); ui.updated(); game.documentZoom=game.deviceZoom; if(game.documentZoom!=1){ @@ -9626,13 +9663,21 @@ } localStorage.removeItem(lib.configprefix+'directstart'); delete lib.init.init; - const libOnload2=lib.onload2; + const libOnload2=lib.onload2,libOnload4=lib.onload4; delete lib.onload2; + delete lib.onload4; + let onload4=[]; + while(Array.isArray(libOnload4)&&libOnload4.length){ + const fun=libOnload4.shift(); + const result=fun(); + onload4.add(gnc.is.generator(fun)?gnc.await(result):result); + } while(Array.isArray(libOnload2)&&libOnload2.length){ const fun=libOnload2.shift(); const result=fun(); - yield (fun instanceof GeneratorFunction)?genAwait(result):result; + yield gnc.is.generator(fun)?gnc.await(result):result; } + yield Promise.allSettled(onload4); }), startOnline:function(){ 'step 0' From 67b1b45a835d04e1559e10cded612335b49c6a4f Mon Sep 17 00:00:00 2001 From: Rintim Date: Tue, 5 Sep 2023 22:40:16 +0800 Subject: [PATCH 2/6] fix bugs && modify lib.onload3,4 to lib.onprepare --- game/game.js | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/game/game.js b/game/game.js index aa8320ff8..4406ae690 100644 --- a/game/game.js +++ b/game/game.js @@ -144,8 +144,7 @@ }, onload:[], onload2:[], - onload3:[], - onload4:[], + onprepare:[], arenaReady:[], onfree:[], inpile:[], @@ -8137,6 +8136,12 @@ } } const loadPack=()=>{ + if (Array.isArray(lib.onprepare)&&lib.onprepare.length){ + _status.onprepare=Object.freeze(lib.onprepare.map(fn=>{ + const result=fn(); + return gnc.is.generator(fn)?gnc.await(result):result; + })); + } let toLoad=lib.config.all.cards.length+lib.config.all.characters.length+1; if(_status.jsExt) toLoad+=_status.jsExt.reduce((previousValue,currentValue)=>{ const arrayLengths=Object.values(currentValue).reduce((previousElement,currentElement)=>{ @@ -8336,7 +8341,7 @@ document.addEventListener('touchmove',ui.click.windowtouchmove); } }; - var proceed2=function(){ + var proceed2=()=>{ if(config3){ proceed(config3); } @@ -8891,21 +8896,13 @@ }, //lib.onload支持传入GeneratorFunction以解决异步函数的问题 by诗笺 onload:gnc.async(function*(){ - const libOnload=lib.onload,libOnload3=lib.onload3; + const libOnload=lib.onload; delete lib.onload; - delete lib.onload3; - let onload3=[]; - while(Array.isArray(libOnload3)&&libOnload3.length){ - const fun=libOnload3.shift(); - const result=fun(); - onload3.add(gnc.is.generator(fun)?gnc.await(result):result); - } while(Array.isArray(libOnload)&&libOnload.length){ const fun=libOnload.shift(); const result=fun(); yield gnc.is.generator(fun)?gnc.await(result):result; } - yield Promise.allSettled(onload3); ui.updated(); game.documentZoom=game.deviceZoom; if(game.documentZoom!=1){ @@ -9663,21 +9660,17 @@ } localStorage.removeItem(lib.configprefix+'directstart'); delete lib.init.init; - const libOnload2=lib.onload2,libOnload4=lib.onload4; + const libOnload2=lib.onload2; delete lib.onload2; - delete lib.onload4; - let onload4=[]; - while(Array.isArray(libOnload4)&&libOnload4.length){ - const fun=libOnload4.shift(); - const result=fun(); - onload4.add(gnc.is.generator(fun)?gnc.await(result):result); - } while(Array.isArray(libOnload2)&&libOnload2.length){ const fun=libOnload2.shift(); const result=fun(); yield gnc.is.generator(fun)?gnc.await(result):result; } - yield Promise.allSettled(onload4); + if(Array.isArray(_status.onprepare)&&_status.onprepare.length){ + Promise.allSettled(_status.onprepare); + delete _status.onprepare; + } }), startOnline:function(){ 'step 0' From 92504b1adf2a97b5e8d1d7cda9532e7e830116e3 Mon Sep 17 00:00:00 2001 From: Rintim Date: Wed, 6 Sep 2023 00:48:14 +0800 Subject: [PATCH 3/6] push onprepare later && add methods to determine genCoroutine. --- game/game.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/game/game.js b/game/game.js index 4406ae690..59ad07a93 100644 --- a/game/game.js +++ b/game/game.js @@ -7157,6 +7157,8 @@ }, genAsync:fn=>gnc.async(fn), genAwait:gen=>gnc.await(gen), + isGenCoroutine:(item)=>typeof item=="function"&&item.name=="genCoroutine", + isGenerator:(item)=>item instanceof GeneratorFunction, init:{ init:function(){ if(typeof __dirname==='string'&&__dirname.length){ @@ -9096,7 +9098,7 @@ }); } - var proceed2=function(){ + var proceed2=gnc.async(function*(){ var mode=lib.imported.mode; var card=lib.imported.card; var character=lib.imported.character; @@ -9548,8 +9550,12 @@ } } delete lib.init.start; + if(Array.isArray(_status.onprepare)&&_status.onprepare.length){ + yield Promise.allSettled(_status.onprepare); + delete _status.onprepare; + } game.loop(); - } + }) var proceed=function(){ if(!lib.db){ try{ @@ -9667,10 +9673,6 @@ const result=fun(); yield gnc.is.generator(fun)?gnc.await(result):result; } - if(Array.isArray(_status.onprepare)&&_status.onprepare.length){ - Promise.allSettled(_status.onprepare); - delete _status.onprepare; - } }), startOnline:function(){ 'step 0' From f99ec24852cb9b47f90d93cc5b5eada351aac16c Mon Sep 17 00:00:00 2001 From: Rintim Date: Wed, 6 Sep 2023 00:53:11 +0800 Subject: [PATCH 4/6] modify names. --- game/game.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/game/game.js b/game/game.js index 59ad07a93..9a6df79fa 100644 --- a/game/game.js +++ b/game/game.js @@ -32,17 +32,18 @@ const GeneratorFunction=(function*(){}).constructor; // gnc: GeNCoroutine const gnc={ - async:(fn)=>function genCoroutine(){ + async:fn=>function genCoroutine(){ return gnc.await(fn.apply(this,arguments)) }, - await:(gen)=>new Promise((resolve,reject)=>{ + 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); }), is:{ - coroutine:(item)=>typeof item=="function"&&item.name=="genCoroutine", - generator:(item)=>item instanceof GeneratorFunction + coroutine:item=>typeof item=="function"&&item.name=="genCoroutine", + generator:item=>item instanceof GeneratorFunction, + a:item=>item.constructor==GeneratorFunction }, next:(gen,resolve,reject,key,arg,_next,_throw)=>{ let info,value; @@ -7157,8 +7158,9 @@ }, genAsync:fn=>gnc.async(fn), genAwait:gen=>gnc.await(gen), - isGenCoroutine:(item)=>typeof item=="function"&&item.name=="genCoroutine", - isGenerator:(item)=>item instanceof GeneratorFunction, + isGenCoroutine:item=>gnc.is.coroutine(item), + isGeneratorFunc:item=>gnc.is.generator(item), + isGenerator:item=>gnc.is.a(item), init:{ init:function(){ if(typeof __dirname==='string'&&__dirname.length){ From c17a345781a08e6b2cf5b511c8f1881c6188f169 Mon Sep 17 00:00:00 2001 From: Rintim Date: Wed, 6 Sep 2023 01:09:20 +0800 Subject: [PATCH 5/6] remove gnc.is.* in lib. --- game/game.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/game/game.js b/game/game.js index 9a6df79fa..80925250a 100644 --- a/game/game.js +++ b/game/game.js @@ -7158,9 +7158,6 @@ }, genAsync:fn=>gnc.async(fn), genAwait:gen=>gnc.await(gen), - isGenCoroutine:item=>gnc.is.coroutine(item), - isGeneratorFunc:item=>gnc.is.generator(item), - isGenerator:item=>gnc.is.a(item), init:{ init:function(){ if(typeof __dirname==='string'&&__dirname.length){ From 87915084c7eb515598990cf5cb7af52f2b5fd075 Mon Sep 17 00:00:00 2001 From: Rintim Date: Wed, 6 Sep 2023 16:47:47 +0800 Subject: [PATCH 6/6] lib.gnc --- game/game.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/game/game.js b/game/game.js index 80925250a..c79096a7e 100644 --- a/game/game.js +++ b/game/game.js @@ -42,8 +42,8 @@ }), is:{ coroutine:item=>typeof item=="function"&&item.name=="genCoroutine", - generator:item=>item instanceof GeneratorFunction, - a:item=>item.constructor==GeneratorFunction + generatorFunc:item=>item instanceof GeneratorFunction, + generator:item=>item.constructor==GeneratorFunction }, next:(gen,resolve,reject,key,arg,_next,_throw)=>{ let info,value; @@ -7158,6 +7158,15 @@ }, genAsync:fn=>gnc.async(fn), genAwait:gen=>gnc.await(gen), + gnc:{ + async:gnc.async(fn), + await:gnc.await(gen), + is:{ + coroutine:item=>gnc.is.coroutine(item), + generatorFunc:item=>gnc.is.generatorFunc(item), + generator:item=>gnc.is.generator(item) + } + }, init:{ init:function(){ if(typeof __dirname==='string'&&__dirname.length){ @@ -8140,7 +8149,7 @@ if (Array.isArray(lib.onprepare)&&lib.onprepare.length){ _status.onprepare=Object.freeze(lib.onprepare.map(fn=>{ const result=fn(); - return gnc.is.generator(fn)?gnc.await(result):result; + return gnc.is.generatorFunc(fn)?gnc.await(result):result; })); } let toLoad=lib.config.all.cards.length+lib.config.all.characters.length+1; @@ -8902,7 +8911,7 @@ while(Array.isArray(libOnload)&&libOnload.length){ const fun=libOnload.shift(); const result=fun(); - yield gnc.is.generator(fun)?gnc.await(result):result; + yield gnc.is.generatorFunc(fun)?gnc.await(result):result; } ui.updated(); game.documentZoom=game.deviceZoom; @@ -9670,7 +9679,7 @@ while(Array.isArray(libOnload2)&&libOnload2.length){ const fun=libOnload2.shift(); const result=fun(); - yield gnc.is.generator(fun)?gnc.await(result):result; + yield gnc.is.generatorFunc(fun)?gnc.await(result):result; } }), startOnline:function(){