Skip to content

Commit

Permalink
Refactor to use Redux library
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbdk committed Aug 17, 2019
1 parent f9b6401 commit 8d7614b
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 222 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"koa": "^2.7.0",
"koa-router": "^7.4.0",
"koa-websocket": "^5.0.1",
"redux": "^4.0.4",
"redux-thunk": "^2.3.0",
"uuid": "^3.3.2"
},
"devDependencies": {
Expand Down
59 changes: 0 additions & 59 deletions src/actions.js

This file was deleted.

20 changes: 0 additions & 20 deletions src/connection.reducer.js

This file was deleted.

39 changes: 0 additions & 39 deletions src/effects.js

This file was deleted.

38 changes: 0 additions & 38 deletions src/public.reducer.js

This file was deleted.

30 changes: 30 additions & 0 deletions src/store/connection.reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const connectionActions = {
OPEN: 'connection:open',
CLOSE: 'connection:close'
}

function connection(state = { sockets: [] }, action) {
switch(action.type) {
case connectionActions.OPEN: {
return {
...state,
sockets: [...state.sockets, action.socket.uuid]
}
}
case connectionActions.CLOSE: {
action.data.socket.terminate();

return {
...state,
sockets: state.sockets.filter(a => a !== action.data.socket.uuid)
}
}
}

return state;
}

module.exports = {
connectionActions,
connection
}
46 changes: 46 additions & 0 deletions src/store/effects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const { performance } = require('perf_hooks');
const { tableActions } = require('./table.reducer');
const { connectionActions } = require('./connection.reducer');

const timesyncActions = {
GET_TIME: 'get:time'
};

module.exports = {
timesyncActions,
tableEffect: ({ action, dispatch, getState, ws}) => {
switch(action.type) {
case connectionActions.OPEN:
case tableActions.CURRENT:
return action.socket.send(JSON.stringify({
type: tableActions.SET,
data: getState().clientState,
time: performance.now()
}));
case tableActions.ADD:
case tableActions.SET:
return ws.server.clients.forEach(function each(client) {
if(client !== action.socket) {
client.send(JSON.stringify({ ...action, time: performance.now() }));
}
});
case tableActions.ADD_ALL:
case tableActions.SET_ALL:
return ws.server.clients.forEach(function each(client) {
client.send(JSON.stringify({ ...action, time: performance.now() }));
});
}
},
timesyncEffect: ({ action, dispatch, getState, ws}) => {
switch(action.type) {
case timesyncActions.GET_TIME: {
action.socket.send(JSON.stringify({
type: timesyncActions.GET_TIME,
data: {
time: performance.now()
}
}));
}
}
}
};
9 changes: 9 additions & 0 deletions src/store/effects.middlware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function createEffectsMiddleware(effects = [], ws) {
return ({ dispatch, getState }) => next => action => {
const n = next(action);
effects.forEach(e => e({action, dispatch, getState, ws}));
return n;
};
}

module.exports = createEffectsMiddleware;
46 changes: 46 additions & 0 deletions src/store/table.reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const tableActions = {
ADD: 'public:add',
ADD_ALL: 'public:add:all',
CURRENT: 'public:current',
SET: 'public:set',
SET_ALL: 'public:set:all',
}

function createTableReducer(prefix = '') {
return function(state = {}, action) {
switch(action.type) {
case prefix+tableActions.ADD:
case prefix+tableActions.ADD_ALL:
const newState = { ...state }

Object.keys(action.data).forEach(k => {
if (newState[k] && Array.isArray(newState[k])) {
newState[k] = [
...newState[k],
...action.data[k]
];
} else {
newState[k] = typeof(newState[k]) === 'object'
? { ...newState[k], ...action.data[k] }
: action.data[k];
}
});

return { ...state, newState }
case prefix+tableActions.SET:
case prefix+tableActions.SET_ALL:
return {
...state,
...action.data
}
default:
return state;
}
}
}

module.exports = {
table: createTableReducer(),
createTableReducer,
tableActions
}
26 changes: 26 additions & 0 deletions src/store/table.store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { createStore, applyMiddleware, combineReducers } = require('redux');
const thunk = require('redux-thunk');

const effectsMiddlware = require('./effects.middlware');

const { tableEffect, timesyncEffect } = require('./effects');
const { connection } = require('./connection.reducer');
const { table } = require('./table.reducer');

module.exports = function({ customReducers = {}, initialState = {}, effects = [], ws }) {
let currentAppState = { ...initialState };

const allEffects = [
...effects,
tableEffect,
timesyncEffect
]

const rootReducer = combineReducers({
connection,
table,
...customReducers
});

return createStore(rootReducer, currentAppState, applyMiddleware(thunk.default, effectsMiddlware(allEffects, ws)));
}
Loading

0 comments on commit 8d7614b

Please sign in to comment.