Skip to content

Commit 3206548

Browse files
committed
don't invoke callback on subscribe in multiplayer mode unless client is already connected
1 parent 9596fa4 commit 3206548

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

src/client/client.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class _ClientImpl {
104104
this.debug = debug;
105105
this.gameStateOverride = null;
106106
this.subscribers = {};
107+
this._running = false;
107108

108109
this.reducer = CreateGameReducer({
109110
game: this.game,
@@ -320,6 +321,8 @@ class _ClientImpl {
320321

321322
start() {
322323
this.transport.connect();
324+
this.notifySubscribers();
325+
this._running = true;
323326

324327
if (
325328
process.env.NODE_ENV !== 'production' &&
@@ -341,6 +344,7 @@ class _ClientImpl {
341344

342345
stop() {
343346
this.transport.disconnect();
347+
this._running = false;
344348

345349
if (this._debugPanel != null) {
346350
this._debugPanel.$destroy();
@@ -352,7 +356,10 @@ class _ClientImpl {
352356
const id = Object.keys(this.subscribers).length;
353357
this.subscribers[id] = fn;
354358
this.transport.subscribe(() => this.notifySubscribers());
355-
fn(this.getState());
359+
360+
if (this._running || !this.multiplayer) {
361+
fn(this.getState());
362+
}
356363

357364
// Return a handle that allows the caller to unsubscribe.
358365
return () => {

src/client/client.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,31 @@ describe('subscribe', () => {
660660
client.transport.callback();
661661
expect(fn).toHaveBeenCalled();
662662
});
663+
664+
describe('multiplayer', () => {
665+
test('subscribe before start', () => {
666+
const fn = jest.fn();
667+
const client = Client({
668+
game: {},
669+
multiplayer: { local: true },
670+
});
671+
client.subscribe(fn);
672+
expect(fn).not.toBeCalled();
673+
client.start();
674+
expect(fn).toBeCalled();
675+
});
676+
677+
test('subscribe after start', () => {
678+
const fn = jest.fn();
679+
const client = Client({
680+
game: {},
681+
multiplayer: { local: true },
682+
});
683+
client.start();
684+
client.subscribe(fn);
685+
expect(fn).toBeCalled();
686+
});
687+
});
663688
});
664689

665690
test('override game state', () => {

0 commit comments

Comments
 (0)