Skip to content

Commit 94472c0

Browse files
kuket15delucis
andauthored
feat(react-native): handle multiplayer loading state in client (#1026)
Co-authored-by: delucis <[email protected]>
1 parent 525c8f6 commit 94472c0

4 files changed

Lines changed: 47 additions & 1 deletion

File tree

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ Pete Nykänen
1717
Philihp Busby
1818
Jason Harrison
1919
Brendon Roberto
20+
Luca Vallisa

src/client/react-native.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ import { Client as RawClient } from './client';
2929
*/
3030
export function Client(opts) {
3131
const { game, numPlayers, board, multiplayer, enhancer } = opts;
32+
let { loading } = opts;
33+
34+
// Component that is displayed before the client has synced
35+
// with the game master.
36+
if (loading === undefined) {
37+
const Loading = () => <></>;
38+
loading = Loading;
39+
}
3240

3341
/*
3442
* WrappedBoard
@@ -96,6 +104,11 @@ export function Client(opts) {
96104
let _board = null;
97105

98106
const state = this.client.getState();
107+
108+
if (state === null) {
109+
return React.createElement(loading);
110+
}
111+
99112
const { matchID, playerID, ...rest } = this.props;
100113

101114
if (board) {

src/client/react-native.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ import { Client } from './react-native';
1212
import Enzyme from 'enzyme';
1313
import Adapter from 'enzyme-adapter-react-16';
1414
import { Local } from './transport/local';
15+
import { Transport } from './transport/transport';
16+
17+
class NoConnectionTransport extends Transport {
18+
connect() {}
19+
disconnect() {}
20+
sendAction() {}
21+
sendChatMessage() {}
22+
requestSync() {}
23+
updateMatchID() {}
24+
updatePlayerID() {}
25+
updateCredentials() {}
26+
}
1527

1628
Enzyme.configure({ adapter: new Adapter() });
1729

@@ -36,6 +48,25 @@ test('board is rendered', () => {
3648
game.unmount();
3749
});
3850

51+
test('board is rendered with custom loading', () => {
52+
const Loading = () => <>connecting...</>;
53+
54+
const Board = Client({
55+
game: {},
56+
board: TestBoard,
57+
loading: Loading,
58+
multiplayer: (opts) => new NoConnectionTransport(opts),
59+
});
60+
61+
const game = Enzyme.mount(<Board />);
62+
const loadingComponent = game.find(Loading);
63+
64+
expect(loadingComponent.props()).toEqual({});
65+
expect(loadingComponent.text()).toBe('connecting...');
66+
67+
game.unmount();
68+
});
69+
3970
test('board props', () => {
4071
const Board = Client({
4172
game: {},

src/client/react.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ export function Client<
7373
P extends BoardProps<G> = BoardProps<G>,
7474
ContextWithPlugins extends Ctx = Ctx
7575
>(opts: ReactClientOpts<G, P, ContextWithPlugins>) {
76-
let { game, numPlayers, loading, board, multiplayer, enhancer, debug } = opts;
76+
const { game, numPlayers, board, multiplayer, enhancer } = opts;
77+
let { loading, debug } = opts;
7778

7879
// Component that is displayed before the client has synced
7980
// with the game master.

0 commit comments

Comments
 (0)