Skip to content

Commit

Permalink
only notify the latest subscriber during client.subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolodavis committed Oct 25, 2019
1 parent 5a13f00 commit 9596fa4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ class _ClientImpl {
const id = Object.keys(this.subscribers).length;
this.subscribers[id] = fn;
this.transport.subscribe(() => this.notifySubscribers());
this.notifySubscribers();
fn(this.getState());

// Return a handle that allows the caller to unsubscribe.
return () => {
Expand Down
20 changes: 18 additions & 2 deletions src/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,20 @@ describe('subscribe', () => {
const fn2 = jest.fn();
const unsubscribe = client.subscribe(fn2);

// The subscriber that just subscribed is notified.
expect(fn).not.toBeCalled();
expect(fn2).toBeCalledWith(
expect.objectContaining({
G: { moved: true },
})
);

fn.mockClear();
fn2.mockClear();

client.moves.A();

// Both subscribers are notified.
expect(fn).toBeCalledWith(
expect.objectContaining({
G: { moved: true },
Expand All @@ -618,11 +632,13 @@ describe('subscribe', () => {
G: { moved: true },
})
);
fn.mockClear();
fn2.mockClear();

unsubscribe();

fn.mockClear();
fn2.mockClear();

// The subscriber the unsubscribed is not notified.
client.moves.A();
expect(fn).toBeCalledWith(
expect.objectContaining({
Expand Down

0 comments on commit 9596fa4

Please sign in to comment.