-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (35 loc) · 1.23 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const createPhoenix = require('phoenix');
export const CONNECTED = '@@phoenix/CONNECTED';
export const DISCONNECTED = '@@phoenix/DISCONNECTED';
export const MESSAGE = '@@phoenix/MESSAGE';
export const SEND = '@@phoenix/SEND';
export const DISCONNECT = '@@phoenix/DISCONNECT';
export default function createPhoenixMiddleware(options, client = WebSocket) {
let phoenix = null;
return function ({ dispatch }) {
if (!phoenix) {
phoenix = createPhoenix(client, options);
phoenix
.on('connected', () => {
dispatch({ type: CONNECTED });
})
.on('disconnected', () => {
dispatch({ type: DISCONNECTED });
})
.on('message', (message) => {
dispatch({ type: MESSAGE, payload: message });
});
}
return function (next) {
return function (action) {
if (action.type === SEND) {
phoenix.send(action.payload);
}
if (action.type === DISCONNECT) {
phoenix.destroy();
}
return next(action);
};
};
};
}