Merge pull request #340 from nofficalfs/PR-Alter-GenCoroutine
[Alter] 修改`genCoroutine`的机制
This commit is contained in:
commit
b5678f7715
80
game/game.js
80
game/game.js
|
@ -32,22 +32,16 @@
|
||||||
const GeneratorFunction=(function*(){}).constructor;
|
const GeneratorFunction=(function*(){}).constructor;
|
||||||
// gnc: GeNCoroutine
|
// gnc: GeNCoroutine
|
||||||
const gnc={
|
const gnc={
|
||||||
async:fn=>function genCoroutine(){
|
of:fn=>gnc.is.generatorFunc(fn)?function genCoroutine(){
|
||||||
let result=fn.apply(this,arguments);
|
let gen=fn.apply(this,arguments);
|
||||||
result.name="genCoroutine";
|
gen.status="next";
|
||||||
result.status="next";
|
gen.state=undefined;
|
||||||
result.state=undefined;
|
const callback=(resolve,reject)=>{
|
||||||
return gnc.await(result);
|
let result,
|
||||||
},
|
nexts=resolve,
|
||||||
await:gen=>new Promise((resolve,reject)=>{
|
throws=reject;
|
||||||
let result=gen;
|
|
||||||
let nexts=resolve;
|
|
||||||
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{
|
try{
|
||||||
result=gen[result.status](result.state);
|
result=gen[gen.status](gen.state);
|
||||||
}catch(error){
|
}catch(error){
|
||||||
reject(error);
|
reject(error);
|
||||||
return;
|
return;
|
||||||
|
@ -56,24 +50,21 @@
|
||||||
nexts=(item)=>{
|
nexts=(item)=>{
|
||||||
gen.state=item;
|
gen.state=item;
|
||||||
gen.status="next";
|
gen.status="next";
|
||||||
gnc.await(gen).then(resolve,reject);
|
callback(resolve,reject);
|
||||||
}
|
}
|
||||||
throws=(err)=>{
|
throws=(err)=>{
|
||||||
gen.state=err;
|
gen.state=err;
|
||||||
gen.status="throw";
|
gen.status="throw";
|
||||||
gnc.await(gen).then(resolve,reject);
|
callback(resolve,reject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result=result.value;
|
result=result.value;
|
||||||
}
|
|
||||||
Promise.resolve(result).then(nexts,throws);
|
Promise.resolve(result).then(nexts,throws);
|
||||||
}),
|
}
|
||||||
escape:gen=>{
|
return new Promise(callback);
|
||||||
gen.nocoroutine=true;
|
}:(()=>{throw new TypeError("gnc.of needs a GeneratorFunction.")})(),
|
||||||
return gen;
|
|
||||||
},
|
|
||||||
is:{
|
is:{
|
||||||
coroutine:item=>(typeof item=="function"||gnc.is.generator(item))&&item.name=="genCoroutine",
|
coroutine:item=>typeof item=="function"&&item.name=="genCoroutine",
|
||||||
generatorFunc:item=>item instanceof GeneratorFunction,
|
generatorFunc:item=>item instanceof GeneratorFunction,
|
||||||
generator:item=>(typeof item=="object")&&("constructor" in item)&&item.constructor&&("constructor" in item.constructor)&&item.constructor.constructor===GeneratorFunction
|
generator:item=>(typeof item=="object")&&("constructor" in item)&&item.constructor&&("constructor" in item.constructor)&&item.constructor.constructor===GeneratorFunction
|
||||||
}
|
}
|
||||||
|
@ -7279,12 +7270,10 @@
|
||||||
'无名杀 - 录像 - '+_status.videoToSave.name[0]+' - '+_status.videoToSave.name[1]);
|
'无名杀 - 录像 - '+_status.videoToSave.name[0]+' - '+_status.videoToSave.name[1]);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
genAsync:fn=>gnc.async(fn),
|
genAsync:fn=>gnc.of(fn),
|
||||||
genAwait:gen=>gnc.await(gen),
|
genAwait:item=>gnc.is.generator(item)?gnc.of(function*(){for(const content of item){yield content;}})():Promise.resolve(item),
|
||||||
gnc:{
|
gnc:{
|
||||||
async:fn=>gnc.async(fn),
|
of:fn=>gnc.of(fn),
|
||||||
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),
|
||||||
|
@ -8324,7 +8313,7 @@
|
||||||
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=>{
|
||||||
if(typeof fn!="function") return;
|
if(typeof fn!="function") return;
|
||||||
return gnc.await(fn());
|
return (gnc.is.generatorFunc(fn)?gnc.of(fn):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;
|
||||||
|
@ -8336,7 +8325,7 @@
|
||||||
if(!arrayLengths.length) return previousValue+1;
|
if(!arrayLengths.length) return previousValue+1;
|
||||||
return previousValue+Math.min(...arrayLengths);
|
return previousValue+Math.min(...arrayLengths);
|
||||||
},0);
|
},0);
|
||||||
const packLoaded=gnc.async(function*(){
|
const packLoaded=gnc.of(function*(){
|
||||||
toLoad--;
|
toLoad--;
|
||||||
if(toLoad) return;
|
if(toLoad) return;
|
||||||
if(_status.importing){
|
if(_status.importing){
|
||||||
|
@ -8449,13 +8438,13 @@
|
||||||
throw e;
|
throw e;
|
||||||
});
|
});
|
||||||
var styleToLoad=6;
|
var styleToLoad=6;
|
||||||
var styleLoaded=gnc.async(function*(){
|
var styleLoaded=gnc.of(function*(){
|
||||||
--styleToLoad;
|
--styleToLoad;
|
||||||
if(styleToLoad==0){
|
if(styleToLoad==0){
|
||||||
if(extensionlist.length&&(lib.config.mode!='connect'||show_splash)){
|
if(extensionlist.length&&(lib.config.mode!='connect'||show_splash)){
|
||||||
_status.extensionLoading=[];
|
_status.extensionLoading=[];
|
||||||
let extToLoad=extensionlist.length;
|
let extToLoad=extensionlist.length;
|
||||||
const extLoaded=gnc.async(function*(){
|
const extLoaded=gnc.of(function*(){
|
||||||
--extToLoad;
|
--extToLoad;
|
||||||
if(extToLoad==0){
|
if(extToLoad==0){
|
||||||
yield Promise.allSettled(_status.extensionLoading);
|
yield Promise.allSettled(_status.extensionLoading);
|
||||||
|
@ -8477,7 +8466,7 @@
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
lib.init.js(lib.assetURL+'extension/'+extensionlist[i],'extension',extLoaded,(function(i){
|
lib.init.js(lib.assetURL+'extension/'+extensionlist[i],'extension',extLoaded,(function(i){
|
||||||
return gnc.async(function*(){
|
return gnc.of(function*(){
|
||||||
game.removeExtension(i);
|
game.removeExtension(i);
|
||||||
--extToLoad;
|
--extToLoad;
|
||||||
if(extToLoad==0){
|
if(extToLoad==0){
|
||||||
|
@ -9125,13 +9114,13 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
//lib.onload支持传入GeneratorFunction以解决异步函数的问题 by诗笺
|
//lib.onload支持传入GeneratorFunction以解决异步函数的问题 by诗笺
|
||||||
onload:gnc.async(function*(){
|
onload:gnc.of(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(typeof fun!="function") continue;
|
if(typeof fun!="function") continue;
|
||||||
yield gnc.await(fun());
|
yield (gnc.is.generatorFunc(fun)?gnc.of(fun):fun)();
|
||||||
}
|
}
|
||||||
ui.updated();
|
ui.updated();
|
||||||
game.documentZoom=game.deviceZoom;
|
game.documentZoom=game.deviceZoom;
|
||||||
|
@ -9326,7 +9315,7 @@
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var proceed2=gnc.async(function*(){
|
var proceed2=gnc.of(function*(){
|
||||||
var mode=lib.imported.mode;
|
var mode=lib.imported.mode;
|
||||||
var card=lib.imported.card;
|
var card=lib.imported.card;
|
||||||
var character=lib.imported.character;
|
var character=lib.imported.character;
|
||||||
|
@ -9740,7 +9729,8 @@
|
||||||
try{
|
try{
|
||||||
_status.extension=lib.extensions[i][0];
|
_status.extension=lib.extensions[i][0];
|
||||||
_status.evaluatingExtension=lib.extensions[i][3];
|
_status.evaluatingExtension=lib.extensions[i][3];
|
||||||
if (typeof lib.extensions[i][1]=="function") yield gnc.await(lib.extensions[i][1](lib.extensions[i][2],lib.extensions[i][4]));
|
if (typeof lib.extensions[i][1]=="function")
|
||||||
|
yield (gnc.is.coroutine(lib.extensions[i][1])?gnc.of(lib.extensions[i][1]):lib.extensions[i][1])(lib.extensions[i][2],lib.extensions[i][4]);
|
||||||
if(lib.extensions[i][4]){
|
if(lib.extensions[i][4]){
|
||||||
if(lib.extensions[i][4].character){
|
if(lib.extensions[i][4].character){
|
||||||
for(var j in lib.extensions[i][4].character.character){
|
for(var j in lib.extensions[i][4].character.character){
|
||||||
|
@ -9792,7 +9782,7 @@
|
||||||
}
|
}
|
||||||
game.loop();
|
game.loop();
|
||||||
})
|
})
|
||||||
var proceed=gnc.async(function*(){
|
var proceed=gnc.of(function*(){
|
||||||
if(!lib.db){
|
if(!lib.db){
|
||||||
try{
|
try{
|
||||||
lib.storage=JSON.parse(localStorage.getItem(lib.configprefix+lib.config.mode));
|
lib.storage=JSON.parse(localStorage.getItem(lib.configprefix+lib.config.mode));
|
||||||
|
@ -9907,7 +9897,7 @@
|
||||||
while(Array.isArray(libOnload2)&&libOnload2.length){
|
while(Array.isArray(libOnload2)&&libOnload2.length){
|
||||||
const fun=libOnload2.shift();
|
const fun=libOnload2.shift();
|
||||||
if(typeof fun!="function") continue;
|
if(typeof fun!="function") continue;
|
||||||
yield gnc.await(fun());
|
yield (gnc.is.generatorFunc(fun)?gnc.of(fun):fun)();
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
startOnline:function(){
|
startOnline:function(){
|
||||||
|
@ -33640,7 +33630,7 @@
|
||||||
if(!lib.imported[type])lib.imported[type]={};
|
if(!lib.imported[type])lib.imported[type]={};
|
||||||
if(typeof _status.importing=="undefined")_status.importing={};
|
if(typeof _status.importing=="undefined")_status.importing={};
|
||||||
if(!_status.importing[type])_status.importing[type]=[];
|
if(!_status.importing[type])_status.importing[type]=[];
|
||||||
const promise=gnc.await(content(lib,game,ui,get,ai,_status)).then(content2=>{
|
const promise=Promise.resolve((gnc.is.generator(content)?gnc.of(content):content)(lib,game,ui,get,ai,_status)).then(content2=>{
|
||||||
if(content2.name){
|
if(content2.name){
|
||||||
lib.imported[type][content2.name]=content2;
|
lib.imported[type][content2.name]=content2;
|
||||||
delete content2.name;
|
delete content2.name;
|
||||||
|
@ -33650,10 +33640,10 @@
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
loadExtension:gnc.async(function*(obj){
|
loadExtension:gnc.of(function*(obj){
|
||||||
var noeval=false;
|
var noeval=false;
|
||||||
if(typeof obj=='function'){
|
if(typeof obj=='function'){
|
||||||
obj=yield gnc.await(obj(lib,game,ui,get,ai,_status));
|
obj=yield (gnc.is.generatorFunc(obj)?gnc.of(obj):obj)(lib,game,ui,get,ai,_status);
|
||||||
noeval=true;
|
noeval=true;
|
||||||
}
|
}
|
||||||
lib.extensionMenu['extension_'+obj.name]={
|
lib.extensionMenu['extension_'+obj.name]={
|
||||||
|
@ -33771,7 +33761,7 @@
|
||||||
}
|
}
|
||||||
if(obj.precontent){
|
if(obj.precontent){
|
||||||
_status.extension=obj.name;
|
_status.extension=obj.name;
|
||||||
yield gnc.await(obj.precontent(cfg));
|
yield (gnc.is.generatorFunc(obj.precontent)?gnc.of(obj.precontent):obj.precontent)(cfg);
|
||||||
delete _status.extension;
|
delete _status.extension;
|
||||||
}
|
}
|
||||||
if(obj.content){
|
if(obj.content){
|
||||||
|
@ -33825,7 +33815,7 @@
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
importExtension:gnc.async(function*(data,finishLoad,exportext,pkg){
|
importExtension:gnc.of(function*(data,finishLoad,exportext,pkg){
|
||||||
//by 来瓶可乐加冰
|
//by 来瓶可乐加冰
|
||||||
if(!window.JSZip)
|
if(!window.JSZip)
|
||||||
yield new Promise((resolve,reject)=>lib.init.js(`${lib.assetURL}game`,"jszip",resolve,reject));
|
yield new Promise((resolve,reject)=>lib.init.js(`${lib.assetURL}game`,"jszip",resolve,reject));
|
||||||
|
|
Loading…
Reference in New Issue