-
Notifications
You must be signed in to change notification settings - Fork 716
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -303,10 +303,25 @@ class _ClientImpl { | |
} | ||
|
||
subscribe(fn) { | ||
const callback = () => fn(this.getState()); | ||
this.transport.subscribe(callback); | ||
// If we already have a subscription, then create a new | ||
// callback that invokes both the old and new subscriptions. | ||
const prev = this.subscribeCallback; | ||
const callback = () => { | ||
prev(); | ||
fn(this.getState()); | ||
}; | ||
|
||
this.subscribeCallback = callback; | ||
this.transport.subscribe(callback); | ||
callback(); | ||
|
||
// Return a handle that allows the caller to unsubscribe. | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
nicolodavis
Author
Member
|
||
// Warning: Will revert any callbacks that were added | ||
// after this current call to subscribe(), so use it to | ||
// only remove the latest subscription. | ||
return () => { | ||
this.subscribeCallback = prev; | ||
}; | ||
} | ||
|
||
getState() { | ||
|
@nicolodavis
the unsubscribe behavior is very unexpected and practically does not enable more than 2 subscribers.
it would be simple to just use something like rxjs stream code, but otherwise (if you want to avoid the overhead for now) implementing this properly is also not that difficult, just need to have an object of ids to callback-fn, the unsubscribe function has the id in the closure and removes it from the object (I will have sent a pull request but I don't have time right now, this is already me procrastinating something else)