Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
Change structure of project; remove SocketIO and LocalDB
Browse files Browse the repository at this point in the history
  • Loading branch information
eitak committed Jun 5, 2016
1 parent 898a17f commit acd9d2d
Show file tree
Hide file tree
Showing 21 changed files with 185 additions and 384 deletions.
6 changes: 5 additions & 1 deletion .gitignore
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
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
},
"main": "lib/index.js",
"scripts": {
"clean": "rm -rf lib coverage",
"clean": "rm -rf lib coverage index.js client.js server.js shared.js",
"test": "mocha --compilers js:babel-register --reporter nyan --recursive",
"test:coverage": "babel-node node_modules/isparta/bin/isparta cover --report html --include-all-sources node_modules/mocha/bin/_mocha -- --reporter dot --recursive",
"compile": "babel -d lib/ src/",
"prepublish": "npm run clean && npm run compile"
"build": "babel -d ./ src/",
"prepublish": "npm run clean && npm run build"
},
"author": "eitak",
"license": "ISC",
Expand All @@ -35,13 +35,11 @@
"babel-register": "^6.9.0",
"isparta": "^4.0.0",
"mocha": "^2.5.3",
"redux-live-localdb": "0.0.1",
"should": "^8.4.0"
},
"dependencies": {
"lodash": "^4.13.1",
"object-hash": "^1.1.2",
"redux": "^3.5.2",
"socket.io": "^1.4.5",
"socket.io-client": "^1.4.5"
"redux": "^3.5.2"
}
}
7 changes: 7 additions & 0 deletions src/client.js
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
};
7 changes: 0 additions & 7 deletions src/client/index.js

This file was deleted.

52 changes: 0 additions & 52 deletions src/client/server-communicator/SocketIoServerCommunicator.js

This file was deleted.

13 changes: 13 additions & 0 deletions src/index.js
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
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from 'lodash'

import {SET_STREAM_INITIAL_STATE, CONFIRM_ACTION, UNSUBSCRIBE_TO_STREAM, SUBSCRIBE_TO_STREAM} from '../shared/constants/ActionTypes'
import {defaultMergeActions} from '../shared/Defaults'
import {defaultMergeActions} from '../shared/defaults'

const streamActionTypes = [UNSUBSCRIBE_TO_STREAM, SUBSCRIBE_TO_STREAM, SET_STREAM_INITIAL_STATE];

Expand Down
File renamed without changes.
114 changes: 114 additions & 0 deletions src/lib/server/ReduxLiveServer.js
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.
1 change: 0 additions & 1 deletion src/shared/defaults.js → src/lib/shared/defaults.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export const defaultMergeActions = (action1, action2) => [action2, action1];
export const defaultGetSocketIoRoom = streamId => streamId;

export default {
mergeActions: defaultMergeActions
Expand Down
5 changes: 5 additions & 0 deletions src/server.js
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 src/server/client-communicator/SocketIoClientCommunicator.js

This file was deleted.

Loading

0 comments on commit acd9d2d

Please sign in to comment.