From 6c0511b06fdc359ec4c48879f803c80d7fbeb34c Mon Sep 17 00:00:00 2001 From: Jason Date: Sat, 18 Jun 2016 14:11:42 +0800 Subject: [web] add app ducks --- web/src/js/ducks/app.js | 27 +++++++++++++++++++++++++++ web/src/js/ducks/websocket.js | 19 +++++++++++-------- 2 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 web/src/js/ducks/app.js (limited to 'web/src/js/ducks') diff --git a/web/src/js/ducks/app.js b/web/src/js/ducks/app.js new file mode 100644 index 00000000..f1dcb490 --- /dev/null +++ b/web/src/js/ducks/app.js @@ -0,0 +1,27 @@ +import { connect as wsConnect, disconnect as wsDisconnect } from './websocket' + +export const INIT = 'APP_INIT' + +const defaultState = {} + +export function reduce(state = defaultState, action) { + switch (action.type) { + + default: + return state + } +} + +export function init() { + return dispatch => { + dispatch(wsConnect()) + dispatch({ type: INIT }) + } +} + +export function destruct() { + return dispatch => { + dispatch(wsDisconnect()) + dispatch({ type: DESTRUCT }) + } +} diff --git a/web/src/js/ducks/websocket.js b/web/src/js/ducks/websocket.js index 268c9e65..0d0556ad 100644 --- a/web/src/js/ducks/websocket.js +++ b/web/src/js/ducks/websocket.js @@ -3,17 +3,17 @@ import { AppDispatcher } from '../dispatcher.js' import * as eventLogActions from './eventLog' import * as flowActions from './flows' -const CONNECT = 'WEBSOCKET_CONNECT' -const CONNECTED = 'WEBSOCKET_CONNECTED' -const DISCONNECT = 'WEBSOCKET_DISCONNECT' -const DISCONNECTED = 'WEBSOCKET_DISCONNECTED' -const ERROR = 'WEBSOCKET_ERROR' -const MESSAGE = 'WEBSOCKET_MESSAGE' +export const CONNECT = 'WEBSOCKET_CONNECT' +export const CONNECTED = 'WEBSOCKET_CONNECTED' +export const DISCONNECT = 'WEBSOCKET_DISCONNECT' +export const DISCONNECTED = 'WEBSOCKET_DISCONNECTED' +export const ERROR = 'WEBSOCKET_ERROR' +export const MESSAGE = 'WEBSOCKET_MESSAGE' /* we may want to have an error message attribute here at some point */ const defaultState = { connected: false, socket: null } -export default function reducer(state = defaultState, action) { +export default function reduce(state = defaultState, action) { switch (action.type) { case CONNECT: @@ -52,7 +52,10 @@ export function connect() { } export function disconnect() { - return { type: DISCONNECT } + return (dispatch, getState) => { + getState().settings.socket.close() + dispatch({ type: DISCONNECT }) + } } export function onConnect() { -- cgit v1.2.3