From d6941b3774073a186b627b672f7e84cb8431a403 Mon Sep 17 00:00:00 2001 From: shijian <2954700422@qq.com> Date: Mon, 18 Dec 2023 00:29:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dasync=20content=E4=B8=AD?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E4=B8=8D=E4=BD=BF=E7=94=A8await=E4=BC=9A?= =?UTF-8?q?=E6=8A=A5=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/game.js | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/game/game.js b/game/game.js index 973ae0b99..3e14af9d2 100644 --- a/game/game.js +++ b/game/game.js @@ -32143,21 +32143,29 @@ new Promise(resolve=>{ if(!_status.event) return; // game.createEvent的时候还没立即push到next里 Promise.resolve().then(()=>{ - const eventPromise=_status.event.next.find(e=>e.toEvent()==event); - // 如果父级事件也是一个异步的话,那应该立即执行这个事件的 - // 如果在AsyncFunction执行过程中在别的位置新建了一个异步事件,那也直接(等会set配置完)执行 - if(eventPromise&&_status.event.content instanceof AsyncFunction){ - if(_status.event!=eventPromise){ - eventPromise.parent=_status.event; - _status.event=eventPromise; - game.getGlobalHistory('everything').push(eventPromise); + const callback=()=>{ + let eventPromise=_status.event.next.find(e=>e.toEvent()==event); + // 如果父级事件也是一个异步的话,那应该立即执行这个事件的 + // 如果在AsyncFunction执行过程中在别的位置新建了一个异步事件,那也直接(等会set配置完)执行 + 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 + game.executingAsyncEvent=Promise.resolve().then(()=>game.loop(eventPromise)) + .then(()=>{ + delete game.executingAsyncEvent; + }); } - // 异步执行game.loop - // 不直接game.loop(event)是因为需要让别人可以手动set()和setContent() - // 再执行game.loop是因为原有的game.loop被await卡住了, - // 得新执行一个只执行这个异步事件的game.loop - Promise.resolve().then(()=>game.loop(eventPromise)); - } + }; + // 没有await上个事件(也就是_status.event) + if(game.executingAsyncEvent) game.executingAsyncEvent.then(callback); + else callback(); }); }); this.#event=event;