This repository has been archived by the owner on Jan 16, 2023. It is now read-only.
-
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.
Change structure of project; remove SocketIO and LocalDB
- Loading branch information
Showing
21 changed files
with
185 additions
and
384 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
.idea | ||
node_modules | ||
lib | ||
/lib | ||
build | ||
coverage | ||
/index.js | ||
/client.js | ||
/server.js | ||
/shared.js |
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 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,7 @@ | ||
import createReduxLiveMiddleware from './lib/client/createReduxLiveMiddleware' | ||
import reduxLiveReducer from './lib/client/reduxLiveReducer' | ||
|
||
export { | ||
createReduxLiveMiddleware, | ||
reduxLiveReducer | ||
}; |
This file was deleted.
Oops, something went wrong.
52 changes: 0 additions & 52 deletions
52
src/client/server-communicator/SocketIoServerCommunicator.js
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,13 @@ | ||
import createReduxLiveMiddleware from './lib/client/createReduxLiveMiddleware' | ||
import reduxLiveReducer from './lib/client/reduxLiveReducer' | ||
import ReduxLiveServer from './lib/server/ReduxLiveServer' | ||
import {SET_STREAM_INITIAL_STATE, CONFIRM_ACTION, UNSUBSCRIBE_TO_STREAM, SUBSCRIBE_TO_STREAM} from './lib/shared/constants/ActionTypes' | ||
|
||
const ReduxLiveActionTypes = {SET_STREAM_INITIAL_STATE, CONFIRM_ACTION, UNSUBSCRIBE_TO_STREAM, SUBSCRIBE_TO_STREAM}; | ||
|
||
export { | ||
createReduxLiveMiddleware, | ||
reduxLiveReducer, | ||
ReduxLiveServer, | ||
ReduxLiveActionTypes | ||
}; |
2 changes: 1 addition & 1 deletion
2
src/client/createReduxLiveMiddleware.js → src/lib/client/createReduxLiveMiddleware.js
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
File renamed without changes.
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,114 @@ | ||
import {createStore} from 'redux' | ||
import _ from 'lodash' | ||
|
||
import {defaultMergeActions} from '../shared/defaults' | ||
import {defaultIsActionValid} from './defaults' | ||
import {SET_STREAM_INITIAL_STATE} from '../shared/constants/ActionTypes' | ||
|
||
class ReduxLiveServer { | ||
|
||
constructor({getReducer, reducer, mergeActions=defaultMergeActions, isActionValid=defaultIsActionValid, db, clientCommunicator}) { | ||
this.getReducer = getReducer ? getReducer : () => reducer; | ||
this.mergeActions = mergeActions; | ||
this.isActionValid = isActionValid; | ||
this.db = db; | ||
this.clientCommunicator = clientCommunicator; | ||
} | ||
|
||
async start() { | ||
try { | ||
await this.db.connect(); | ||
|
||
this.clientCommunicator.onNewAction(async action => { | ||
await this.saveAction(action) | ||
}); | ||
|
||
this.db.onNewAction(action => { | ||
try { | ||
this.clientCommunicator.sendAction(action) | ||
} catch (err) { | ||
console.error('Failed to send action to client', err) | ||
} | ||
}); | ||
|
||
this.clientCommunicator.onNewSubscription(async (clientId, streamId) => { | ||
try { | ||
const currentSnapshot = await this.db.getSnapshot(streamId); | ||
const action = { | ||
type: SET_STREAM_INITIAL_STATE, | ||
state: _.omit(currentSnapshot, 'reduxLive'), | ||
reduxLive: { | ||
streamId: streamId, | ||
sequenceNumber: currentSnapshot.reduxLive.sequenceNumber | ||
} | ||
}; | ||
|
||
this.clientCommunicator.sendActionToClient(clientId, action) | ||
} catch (err) { | ||
console.log('Failed to send initial state for stream to client', err) | ||
} | ||
}); | ||
console.log('Started Redux Live server') | ||
} catch (err) { | ||
console.error('Failed to start Redux Live server', err); | ||
} | ||
} | ||
|
||
async saveAction(action) { | ||
try { | ||
if (!this.isActionValid(action)) { | ||
console.error('Received invalid action: %j', action); | ||
return; | ||
} | ||
|
||
const streamId = action.reduxLive.streamId; | ||
const previousSnapshot = await this.db.getSnapshot(streamId); | ||
const lastSequenceNumber = previousSnapshot.reduxLive.sequenceNumber; | ||
const nextSequenceNumber = lastSequenceNumber + 1; | ||
|
||
const sequenceNumber = action.reduxLive.sequenceNumber; | ||
const invalidSequenceNumber = sequenceNumber <= 0 || sequenceNumber > nextSequenceNumber; | ||
if (invalidSequenceNumber) { | ||
console.error('Action has invalid sequence number %j', action); | ||
return; | ||
} | ||
|
||
const serverActions = await Promise.all(_.range(sequenceNumber, nextSequenceNumber) | ||
.map((sequenceNumber) => this.db.getAction(streamId, sequenceNumber))); | ||
|
||
const transformedAction = serverActions | ||
.reduce((transformedAction, serverAction) => { | ||
const mergedActions = this.mergeActions(transformedAction, serverAction); | ||
return mergedActions[1]; | ||
}, action); | ||
|
||
const actionToSave = { | ||
...transformedAction, | ||
reduxLive: { | ||
...action.reduxLive, | ||
sequenceNumber: nextSequenceNumber, | ||
streamId: streamId, | ||
timestamp: Date.now() | ||
} | ||
}; | ||
|
||
const store = createStore(this.getReducer(streamId), previousSnapshot); | ||
store.dispatch(actionToSave); | ||
|
||
await this.db.saveSnapshot({ | ||
...store.getState(), | ||
reduxLive: { | ||
sequenceNumber: nextSequenceNumber, | ||
streamId: streamId | ||
} | ||
}); | ||
await this.db.saveAction(actionToSave); | ||
} catch (err) { | ||
console.error('Failed to save action %j', action, err) | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
export default ReduxLiveServer |
File renamed without changes.
File renamed without changes.
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 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,5 @@ | ||
import ReduxLiveServer from './lib/server/ReduxLiveServer' | ||
|
||
export { | ||
ReduxLiveServer | ||
}; |
72 changes: 0 additions & 72 deletions
72
src/server/client-communicator/SocketIoClientCommunicator.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.