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

Multiple once calls cancel each other #20

Closed
TimoBechtel opened this issue Feb 15, 2021 · 2 comments
Closed

Multiple once calls cancel each other #20

TimoBechtel opened this issue Feb 15, 2021 · 2 comments
Labels
bug Something isn't working released

Comments

@TimoBechtel
Copy link
Owner

what it does

When once is called multiple times with the same path, the first one that receives an update will unsubscribe the path and therefore the second one is never called.

db.get('path').once(() => {
  // receives update and unsubscribes from path
});
db.get('path').once(() => {
  // never receives update
})

db.get('path').set('test');

Issue is basically this line:

unsubscribe(path, listener);

what it should do

All once listener should be called when an update arrives.

@TimoBechtel TimoBechtel added the bug Something isn't working label Feb 15, 2021
@TimoBechtel
Copy link
Owner Author

Now, this took me a while to figure out, but the issue comes down to this:

For some reason I chose to use Array.splice to remove listeners.
This was a really bad idea. Because now, if someone unsubscribes from a path while an iteration over all listeners is in progress, the iteration does behave weirdly, as splice will remove items in place. Therefore skipping the last once in the example above.

Replacing splice with a simple Array.filter should be enough to fix the issue.

github-actions bot pushed a commit that referenced this issue Feb 17, 2021
## [3.6.1](v3.6.0...v3.6.1) (2021-02-17)

### Bug Fixes

* **client:** once cancels other subscriptions ([7e68a87](7e68a87)), closes [#20](#20)
@github-actions
Copy link
Contributor

🎉 This issue has been resolved in version 3.6.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

No branches or pull requests

1 participant