Skip to content

Commit 2f86d92

Browse files
committed
rename mount/unmount to start/stop
The Client usage is now: const client = Client({ ... }); client.subscribe(...); client.start(); If used inside a component that can mount or unmount: constructor() { this.client = Client({ ... }); } mount() { this.unsubscribe = this.client.subscribe(...); this.client.start(); } unount() { this.unsubscribe(); this.client.stop(); }
1 parent c77ba53 commit 2f86d92

8 files changed

Lines changed: 90 additions & 50 deletions

File tree

src/client/client.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ class _ClientImpl {
253253
subscribe: () => {},
254254
subscribeGameMetadata: _metadata => {}, // eslint-disable-line no-unused-vars
255255
connect: () => {},
256+
disconnect: () => {},
256257
updateGameID: () => {},
257258
updatePlayerID: () => {},
258259
};
@@ -317,7 +318,9 @@ class _ClientImpl {
317318
this.notifySubscribers();
318319
}
319320

320-
mount() {
321+
start() {
322+
this.transport.connect();
323+
321324
if (
322325
process.env.NODE_ENV !== 'production' &&
323326
this.debug !== false &&
@@ -336,7 +339,9 @@ class _ClientImpl {
336339
}
337340
}
338341

339-
unmount() {
342+
stop() {
343+
this.transport.disconnect();
344+
340345
if (this._debugPanel != null) {
341346
this._debugPanel.$destroy();
342347
this._debugPanel = null;
@@ -408,10 +413,6 @@ class _ClientImpl {
408413
return ret;
409414
}
410415

411-
connect() {
412-
this.transport.connect();
413-
}
414-
415416
createDispatchers() {
416417
this.moves = createMoveDispatchers(
417418
this.game.moveNames,

src/client/client.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ describe('multiplayer', () => {
182182
game: { moves: { A: () => {} } },
183183
multiplayer: { server: host + ':' + port },
184184
});
185-
client.connect();
185+
client.start();
186186
});
187187

188188
afterAll(() => {
@@ -214,7 +214,7 @@ describe('multiplayer', () => {
214214
game: {},
215215
multiplayer: true,
216216
});
217-
client.connect();
217+
client.start();
218218
});
219219

220220
test('correct transport used', () => {
@@ -236,8 +236,8 @@ describe('multiplayer', () => {
236236
client0 = Client({ ...spec, playerID: '0' });
237237
client1 = Client({ ...spec, playerID: '1' });
238238

239-
client0.connect();
240-
client1.connect();
239+
client0.start();
240+
client1.start();
241241
});
242242

243243
test('correct transport used', () => {
@@ -650,16 +650,16 @@ test('override game state', () => {
650650
expect(client.getState().G).toEqual({ moved: true });
651651
});
652652

653-
describe('mount / unmount', () => {
653+
describe('start / stop', () => {
654654
test('mount on custom element', () => {
655655
const el = document.createElement('div');
656656
const client = Client({ game: {}, debug: { target: el } });
657-
client.mount();
658-
client.unmount();
657+
client.start();
658+
client.stop();
659659
});
660660

661-
test('unmount', () => {
661+
test('try to stop without starting', () => {
662662
const client = Client({ game: {} });
663-
client.unmount();
663+
client.stop();
664664
});
665665
});

src/client/react-native.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export function Client(opts) {
6767
gameID: props.gameID,
6868
playerID: props.playerID,
6969
credentials: props.credentials,
70+
debug: false,
7071
socketOpts: {
7172
transports: ['websocket'],
7273
},
@@ -75,11 +76,12 @@ export function Client(opts) {
7576
}
7677

7778
componentDidMount() {
78-
this.client.connect();
7979
this.unsubscribe = this.client.subscribe(() => this.forceUpdate());
80+
this.client.start();
8081
}
8182

8283
componentWillUnmount() {
84+
this.client.stop();
8385
this.unsubscribe();
8486
}
8587

src/client/react.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,13 @@ export function Client(opts) {
9999
}
100100

101101
componentDidMount() {
102-
this.client.connect();
103102
this.unsubscribe = this.client.subscribe(() => this.forceUpdate());
104-
this.client.mount();
103+
this.client.start();
105104
}
106105

107106
componentWillUnmount() {
107+
this.client.stop();
108108
this.unsubscribe();
109-
this.client.unmount();
110109
}
111110

112111
componentDidUpdate(prevProps) {

src/client/transport/local.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class Local {
102102
}
103103

104104
/**
105-
* Connect to the server.
105+
* Connect to the master.
106106
*/
107107
connect() {
108108
this.master.connect(this.gameID, this.playerID, (type, ...args) => {
@@ -116,6 +116,11 @@ export class Local {
116116
this.master.onSync(this.gameID, this.playerID, this.numPlayers);
117117
}
118118

119+
/**
120+
* Disconnect from the master.
121+
*/
122+
disconnect() {}
123+
119124
/**
120125
* Subscribe to connection state changes.
121126
*/

src/client/transport/local.test.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ describe('LocalMaster', () => {
2727
storeB.dispatch = jest.fn();
2828
});
2929

30-
test('connect', async () => {
31-
await localA.connect();
32-
await localB.connect();
30+
test('connect', () => {
31+
localA.connect();
32+
localB.connect();
3333
localA.subscribe();
3434

3535
expect(storeA.dispatch).toBeCalledWith(
@@ -44,8 +44,8 @@ describe('LocalMaster', () => {
4444
);
4545
});
4646

47-
test('update', async () => {
48-
await localA.onAction({ _stateID: 0 }, gameEvent('endTurn'));
47+
test('update', () => {
48+
localA.onAction({ _stateID: 0 }, gameEvent('endTurn'));
4949

5050
expect(storeA.dispatch).toBeCalledWith(
5151
expect.objectContaining({
@@ -59,9 +59,14 @@ describe('LocalMaster', () => {
5959
);
6060
});
6161

62-
test('connect without callback', async () => {
62+
test('connect without callback', () => {
6363
master.connect('gameID', '0', undefined);
64-
await master.onSync('gameID', '0');
64+
master.onSync('gameID', '0');
65+
});
66+
67+
test('disconnect', () => {
68+
localA.disconnect();
69+
localB.disconnect();
6570
});
6671
});
6772

@@ -71,14 +76,14 @@ describe('Local', () => {
7176
const store = { dispatch: () => {} };
7277
const m = new Local({ master, store });
7378

74-
test('gameID', async () => {
75-
await m.updateGameID('test');
79+
test('gameID', () => {
80+
m.updateGameID('test');
7681
expect(m.gameID).toBe('default:test');
7782
expect(master.onSync).lastCalledWith('default:test', null, 2);
7883
});
7984

80-
test('playerID', async () => {
81-
await m.updatePlayerID('player');
85+
test('playerID', () => {
86+
m.updatePlayerID('player');
8287
expect(m.playerID).toBe('player');
8388
expect(master.onSync).lastCalledWith('default:test', 'player', 2);
8489
});

src/client/transport/socketio.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ export class SocketIO {
119119
});
120120
}
121121

122+
/**
123+
* Disconnect from the server.
124+
*/
125+
disconnect() {
126+
this.socket.close();
127+
this.socket = null;
128+
this.isConnected = false;
129+
this.callback();
130+
}
131+
122132
/**
123133
* Subscribe to connection state changes.
124134
*/

src/client/transport/socketio.test.js

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class MockSocket {
2626
on(type, callback) {
2727
this.callbacks[type] = callback;
2828
}
29+
30+
close() {}
2931
}
3032

3133
test('defaults', () => {
@@ -54,27 +56,43 @@ describe('update gameID / playerID', () => {
5456
});
5557
});
5658

57-
test('connection status', () => {
58-
const onChangeMock = jest.fn();
59-
const mockSocket = new MockSocket();
60-
const m = new SocketIO({
61-
socket: mockSocket,
62-
gameID: 0,
63-
playerID: 0,
64-
gameName: 'foo',
65-
numPlayers: 2,
66-
});
67-
m.subscribe(onChangeMock);
68-
m.connect();
59+
describe('connection status', () => {
60+
let onChangeMock;
61+
let mockSocket;
62+
let m;
6963

70-
mockSocket.callbacks['connect']();
71-
expect(onChangeMock).toHaveBeenCalled();
72-
expect(m.isConnected).toBe(true);
64+
beforeEach(() => {
65+
onChangeMock = jest.fn();
66+
mockSocket = new MockSocket();
67+
m = new SocketIO({
68+
socket: mockSocket,
69+
gameID: 0,
70+
playerID: 0,
71+
gameName: 'foo',
72+
numPlayers: 2,
73+
});
74+
m.subscribe(onChangeMock);
75+
m.connect();
76+
});
7377

74-
onChangeMock.mockClear();
75-
mockSocket.callbacks['disconnect']();
76-
expect(onChangeMock).toHaveBeenCalled();
77-
expect(m.isConnected).toBe(false);
78+
test('connect', () => {
79+
mockSocket.callbacks['connect']();
80+
expect(onChangeMock).toHaveBeenCalled();
81+
expect(m.isConnected).toBe(true);
82+
});
83+
84+
test('disconnect', () => {
85+
mockSocket.callbacks['disconnect']();
86+
expect(onChangeMock).toHaveBeenCalled();
87+
expect(m.isConnected).toBe(false);
88+
});
89+
90+
test('close socket', () => {
91+
mockSocket.callbacks['connect']();
92+
expect(m.isConnected).toBe(true);
93+
m.disconnect();
94+
expect(m.isConnected).toBe(false);
95+
});
7896
});
7997

8098
describe('multiplayer', () => {

0 commit comments

Comments
 (0)