Skip to content

Commit

Permalink
various updates
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbdk committed Jul 1, 2019
1 parent 42745c9 commit d64a7f5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
agent: require('./src/agent'),
table: require('./src/table')
createTable: require('./src/table')
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
8 changes: 8 additions & 0 deletions src/connection.reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 5 additions & 7 deletions src/public.reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [
Expand All @@ -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 {
Expand All @@ -31,8 +30,7 @@ module.exports = function(state, action) {
...action.data
}
}
break;
}

return state;
default:
return state;
}
}
29 changes: 18 additions & 11 deletions src/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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 };
}
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
Expand All @@ -39,7 +39,7 @@ describe(`Test table`, () => {
});

afterAll(() => {
listenRef.close()
table.kill();
});

it('Agents can connect', done => {
Expand Down

0 comments on commit d64a7f5

Please sign in to comment.