-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbsockMiddleware.js
More file actions
128 lines (109 loc) · 3.87 KB
/
bsockMiddleware.js
File metadata and controls
128 lines (109 loc) · 3.87 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* eslint-disable consistent-return */
// With help from the following:
// https://exec64.co.uk/blog/websockets_with_redux/
// https://github.com/quirinpa/redux-socket
require('babel-polyfill');
const bsock = require('bsock');
const assert = require('bsert');
module.exports = function bsockMiddleware (options) {
let socket = null;
const { listeners, debug, disconnectedAction } = options;
return ({ dispatch }) => next => async (action) => {
// check if action has a bsock property
if (!action.bsock)
return next(action);
switch (action.type) {
case 'CONNECT_SOCKET': {
if (debug)
console.log('Connecting bsock client');
// Start a new connection to the server
if(socket !== null) {
socket.close();
}
// if it has a `connect` action then we connect to the bcoin socket
const { port, host, ssl, protocols, namespace, apiKey } = action.bsock;
socket = bsock.connect(port, host, ssl, protocols, namespace);
socket.on('error', (err) => {
if (debug)
console.error('There was an error with bsock: ', err);
if (disconnectedAction)
return next(disconnectedAction);
dispatch({ type: 'SOCKET_ERROR', payload: err });
});
socket.on('connect', async () => {
await socket.call('auth', apiKey);
if (debug)
console.log('bsock client connected');
// setup the listeners
if (listeners && listeners.length) {
listeners.forEach((listener) => {
const { event, actionType, ack } = listener;
assert(typeof event === 'string',
'Event listener was not a string');
// actionType is required to dispatch the action when msg received
assert(actionType && (typeof actionType === 'string'),
'Need an action type to create the action');
if (ack) {
// for listeners that need to acknowledge, use `bsock.hook`
socket.hook(event, async (payload) => {
if (payload) {
dispatch({ type: actionType, payload});
}
return Buffer.from(ack);
});
} else {
if (debug)
console.log('binding event: ', event);
socket.bind(event, (...data) =>
dispatch({ type: actionType, payload: data })
);
}
});
}
dispatch({
type: 'SOCKET_CONNECTED'
});
});
break;
}
case 'DISCONNECT_SOCKET': {
if (socket !== null)
socket.close();
socket = null;
if (disconnectedAction)
return next(disconnectedAction);
dispatch({ type: 'SOCKET_DISCONNECTED' });
break;
}
case 'EMIT_SOCKET': {
if (socket === null) {
console.log('Please connect bsock before trying to call server');
return next(action);
}
const { type, message, acknowledge, ...rest } = action.bsock;
let args = [];
if (Object.keys(rest).length) {
args = Object.keys(rest).map(key => rest[key]);
}
try {
if (acknowledge) {
assert(typeof acknowledge === 'function',
'acknowledge property must be a function'
);
const ack = await socket.call(type, message, ...args);
acknowledge(ack);
} else {
// if there's no acknowledge function then just use the fire method
socket.fire(type, message, ...args);
}
} catch(error) {
if (debug)
console.error('There was a problem calling the socket:', error);
}
break;
}
default:
return next(action);
}
};
};