summaryrefslogtreecommitdiff
path: root/frontend/src/api
diff options
context:
space:
mode:
authorJoffrey Bion <joffrey.bion@amadeus.com>2017-07-21 10:00:07 +0200
committerJoffrey Bion <joffrey.bion@amadeus.com>2017-07-21 16:29:16 +0200
commit859bafb1e4223216d8d818fb5566fd42aaf80705 (patch)
tree25994f3165019e5703c9647b3c435f86eb056cbb /frontend/src/api
parentRename actions in games.js (GAME -> GAMES) (diff)
downloadseven-wonders-859bafb1e4223216d8d818fb5566fd42aaf80705.tar.gz
seven-wonders-859bafb1e4223216d8d818fb5566fd42aaf80705.tar.bz2
seven-wonders-859bafb1e4223216d8d818fb5566fd42aaf80705.zip
Isolate Seven Wonders API from sagas
Diffstat (limited to 'frontend/src/api')
-rw-r--r--frontend/src/api/sevenWondersApi.js66
-rw-r--r--frontend/src/api/websocket.js32
2 files changed, 98 insertions, 0 deletions
diff --git a/frontend/src/api/sevenWondersApi.js b/frontend/src/api/sevenWondersApi.js
new file mode 100644
index 00000000..4ba0fff3
--- /dev/null
+++ b/frontend/src/api/sevenWondersApi.js
@@ -0,0 +1,66 @@
+import { createJsonSubscriptionChannel, createStompSession } from './websocket';
+import type { Client } from 'webstomp-client';
+import type { Channel } from 'redux-saga';
+
+const wsURL = '/seven-wonders-websocket';
+
+export class SevenWondersSession {
+ client: Client;
+
+ constructor(client: Client) {
+ this.client = client;
+ }
+
+ watchErrors(): Channel<ApiError> {
+ return createJsonSubscriptionChannel(this.client, '/user/queue/errors');
+ }
+
+ chooseName(displayName: string): void {
+ this.client.send('/app/chooseName', JSON.stringify({ playerName: displayName }));
+ }
+
+ watchNameChoice(): Channel<Object> {
+ return createJsonSubscriptionChannel(this.client, '/user/queue/nameChoice');
+ }
+
+ watchGames(): Channel<Object> {
+ return createJsonSubscriptionChannel(this.client, '/topic/games');
+ }
+
+ watchLobbyJoined(): Channel<Object> {
+ return createJsonSubscriptionChannel(this.client, '/user/queue/lobby/joined');
+ }
+
+ watchLobbyUpdated(currentGameId: number): Channel<Object> {
+ return createJsonSubscriptionChannel(this.client, `/topic/lobby/${currentGameId}/updated`);
+ }
+
+ watchGameStarted(currentGameId: number): Channel<Object> {
+ return createJsonSubscriptionChannel(this.client, `/topic/lobby/${currentGameId}/started`);
+ }
+
+ createGame(gameName: string): void {
+ this.client.send('/app/lobby/create', JSON.stringify({ gameName }));
+ }
+
+ joinGame(gameId: number): void {
+ this.client.send('/app/lobby/join', JSON.stringify({ gameId }));
+ }
+
+ startGame(): void {
+ this.client.send('/app/lobby/startGame', {});
+ }
+}
+
+export function createSession(): Promise<SevenWondersSession> {
+ return createStompSession(wsURL).then(client => new SevenWondersSession(client));
+}
+
+export class ApiError {
+ message: string;
+ details: ApiErrorDetail[];
+}
+
+export class ApiErrorDetail {
+ message: string;
+}
diff --git a/frontend/src/api/websocket.js b/frontend/src/api/websocket.js
new file mode 100644
index 00000000..6dc6e1a0
--- /dev/null
+++ b/frontend/src/api/websocket.js
@@ -0,0 +1,32 @@
+// @flow
+import SockJS from 'sockjs-client';
+import Stomp from 'webstomp-client';
+import type { Client, Frame, Subscription } from 'webstomp-client';
+
+import { eventChannel } from 'redux-saga';
+import type { Channel } from 'redux-saga';
+
+function createStompClient(url: string): Client {
+ return Stomp.over(new SockJS(url), {
+ debug: process.env.NODE_ENV !== 'production',
+ });
+}
+
+export function createStompSession(url: string, headers: Object = {}): Promise<Client> {
+ return new Promise((resolve, reject) => {
+ const client: Client = createStompClient(url);
+ const onSuccess = (frame: Frame) => resolve(client);
+ client.connect(headers, onSuccess, reject);
+ });
+}
+
+export function createJsonSubscriptionChannel(client: Client, path: string): Channel {
+ return eventChannel((emitter: (data: any) => void) => {
+ const socketSubscription: Subscription = client.subscribe(path, (frame: Frame) => {
+ // not all frames have a JSON body
+ const value = frame && frame.body && JSON.parse(frame.body);
+ emitter(value);
+ });
+ return () => socketSubscription.unsubscribe();
+ });
+}
bgstack15