diff --git a/src/client/client.js b/src/client/client.js index f0aee4118..dc841d73f 100644 --- a/src/client/client.js +++ b/src/client/client.js @@ -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 () => { diff --git a/src/client/client.test.js b/src/client/client.test.js index ce6c582fe..5364aab57 100644 --- a/src/client/client.test.js +++ b/src/client/client.test.js @@ -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 }, @@ -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({