事件挂起时附加新的侦听器会发生什么? [英] What happens when new listener is attached when an event is pending?

查看:99
本文介绍了事件挂起时附加新的侦听器会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个网络工作者_worker,并且像这样附加一个侦听器:

Say I have a web worker _worker, and I attach a listener to it like so:

_worker.addEventListener('message', (event) => {
    window["test"] = event.data||"foo"
});

比方说,我稍后有条件地将另一个侦听器附加到一个promise中,例如:

Let's say I later conditionally attach another listener inside a promise like so:

if (window["test"] === undefined) {
    _worker.addEventListener('message', (event) => {
        window["test"] = event.data || "foo";
        resolve(window["test"])
    });
}
else {
    resolve (window["test"]);
}

基本上,第一个侦听器的工作是将window ["test"]设置为事件数据. Promise生成器检查第一个侦听器是否已经设置了window ["test"].如果没有,我们将在工作者消息事件被触发时附加另一个侦听器来解决诺言.

Basically the first listener's job is to set window["test"] to the event data. The promise generator checks to see if window["test"] is already set by the first listener; if not, we attach another listener to resolve the promise when the worker message event is fired.

但是,如果在附加第二个侦听器时,工作人员已经触发了一个消息事件,而事件循环正在等待触发第一个侦听器,该怎么办?将第二个侦听器包含在要通知该事件的侦听器列表中吗?

But what if, as we are attaching the second listener, the worker has already fired a message event, and the event loop is waiting to fire the first listener? Will the second listener be included in the list of listeners to be notified of the event?

推荐答案

当我们附加第二个侦听器时,该工作人员已经触发了一个消息事件,并且事件循环正在等待触发第一个侦听器

As we are attaching the second listener, the worker has already fired a message event, and the event loop is waiting to fire the first listener

是的,第二个侦听器将包括在侦听器列表中,以便在此处通知该事件.如果事件循环仍在等待调用侦听器,则即使主线程已触发事件并且正在队列中等待事件,该事件仍未在主流程中真正发生.

Yes, the second listener will be included in the list of listeners to be notified of the event here. If the event loop is still waiting to call the listeners, the event has not yet really occurred in the main process, even when the worker has already triggered it and it is waiting in the queue.

您确实可能错过事件的最极端情况是,在执行message侦听器的过程中创建了诺言,该侦听器在创建window.test的侦听器之前运行,即当您有多个回调在侦听同一事件时.因此,最好立即安装侦听器,尤其是无论如何都要对它作出承诺的时候.您可以立即创建承诺:

The edge case where you could indeed miss the event was if the promise was created during the execution of a message listener that runs before the one that creates window.test, i.e. when you have multiple callbacks listening to the same event. Therefore it's always better to install listeners immediately, especially when you are going to make a promise for it anyway. You can create the promise right away:

window.test = new Promise(resolve => {
    _worker.addEventListener('message', (event) => {
         resolve(event.data || "foo");
    });
});

然后在各处使用Promise(根据需要多次).当promise已经为您完成时,您无需自己存储和缓存数据值.

Then use the promise everywhere (as many times as necessary). No need to store-and-cache the data value yourself when promises already do that for you.

这篇关于事件挂起时附加新的侦听器会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆