Are lightning event synchronous or asynchronous?
闪电事件是同步还是异步?
component1:
<aura:component>
<aura:handler name="event1" event="zced:TypeDoesNotMatter"
action="{!c.event1Function}"/>
<aura:handler name="event2" event="zced:TypeDoesNotMatter"
action="{!c.event2Function}"/>
<component2/>
</aura:component>
component2:
<aura:component>
<aura:registerEvent name="event1" type="zced:TypeDoesNotMatter"/>
<aura:registerEvent name="event2" type="zced:TypeDoesNotMatter"/>
</aura:component>
controller of componen2:
组件控制器:
const event1 = component.getEvent("event1").fire();
const event2 = component.getEvent("event2").fire();
In other words is it guaranteed by the framework that after calling controller of componen2 the function event1Function in component1`s controller will be called before event2Function in component1`s controller?
换句话说是框架保证在调用componen2的控制器之后,component1的控制器中的函数event1Function将在component1的控制器中的event2Function之前调用吗?
1 个解决方案
#1
2
Are lightning event synchronous or asynchronous?
闪电事件是同步还是异步?
Yes.
Asynchronous
When fire() is called, the event is placed in to a queue for later execution.
调用fire()时,会将事件放入队列以供以后执行。
Atomic
Until canceled or completed, only one event will be in the "executing" phase at any given time.
在取消或完成之前,在任何给定时间只有一个事件处于“执行”阶段。
Synchronous
Once an event has been fully handled, the event queue is checked and the next event is fired. This continues until the queue is empty, at which point the life cycle concludes with a render/reRender cycle.
完全处理完事件后,将检查事件队列并触发下一个事件。这一直持续到队列为空,此时生命周期以render / reRender循环结束。
Single-Threaded
JavaScript can only run one thread at a time (ignoring things like ServiceWorker), which means that once the Aura life cycle starts, it will conclude without interruption from external events. It will process all managed events in the order they're placed in the queue, and nothing will change that order.
JavaScript一次只能运行一个线程(忽略像ServiceWorker这样的东西),这意味着一旦Aura生命周期开始,它就会在不中断外部事件的情况下结束。它将按照它们放入队列的顺序处理所有托管事件,并且不会改变该顺序。
Not Guaranteed
However, even though the entire system design is essentially guaranteed that events will happen in the order you expect them to, there is no written guarantee that this is the case, or will continue to be the case in the future. You could choose to rely on this behavior, but if the nature of either JavaScript or Aura changes, you might find that your events no longer work as you expect, or randomly fail because of race conditions.
然而,即使整个系统设计基本上保证事件将按照您期望的顺序发生,但是没有书面保证这种情况,或者将来会继续如此。您可以选择依赖此行为,但如果JavaScript或Aura的性质发生变化,您可能会发现您的事件不再按预期工作,或因竞争条件而随机失败。
I would say that the risk is minimal, but non-zero. I wouldn't rely on this design in my own code, but I also wouldn't stop people from using it. If it's the pattern that makes the most sense, then you can certainly go for it. I'd probably use a slightly larger event that includes handling information, and let the handler decide if event1Function or event2Function would be called based on parameters.
我会说风险很小,但非零。我不会在我自己的代码中依赖这个设计,但我也不会阻止人们使用它。如果这是最有意义的模式,那么你当然可以去做。我可能会使用一个稍大的事件,包括处理信息,让处理程序决定是否根据参数调用event1Function或event2Function。
#1
2
Are lightning event synchronous or asynchronous?
闪电事件是同步还是异步?
Yes.
Asynchronous
When fire() is called, the event is placed in to a queue for later execution.
调用fire()时,会将事件放入队列以供以后执行。
Atomic
Until canceled or completed, only one event will be in the "executing" phase at any given time.
在取消或完成之前,在任何给定时间只有一个事件处于“执行”阶段。
Synchronous
Once an event has been fully handled, the event queue is checked and the next event is fired. This continues until the queue is empty, at which point the life cycle concludes with a render/reRender cycle.
完全处理完事件后,将检查事件队列并触发下一个事件。这一直持续到队列为空,此时生命周期以render / reRender循环结束。
Single-Threaded
JavaScript can only run one thread at a time (ignoring things like ServiceWorker), which means that once the Aura life cycle starts, it will conclude without interruption from external events. It will process all managed events in the order they're placed in the queue, and nothing will change that order.
JavaScript一次只能运行一个线程(忽略像ServiceWorker这样的东西),这意味着一旦Aura生命周期开始,它就会在不中断外部事件的情况下结束。它将按照它们放入队列的顺序处理所有托管事件,并且不会改变该顺序。
Not Guaranteed
However, even though the entire system design is essentially guaranteed that events will happen in the order you expect them to, there is no written guarantee that this is the case, or will continue to be the case in the future. You could choose to rely on this behavior, but if the nature of either JavaScript or Aura changes, you might find that your events no longer work as you expect, or randomly fail because of race conditions.
然而,即使整个系统设计基本上保证事件将按照您期望的顺序发生,但是没有书面保证这种情况,或者将来会继续如此。您可以选择依赖此行为,但如果JavaScript或Aura的性质发生变化,您可能会发现您的事件不再按预期工作,或因竞争条件而随机失败。
I would say that the risk is minimal, but non-zero. I wouldn't rely on this design in my own code, but I also wouldn't stop people from using it. If it's the pattern that makes the most sense, then you can certainly go for it. I'd probably use a slightly larger event that includes handling information, and let the handler decide if event1Function or event2Function would be called based on parameters.
我会说风险很小,但非零。我不会在我自己的代码中依赖这个设计,但我也不会阻止人们使用它。如果这是最有意义的模式,那么你当然可以去做。我可能会使用一个稍大的事件,包括处理信息,让处理程序决定是否根据参数调用event1Function或event2Function。