✨ [Observable] Make observable safe to remove handler while firing.#37887
✨ [Observable] Make observable safe to remove handler while firing.#37887mszylkowski merged 22 commits intoampproject:mainfrom
Conversation
jridgewell
left a comment
There was a problem hiding this comment.
This approach would be good with me. Mind splitting this from the other PR?
| for (const handler of this.handlers_) { | ||
| handler(opt_event); | ||
| } | ||
| this.iterating_ = false; |
There was a problem hiding this comment.
Nit: this isn't capable of reentry, but it may never come up in practice. We could store a local wasIteratingBefore, and only cleanup if !wasIteratingBefore
There was a problem hiding this comment.
Do you mind explaining more? Is it that it won't work if it fires the observer again inside a fire handler?
| for (const handler of this.handlersToRemove_) { | ||
| removeItem(this.handlers_, handler); | ||
| } | ||
| if (this.handlersToRemove_.size) { |
There was a problem hiding this comment.
Yes, removing it now
Sounds good. I'll hold off the other one until this is merged and then sync it. |
|
Added a counter to keep track of the iterating depth so that we can remove the handlers only after all the iterations are done. |
|
Hey @jridgewell! These files were changed: |
|
cc @ampproject/wg-stories |
We need observables to remove handlers safely while firing.
If theres multiple handlers that in the function that remove themselves, only half of the handlers will fire because the iteration doesn't work well with removing items from the iteration.
This will fire only half of the observers because the
fireiteration will remove the items as it goes (simulated run):is the same as
beacuse after each iteration the item that's supposed to go next is at the index
iso it gets skipped.