-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
229 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
})); | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} |
Oops, something went wrong.