Skip to content
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

Merged
merged 22 commits into from
Mar 17, 2022

Conversation

mszylkowski
Copy link
Contributor

@mszylkowski mszylkowski commented Mar 17, 2022

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.

const observable = new Observable();
for (let i = 0; i < 100; i++) {
    const remove = observable.add(() => {
        console.log(i);
        remove();
    })
}
observable.fire();

This will fire only half of the observers because the fire iteration will remove the items as it goes (simulated run):

// This skips 1/2 of the handlers
for (const handler of this.handlers_) {
    handler();
    this.handlers_.remove(handler);
}

is the same as

// This skips 1/2 of the handlers
for (let i = 0; i < this.handlers_.length; i++) {
    handler();
    this.handlers_.remove(handler);
}

beacuse after each iteration the item that's supposed to go next is at the index i so it gets skipped.

Copy link
Contributor

@jridgewell jridgewell left a 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;
Copy link
Contributor

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

Copy link
Contributor Author

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Old Code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, removing it now

@mszylkowski
Copy link
Contributor Author

This approach would be good with me. Mind splitting this from the other PR?

Sounds good. I'll hold off the other one until this is merged and then sync it.

@mszylkowski
Copy link
Contributor Author

Added a counter to keep track of the iterating depth so that we can remove the handlers only after all the iterations are done.

@mszylkowski mszylkowski marked this pull request as ready for review March 17, 2022 17:11
@amp-owners-bot
Copy link

Hey @jridgewell! These files were changed:

src/core/data-structures/observable.js

@mszylkowski
Copy link
Contributor Author

cc @ampproject/wg-stories

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants