Skip to content

✨ [Observable] Make observable safe to remove handler while firing.#37887

Merged
mszylkowski merged 22 commits intoampproject:mainfrom
mszylkowski:observer_removeafter
Mar 17, 2022
Merged

✨ [Observable] Make observable safe to remove handler while firing.#37887
mszylkowski merged 22 commits intoampproject:mainfrom
mszylkowski:observer_removeafter

Conversation

@mszylkowski
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 requested a review from jridgewell March 17, 2022 17:10
@mszylkowski mszylkowski marked this pull request as ready for review March 17, 2022 17:11
@amp-owners-bot
Copy link
Copy Markdown

Hey @jridgewell! These files were changed:

src/core/data-structures/observable.js

@mszylkowski
Copy link
Copy Markdown
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