-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
30 lines (27 loc) · 878 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const Client = require('./client');
const Server = require('./server');
const PORT = process.env.REDUX_IPC_PORT || 8080;
const PROTOCOL = process.env.REDUX_IPC_PROTOCOL || 'redux-ipc';
module.exports = ({ getState }) => {
return next => action => {
const nextState = next(action);
const state = getState();
const socket = new Client(`ws://localhost:${PORT}`, PROTOCOL);
const data = { action, state };
// sends the data to the server
if (typeof WebSocket !== 'undefined' && socket instanceof WebSocket) {
socket.onopen = () => {
socket.send(JSON.stringify(data));
};
} else {
socket.on('open', () => {
socket.send(JSON.stringify(data));
});
}
return nextState;
};
};
module.exports.port = PORT;
module.exports.protocol = PROTOCOL;
module.exports.Client = Client;
module.exports.Server = Server;