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

Change onBecome(Un)Observed implementation to work with multiple listeners #1771

Closed
wants to merge 1 commit into from

Conversation

michalwarda
Copy link

This PR fixes #1537

PR checklist:

  • Added unit tests
  • Updated changelog
  • Updated docs (either in the description of this PR as markdown, or as separate PR on the gh-pages branch. Please refer to this PR). For new functionality, at least API.md should be updated
  • Added typescript typings
  • Verified that there is no significant performance drop (npm run perf)

In this PR I've changed the implementation of onBecomeObserved and onBecomeUnobserved to allow for unsubscribing previous subscription.
In the new implementation I keep the listeners in Sets rather than create a chain of calls as previously.
Also for performance reasons I've created custom getters that lazily initialise the Sets.

This might break external libraries that implement on IObservable. I thought about instead of changing the interface of IObservable, add a custom getter and setter to onBecomeObserved and onBecomeUnobserved to keep the previous interface but I decided to wait for a hint from you to decide on that.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) to 93.75% when pulling 599b4ea on elpassion:master into 3f161f3 on mobxjs:master.

Copy link
Member

@mweststrate mweststrate left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Left one remark to make the change less expensive!

Note that there is a performance test script that will reveal memory consumption, although it is not very accurate as it is just the mem consumption of the process

private unobservedListeners?: Set<Function>

get onBecomeObservedListeners(): Set<Function> {
if (this.observedListeners === undefined) this.observedListeners = new Set()
Copy link
Member

Choose a reason for hiding this comment

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

The getter causes the set to be initializaed lazily, but effectively for every atom, correct (as MobX core will often inspect this getter)? So this means that every atom now will have an additional Set to allocate.

I would prefer to see a solution that doesn't allocate the Set (or just a shared, empty, frozen one), until the first listeners is established, and only then instantiate the local Set of listeners

@mweststrate
Copy link
Member

Closed in favor of #1833

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

Successfully merging this pull request may close these issues.

onBecome(Un)Observed intercept hooks can remove each other
3 participants