From 43685fd7ff83ed03342c273851e69228adde8dc7 Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Mon, 24 Jul 2017 19:40:23 +0200 Subject: Decouple Seven Wonders API from saga channels API --- frontend/src/api/websocket.js | 57 ++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 20 deletions(-) (limited to 'frontend/src/api/websocket.js') diff --git a/frontend/src/api/websocket.js b/frontend/src/api/websocket.js index 6dc6e1a0..d411587a 100644 --- a/frontend/src/api/websocket.js +++ b/frontend/src/api/websocket.js @@ -1,32 +1,49 @@ // @flow import SockJS from 'sockjs-client'; import Stomp from 'webstomp-client'; -import type { Client, Frame, Subscription } from 'webstomp-client'; +import type { Client, Frame, Options, Subscription } from 'webstomp-client'; -import { eventChannel } from 'redux-saga'; -import type { Channel } from 'redux-saga'; +const DEFAULT_DEBUG_OPTIONS = { + debug: process.env.NODE_ENV !== 'production', +}; -function createStompClient(url: string): Client { - return Stomp.over(new SockJS(url), { - debug: process.env.NODE_ENV !== 'production', - }); -} +export type Callback = (value: T) => void; -export function createStompSession(url: string, headers: Object = {}): Promise { - return new Promise((resolve, reject) => { - const client: Client = createStompClient(url); - const onSuccess = (frame: Frame) => resolve(client); - client.connect(headers, onSuccess, reject); - }); -} +export type Callable = () => void; + +export class JsonStompClient { + client: Client; + + constructor(client: Client) { + this.client = client; + } + + connect(headers: Object = {}): Promise { + return new Promise((resolve, reject) => { + this.client.connect(headers, resolve, reject); + }); + } -export function createJsonSubscriptionChannel(client: Client, path: string): Channel { - return eventChannel((emitter: (data: any) => void) => { - const socketSubscription: Subscription = client.subscribe(path, (frame: Frame) => { + subscribe(path: string, callback: Callback): Callable { + const socketSubscription: Subscription = this.client.subscribe(path, (frame: Frame) => { // not all frames have a JSON body const value = frame && frame.body && JSON.parse(frame.body); - emitter(value); + callback(value); }); return () => socketSubscription.unsubscribe(); - }); + } + + send(url: string, body: Object) { + const strBody = body ? JSON.stringify(body) : ''; + this.client.send(url, strBody); + } +} + +function createStompClient(url: string, options: Options = {}): Client { + const optionsWithDebug = Object.assign({}, DEFAULT_DEBUG_OPTIONS, options); + return Stomp.over(new SockJS(url), optionsWithDebug); +} + +export function createJsonStompClient(url: string, options: Options = {}): JsonStompClient { + return new JsonStompClient(createStompClient(url, options)); } -- cgit