-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ [Observable] Make observable safe to remove handler while firing. #37887
✨ [Observable] Make observable safe to remove handler while firing. #37887
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Old Code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
fire
iteration 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
i
so it gets skipped.