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

Client.subscribe breaks after unsubscribing #1137

Open
CJPrez opened this issue Mar 2, 2023 · 0 comments
Open

Client.subscribe breaks after unsubscribing #1137

CJPrez opened this issue Mar 2, 2023 · 0 comments

Comments

@CJPrez
Copy link

CJPrez commented Mar 2, 2023

The Client subscribe method has an bug in how it internally stores the handles to registered subscribers. The code starts like this:

 subscribe(fn: (state: ClientState<G>) => void) {
    const id = Object.keys(this.subscribers).length;
    this.subscribers[id] = fn;
    ...

It is using the count of subscribes as a property index to register a new subscriber. That will then break future subscribers if anyone but the most recent subscriber unsubscribes.

As an example scenario:

  1. Subscriber A subscribes to the client and gets assigned ID 0
  2. Subscriber B subscribes to the client and gets assigned ID 1
  3. Subscriber A unsubscribes. The count of subscribers is now 1
  4. Subscriber C subscribes to the client and gets assigned ID 1... again. Subscriber B no longer gets notifications.
  5. Subscriber D subscribes to the client and gets assigned ID 1... again... again. Subscriber C no longer gets notifications.
  6. etc...

The client implementation could keep the public facing API it has but internally use a CustomEvent object to store the subscribed handlers to avoid managing the subscriptions itself.

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

No branches or pull requests

1 participant