Merge pull request #310 from nofficalfs/PR-Purify-LibGnc
[Purify] Modify `lib.gnc`
This commit is contained in:
commit
b653bb4b19
74
game/game.js
74
game/game.js
|
@ -33,32 +33,49 @@
|
||||||
// gnc: GeNCoroutine
|
// gnc: GeNCoroutine
|
||||||
const gnc={
|
const gnc={
|
||||||
async:fn=>function genCoroutine(){
|
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)=>{
|
await:gen=>new Promise((resolve,reject)=>{
|
||||||
const _next=value=>gnc.next(gen,resolve,reject,"next",value,_next,_throw);
|
let result=gen;
|
||||||
const _throw=err=>gnc.next(gen,resolve,reject,"throw",err,_next,_throw);
|
let nexts=resolve;
|
||||||
_next(undefined);
|
let throws=reject;
|
||||||
|
if(gnc.is.coroutine(gen)||(gnc.is.generator(gen)&&!gen.nocoroutine)) {
|
||||||
|
if(!gen.status)gen.status="next";
|
||||||
|
if(!gen.state)gen.state=undefined;
|
||||||
|
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:{
|
escape:gen=>{
|
||||||
coroutine:item=>typeof item=="function"&&item.name=="genCoroutine",
|
gen.nocoroutine=true;
|
||||||
generatorFunc:item=>item instanceof GeneratorFunction,
|
return gen;
|
||||||
generator:item=>item.constructor==GeneratorFunction
|
|
||||||
},
|
},
|
||||||
next:(gen,resolve,reject,key,arg,_next,_throw)=>{
|
is:{
|
||||||
let info,value;
|
coroutine:item=>(typeof item=="function"||gnc.is.generator(item))&&item.name=="genCoroutine",
|
||||||
try{
|
generatorFunc:item=>item instanceof GeneratorFunction,
|
||||||
info=gen[key](arg);
|
generator:item=>(typeof item=="object")&&("constructor" in item)&&item.constructor&&("constructor" in item.constructor)&&item.constructor.constructor===GeneratorFunction
|
||||||
value=info.value;
|
|
||||||
}catch(error){
|
|
||||||
reject(error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(info.done){
|
|
||||||
resolve(value);
|
|
||||||
}else{
|
|
||||||
Promise.resolve(value).then(_next,_throw);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const _status={
|
const _status={
|
||||||
|
@ -7268,6 +7285,7 @@
|
||||||
gnc:{
|
gnc:{
|
||||||
async:fn=>gnc.async(fn),
|
async:fn=>gnc.async(fn),
|
||||||
await:gen=>gnc.await(gen),
|
await:gen=>gnc.await(gen),
|
||||||
|
escape:gen=>gnc.escape(gen),
|
||||||
is:{
|
is:{
|
||||||
coroutine:item=>gnc.is.coroutine(item),
|
coroutine:item=>gnc.is.coroutine(item),
|
||||||
generatorFunc:item=>gnc.is.generatorFunc(item),
|
generatorFunc:item=>gnc.is.generatorFunc(item),
|
||||||
|
@ -8305,8 +8323,8 @@
|
||||||
const loadPack=()=>{
|
const loadPack=()=>{
|
||||||
if (Array.isArray(lib.onprepare)&&lib.onprepare.length){
|
if (Array.isArray(lib.onprepare)&&lib.onprepare.length){
|
||||||
_status.onprepare=Object.freeze(lib.onprepare.map(fn=>{
|
_status.onprepare=Object.freeze(lib.onprepare.map(fn=>{
|
||||||
const result=fn();
|
if(typeof fn!="function") return;
|
||||||
return gnc.is.generatorFunc(fn)?gnc.await(result):result;
|
return gnc.await(fn());
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
let toLoad=lib.config.all.cards.length+lib.config.all.characters.length+1;
|
let toLoad=lib.config.all.cards.length+lib.config.all.characters.length+1;
|
||||||
|
@ -9097,8 +9115,8 @@
|
||||||
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();
|
||||||
const result=fun();
|
if(typeof fun!="function") continue;
|
||||||
yield gnc.is.generatorFunc(fun)?gnc.await(result):result;
|
yield gnc.await(fun());
|
||||||
}
|
}
|
||||||
ui.updated();
|
ui.updated();
|
||||||
game.documentZoom=game.deviceZoom;
|
game.documentZoom=game.deviceZoom;
|
||||||
|
@ -9871,8 +9889,8 @@
|
||||||
delete lib.onload2;
|
delete lib.onload2;
|
||||||
while(Array.isArray(libOnload2)&&libOnload2.length){
|
while(Array.isArray(libOnload2)&&libOnload2.length){
|
||||||
const fun=libOnload2.shift();
|
const fun=libOnload2.shift();
|
||||||
const result=fun();
|
if(typeof fun!="function") continue;
|
||||||
yield gnc.is.generatorFunc(fun)?gnc.await(result):result;
|
yield gnc.await(fun());
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
startOnline:function(){
|
startOnline:function(){
|
||||||
|
|
Loading…
Reference in New Issue