From d64a7f5ecacb5b3f50b8bc0adc5b982417062bb2 Mon Sep 17 00:00:00 2001 From: Kasper Sebb' brandt Date: Mon, 1 Jul 2019 22:42:16 +0200 Subject: [PATCH] various updates --- index.js | 2 +- package.json | 2 +- src/connection.reducer.js | 8 ++++++++ src/public.reducer.js | 12 +++++------- src/table.js | 29 ++++++++++++++++++----------- test.js | 4 ++-- 6 files changed, 35 insertions(+), 22 deletions(-) diff --git a/index.js b/index.js index d2b1059..27f5e27 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ module.exports = { agent: require('./src/agent'), - table: require('./src/table') + createTable: require('./src/table') } \ No newline at end of file diff --git a/package.json b/package.json index e8b4915..c856996 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@vargr/vargr-table", - "version": "0.1.1", + "version": "0.1.3", "description": "WIP, currently this does not work.", "main": "index.js", "scripts": { diff --git a/src/connection.reducer.js b/src/connection.reducer.js index 609b78b..1ff7eac 100644 --- a/src/connection.reducer.js +++ b/src/connection.reducer.js @@ -6,6 +6,14 @@ module.exports = function(state, action) { sockets: [...state.sockets, action.data.socket.uuid] } } + case 'close': { + action.data.socket.terminate(); + + return { + ...state, + sockets: state.sockets.filter(a => a !== action.data.socket.uuid) + } + } } return state; diff --git a/src/public.reducer.js b/src/public.reducer.js index 650ceb4..1d1acdf 100644 --- a/src/public.reducer.js +++ b/src/public.reducer.js @@ -5,7 +5,7 @@ module.exports = function(state, action) { const clientState = { ...state.clientState } - + Object.keys(action.data).forEach(k => { if (clientState[k] && Array.isArray(clientState[k])) { clientState[k] = [ @@ -16,12 +16,11 @@ module.exports = function(state, action) { clientState[k] = action.data[k]; } }); - + return { ...state, clientState } - break; case 'public:set': case 'public:set:all': return { @@ -31,8 +30,7 @@ module.exports = function(state, action) { ...action.data } } - break; - } - - return state; + default: + return state; + } } \ No newline at end of file diff --git a/src/table.js b/src/table.js index 1c0f713..d78b7c0 100644 --- a/src/table.js +++ b/src/table.js @@ -7,26 +7,27 @@ const connectionReducer = require('./connection.reducer'); const publicReducer = require('./public.reducer'); const { publicEffect, timesyncEffect } = require('./effects'); -module.exports = function ({ initialState = {}, customReducer = () => {}, effects = [], pingInterval = 30000 }) { +module.exports = function ({ initialState = {}, customReducer = a => a, effects = [], pingInterval = 30000 }) { const router = new Router(); const app = websockify(new Koa()); + let serverInstance; effects.push(publicEffect); effects.push(timesyncEffect); - let currentAppAtate = { + let currentAppState = { ...initialState, sockets: [] }; const dispatchedActions = []; function getState() { - return currentAppAtate; + return currentAppState; } function updateState(state) { if (getState() !== state) { - currentAppAtate = state; + currentAppState = state; } } @@ -67,29 +68,35 @@ module.exports = function ({ initialState = {}, customReducer = () => {}, effect }); ctx.websocket.on('close', () => { - dispatch({ type: 'close' }, ctx.websocket); + return dispatch({ type: 'close', data:{ socket: ctx.websocket } }, ctx.websocket);; }); }); const interval = setInterval(function ping() { - // @TODO, update the connection reducers - if (!app.ws.server) { return; } app.ws.server.clients.forEach(function each(ws) { if (ws.isAlive === false) { - console.log('terminate!') - return ws.terminate(); + return dispatch({ type: 'close', data:{ socket: ctx.websocket } }, ctx.websocket);; } ws.isAlive = false; ws.ping(); }); - }, pingInterval); + }, pingInterval); + + function kill() { + serverInstance.close(); + clearInterval(interval); + } + + function listen(port) { + serverInstance = app.listen(port); + } app.ws.use(router.routes()); - return { ws: app.ws, server: app, dispatch, getState }; + return { ws: app.ws, server: app, dispatch, getState, kill, listen }; } \ No newline at end of file diff --git a/test.js b/test.js index 4371a1f..3765e09 100644 --- a/test.js +++ b/test.js @@ -27,7 +27,7 @@ describe(`Test table`, () => { beforeAll(async () => { table = createTable({ initialState, customReducer: appReducer, effects, pingInterval: 500 }); - listenRef = table.server.listen(8500); + listenRef = table.listen(8500); agent01 = new Agent("ws://localhost:8500", ["protocolOne", "protocolTwo"]); agent02 = new Agent("ws://localhost:8500", ["protocolOne", "protocolTwo"]); @@ -39,7 +39,7 @@ describe(`Test table`, () => { }); afterAll(() => { - listenRef.close() + table.kill(); }); it('Agents can connect', done => {