event全面修改为promise,包括event.next和event.after的元素。

This commit is contained in:
shijian 2023-12-13 08:59:31 +08:00
parent e16406ee59
commit f08becf03a
1 changed files with 45 additions and 44 deletions

View File

@ -31258,6 +31258,7 @@ new Promise(resolve=>{
} }
}, },
GameEvent:class{ GameEvent:class{
/** @type { Promise<GameEvent> & GameEvent & GameEventPromise } */
#promise; #promise;
/** /**
* @param {string} [name] * @param {string} [name]
@ -31279,24 +31280,24 @@ new Promise(resolve=>{
* @type {GameEvent[]} * @type {GameEvent[]}
*/ */
this.next=[]; this.next=[];
Object.defineProperty(this.next,'remove',{ // Object.defineProperty(this.next,'remove',{
enumerable:false, // enumerable:false,
value:function(){ // value:function(){
for(const item of arguments){ // for(const item of arguments){
let pos=-1; // let pos=-1;
if (typeof item=='number'&&isNaN(item)){ // if (typeof item=='number'&&isNaN(item)){
pos=this.findIndex(v=>isNaN(v)) // pos=this.findIndex(v=>isNaN(v))
}else if(item instanceof lib.element.GameEventPromise){ // }else if(item instanceof lib.element.GameEventPromise){
pos=this.indexOf(item.toEvent()); // pos=this.indexOf(item.toEvent());
}else{ // }else{
pos=this.indexOf(item); // pos=this.indexOf(item);
} // }
if(pos==-1) continue; // if(pos==-1) continue;
this.splice(pos,1); // this.splice(pos,1);
} // }
return this; // return this;
} // }
}); // });
/** /**
* @type {GameEvent[]} * @type {GameEvent[]}
*/ */
@ -31643,18 +31644,18 @@ new Promise(resolve=>{
return this._rand; return this._rand;
} }
insert(content,map){ insert(content,map){
const next=new lib.element.GameEvent(`${this.name}Inserted`,false); const next=(new lib.element.GameEvent(`${this.name}Inserted`,false)).toPromise();
this.next.push(next); this.next.push(next);
next.setContent(content); next.setContent(content);
Object.entries(map).forEach(entry=>next.set(entry[0],entry[1])); Object.entries(map).forEach(entry=>next.set(entry[0],entry[1]));
return next.toPromise(); return next;
} }
insertAfter(content,map){ insertAfter(content,map){
const next=new lib.element.GameEvent(`${this.name}Inserted`,false); const next=(new lib.element.GameEvent(`${this.name}Inserted`,false)).toPromise();
this.after.push(next); this.after.push(next);
next.setContent(content); next.setContent(content);
Object.entries(map).forEach(entry=>next.set(entry[0],entry[1])); Object.entries(map).forEach(entry=>next.set(entry[0],entry[1]));
return next.toPromise(); return next;
} }
backup(skill){ backup(skill){
this._backup={ this._backup={
@ -32157,20 +32158,25 @@ new Promise(resolve=>{
event.async=true; event.async=true;
// 事件结束后触发resolve // 事件结束后触发resolve
event.resolve=resolve; event.resolve=resolve;
// 如果父级事件也是一个异步的话,那应该立即执行这个事件的 if(!_status.event) return;
// 如果在AsyncFunction执行过程中在别的位置新建了一个异步事件那也直接等会set配置完执行 // game.createEvent的时候还没立即push到next里
if(_status.event&&_status.event.next.includes(event)&&_status.event.content instanceof AsyncFunction){ Promise.resolve().then(()=>{
if (_status.event!=event) { const eventPromise=_status.event.next.find(e=>e.toEvent()==event);
event.parent=_status.event; // 如果父级事件也是一个异步的话,那应该立即执行这个事件的
_status.event=event; // 如果在AsyncFunction执行过程中在别的位置新建了一个异步事件那也直接等会set配置完执行
game.getGlobalHistory('everything').push(event); if(eventPromise&&_status.event.content instanceof AsyncFunction){
if(_status.event!=eventPromise){
eventPromise.parent=_status.event;
_status.event=eventPromise;
game.getGlobalHistory('everything').push(eventPromise);
}
// 异步执行game.loop
// 不直接game.loop(event)是因为需要让别人可以手动set()和setContent()
// 再执行game.loop是因为原有的game.loop被await卡住了
// 得新执行一个只执行这个异步事件的game.loop
Promise.resolve().then(()=>game.loop(eventPromise));
} }
// 异步执行game.loop });
// 不直接game.loop(event)是因为需要让别人可以手动set()和setContent()
// 再执行game.loop是因为原有的game.loop被await卡住了
// 得新执行一个只执行这个异步事件的game.loop
Promise.resolve().then(()=>game.loop(event));
}
}); });
this.#event=event; this.#event=event;
return new Proxy(this,{ return new Proxy(this,{
@ -32183,11 +32189,6 @@ new Promise(resolve=>{
return thisValue; return thisValue;
} }
const eventValue=Reflect.get(event,prop); const eventValue=Reflect.get(event,prop);
// 返回值如果是event则修改为GameEventPromise类实例
if(typeof eventValue=='function') return (function(...args){
const returnValue=eventValue.call(event,...args);
return returnValue==event?receiver:returnValue;
}).bind(event);
return eventValue==event?receiver:eventValue; return eventValue==event?receiver:eventValue;
}, },
set(target,prop,newValue){ set(target,prop,newValue){
@ -40365,9 +40366,9 @@ new Promise(resolve=>{
* @legacy Use {@link lib.element.GameEvent.constructor} instead. * @legacy Use {@link lib.element.GameEvent.constructor} instead.
*/ */
createEvent:(name,trigger,triggerEvent)=>{ createEvent:(name,trigger,triggerEvent)=>{
const next=new lib.element.GameEvent(name,trigger); const next=(new lib.element.GameEvent(name,trigger)).toPromise();
(triggerEvent||_status.event).next.push(next); (triggerEvent||_status.event).next.push(next);
return next.toPromise(); return next;
}, },
addCharacter:(name,information)=>{ addCharacter:(name,information)=>{
const extensionName=_status.extension||information.extension,character=[ const extensionName=_status.extension||information.extension,character=[
@ -41410,7 +41411,7 @@ new Promise(resolve=>{
} }
}, },
/** /**
* @param { GameEvent } [belongAsyncEvent] * @param { Promise<GameEvent> & GameEvent & GameEventPromise } [belongAsyncEvent]
*/ */
async loop(belongAsyncEvent){ async loop(belongAsyncEvent){
if(belongAsyncEvent){ if(belongAsyncEvent){
@ -41424,7 +41425,7 @@ new Promise(resolve=>{
const _resolve = () => { const _resolve = () => {
if (event.async) { if (event.async) {
if (typeof event.resolve == 'function') { if (typeof event.resolve == 'function') {
event.resolve(event); event.resolve(event.toEvent());
} else { } else {
throw new TypeError('异步事件的event.resolve未赋值使用await时将会被永久等待'); throw new TypeError('异步事件的event.resolve未赋值使用await时将会被永久等待');
} }