diff options
author | Victor Chabbert <chabbertvi@eisti.eu> | 2017-05-30 22:12:53 +0200 |
---|---|---|
committer | Victor Chabbert <chabbertvi@eisti.eu> | 2017-05-30 22:12:53 +0200 |
commit | d537784fa1a7780338197a6d1fb1e6187a6df1ec (patch) | |
tree | 3aad952889cf51614f601b89b073aa1c391392cb /frontend/src/utils/websocket.js | |
parent | Change type imports (diff) | |
download | seven-wonders-d537784fa1a7780338197a6d1fb1e6187a6df1ec.tar.gz seven-wonders-d537784fa1a7780338197a6d1fb1e6187a6df1ec.tar.bz2 seven-wonders-d537784fa1a7780338197a6d1fb1e6187a6df1ec.zip |
Add types to game files
Diffstat (limited to 'frontend/src/utils/websocket.js')
-rw-r--r-- | frontend/src/utils/websocket.js | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/frontend/src/utils/websocket.js b/frontend/src/utils/websocket.js index 2751b16c..b01531cf 100644 --- a/frontend/src/utils/websocket.js +++ b/frontend/src/utils/websocket.js @@ -5,17 +5,42 @@ import { eventChannel } from 'redux-saga'; const wsURL = '/seven-wonders-websocket'; -export const createWsConnection = (headers: Object = {}) => +export type FrameType = { + body: string, + command: string, + header: { + 'heart-beat': number, + 'user-name': string, + version: string + } +}; +export type SocketType = { + connect: (headers: Object, onSucces: (frame: FrameType) => void, onReject: (error: any) => void) => void, + subscribe: (path: string, callback: (event: any) => void) => Object +}; +export type SocketSubscriptionType = { + id: string, + unsubscribe: () => void +}; +export type SocketEventType = { + body: string +}; +export type SocketObjectType = { + frame: FrameType, + socket: SocketType +}; + +export const createWsConnection = (headers: Object = {}): Promise<SocketObjectType> => new Promise((resolve, reject) => { - let socket = Stomp.over(new SockJS(wsURL), { + let socket: SocketType = Stomp.over(new SockJS(wsURL), { debug: process.env.NODE_ENV !== 'production', }); - socket.connect(headers, frame => resolve({ frame, socket }), reject); + socket.connect(headers, (frame: FrameType) => resolve({ frame, socket }), reject); }); -export const createSubscriptionChannel = (socket: any, path: string) => { - return eventChannel(emitter => { - const socketSubscription = socket.subscribe(path, event => { +export const createSubscriptionChannel = (socket: SocketType, path: string) => { + return eventChannel((emitter: (data: any) => void) => { + const socketSubscription: SocketSubscriptionType = socket.subscribe(path, (event: SocketEventType) => { emitter(JSON.parse(event.body)); }); return () => socketSubscription.unsubscribe(); |