Skip to content

Commit

Permalink
change custom client transport to a constructor (#417)
Browse files Browse the repository at this point in the history
I'm implementing a custom transport. Having a parameter that just override transport is not very useful as the transport will need access to things like the store (same way that local and socketio transport do).
  • Loading branch information
amitport authored and nicolodavis committed Jun 11, 2019
1 parent 89faece commit 2b98fb6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,14 @@ class _ClientImpl {
socketOpts,
});
} else if (multiplayer.transport !== undefined) {
this.transport = multiplayer.transport;
this.transport = new multiplayer.transport({
store: this.store,
gameID: gameID,
playerID: playerID,
gameName: game.name,
numPlayers,
socketOpts,
});
} else {
error('invalid multiplayer spec');
}
Expand Down
10 changes: 7 additions & 3 deletions src/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,22 @@ describe('multiplayer', () => {
});

describe('custom transport', () => {
const transport = { custom: true };
class CustomTransport {
custom = true;
}

let client;

beforeAll(() => {
client = Client({
game: Game({ moves: { A: () => {} } }),
multiplayer: { transport },
multiplayer: { transport: CustomTransport },
});
});

test('correct transport used', () => {
expect(client.transport).toBe(transport);
expect(client.transport).toBeInstanceOf(CustomTransport);
expect(client.transport.custom).toBe(true);
});
});

Expand Down

0 comments on commit 2b98fb6

Please sign in to comment.