purify lib.gnc
This commit is contained in:
parent
78f6a550c4
commit
4594fb82cb
53
game/game.js
53
game/game.js
|
@ -33,32 +33,43 @@
|
||||||
// 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)) {
|
||||||
|
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:{
|
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,
|
generatorFunc:item=>item instanceof GeneratorFunction,
|
||||||
generator:item=>item.constructor==GeneratorFunction
|
generator:item=>item.constructor.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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const _status={
|
const _status={
|
||||||
|
|
Loading…
Reference in New Issue