@folklore/socket 0.4.9 → 0.4.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs.js +621 -824
- package/dist/es.js +621 -814
- package/package.json +6 -6
package/dist/es.js
CHANGED
|
@@ -1,825 +1,658 @@
|
|
|
1
|
-
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
|
|
2
|
-
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
|
|
3
|
-
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
4
|
-
import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
|
|
5
|
-
import _createClass from '@babel/runtime/helpers/createClass';
|
|
6
|
-
import _assertThisInitialized from '@babel/runtime/helpers/assertThisInitialized';
|
|
7
|
-
import _inherits from '@babel/runtime/helpers/inherits';
|
|
8
|
-
import _possibleConstructorReturn from '@babel/runtime/helpers/possibleConstructorReturn';
|
|
9
|
-
import _getPrototypeOf from '@babel/runtime/helpers/getPrototypeOf';
|
|
10
1
|
import createDebug from 'debug';
|
|
11
2
|
import EventEmitter from 'wolfy87-eventemitter';
|
|
12
3
|
import invariant from 'invariant';
|
|
13
4
|
import isFunction from 'lodash/isFunction';
|
|
14
5
|
import isArray from 'lodash/isArray';
|
|
15
6
|
import React, { useMemo, useState, useRef, useCallback, useEffect, useContext } from 'react';
|
|
16
|
-
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
17
7
|
import PropTypes from 'prop-types';
|
|
18
8
|
import { jsx } from 'react/jsx-runtime';
|
|
19
9
|
import isString from 'lodash/isString';
|
|
20
10
|
|
|
21
|
-
|
|
11
|
+
const debug$2 = createDebug('folklore:socket:pubnub');
|
|
22
12
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
function _isNativeReflectConstruct$2() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
28
|
-
var debug$2 = createDebug('folklore:socket:pubnub');
|
|
29
|
-
|
|
30
|
-
var PubNubSocket = /*#__PURE__*/function (_EventEmitter) {
|
|
31
|
-
_inherits(PubNubSocket, _EventEmitter);
|
|
32
|
-
|
|
33
|
-
var _super = _createSuper$2(PubNubSocket);
|
|
34
|
-
|
|
35
|
-
function PubNubSocket(opts) {
|
|
36
|
-
var _this;
|
|
37
|
-
|
|
38
|
-
_classCallCheck(this, PubNubSocket);
|
|
39
|
-
|
|
40
|
-
_this = _super.call(this);
|
|
41
|
-
_this.options = _objectSpread$4({
|
|
13
|
+
class PubNubSocket extends EventEmitter {
|
|
14
|
+
constructor(opts) {
|
|
15
|
+
super();
|
|
16
|
+
this.options = {
|
|
42
17
|
uuid: null,
|
|
43
18
|
publishKey: null,
|
|
44
19
|
subscribeKey: null,
|
|
45
|
-
secretKey: null
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return _this;
|
|
20
|
+
secretKey: null,
|
|
21
|
+
...opts
|
|
22
|
+
};
|
|
23
|
+
this.onReady = this.onReady.bind(this);
|
|
24
|
+
this.onStatus = this.onStatus.bind(this);
|
|
25
|
+
this.onMessage = this.onMessage.bind(this);
|
|
26
|
+
this.ready = false;
|
|
27
|
+
this.shouldStart = false;
|
|
28
|
+
this.started = false;
|
|
29
|
+
this.starting = false;
|
|
30
|
+
this.PubNub = null;
|
|
31
|
+
this.pubnub = null;
|
|
32
|
+
this.pubnubListener = null;
|
|
33
|
+
this.channels = [];
|
|
34
|
+
this.init();
|
|
62
35
|
}
|
|
63
36
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
this.started = true;
|
|
75
|
-
this.starting = false;
|
|
76
|
-
this.emit('started');
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}, {
|
|
80
|
-
key: "onMessage",
|
|
81
|
-
value: function onMessage(_ref) {
|
|
82
|
-
var message = _ref.message;
|
|
83
|
-
this.emit('message', message);
|
|
84
|
-
|
|
85
|
-
if (typeof message.event !== 'undefined') {
|
|
86
|
-
this.emit(message.event, message.data || message);
|
|
87
|
-
}
|
|
37
|
+
onReady() {
|
|
38
|
+
this.ready = true;
|
|
39
|
+
this.emit('ready');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
onStatus(statusEvent) {
|
|
43
|
+
if (statusEvent.category === 'PNConnectedCategory' && !this.started) {
|
|
44
|
+
this.started = true;
|
|
45
|
+
this.starting = false;
|
|
46
|
+
this.emit('started');
|
|
88
47
|
}
|
|
89
|
-
}
|
|
90
|
-
key: "updateChannels",
|
|
91
|
-
value: function updateChannels(channels) {
|
|
92
|
-
debug$2("Updating channels: ".concat(channels.join(', ')));
|
|
93
|
-
var shouldStart = this.shouldStart,
|
|
94
|
-
started = this.started,
|
|
95
|
-
starting = this.starting;
|
|
96
|
-
|
|
97
|
-
if (started || starting) {
|
|
98
|
-
this.stop();
|
|
99
|
-
}
|
|
48
|
+
}
|
|
100
49
|
|
|
101
|
-
|
|
50
|
+
onMessage(_ref) {
|
|
51
|
+
let {
|
|
52
|
+
message
|
|
53
|
+
} = _ref;
|
|
54
|
+
this.emit('message', message);
|
|
102
55
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
this.start();
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}, {
|
|
109
|
-
key: "init",
|
|
110
|
-
value: function init() {
|
|
111
|
-
var _this2 = this;
|
|
112
|
-
|
|
113
|
-
import('pubnub').then(function (_ref2) {
|
|
114
|
-
var PubNub = _ref2.default;
|
|
115
|
-
_this2.PubNub = PubNub;
|
|
116
|
-
}).then(function () {
|
|
117
|
-
return _this2.createPubNub();
|
|
118
|
-
}).then(function () {
|
|
119
|
-
return _this2.onReady();
|
|
120
|
-
});
|
|
56
|
+
if (typeof message.event !== 'undefined') {
|
|
57
|
+
this.emit(message.event, message.data || message);
|
|
121
58
|
}
|
|
122
|
-
}
|
|
123
|
-
key: "createPubNub",
|
|
124
|
-
value: function createPubNub() {
|
|
125
|
-
var PubNub = this.PubNub;
|
|
126
|
-
var pubnubOptions = {
|
|
127
|
-
publishKey: this.options.publishKey,
|
|
128
|
-
subscribeKey: this.options.subscribeKey
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
if (this.options.uuid !== null) {
|
|
132
|
-
pubnubOptions.uuid = this.options.uuid;
|
|
133
|
-
}
|
|
59
|
+
}
|
|
134
60
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
61
|
+
updateChannels(channels) {
|
|
62
|
+
debug$2("Updating channels: ".concat(channels.join(', ')));
|
|
63
|
+
const {
|
|
64
|
+
shouldStart,
|
|
65
|
+
started,
|
|
66
|
+
starting
|
|
67
|
+
} = this;
|
|
138
68
|
|
|
139
|
-
|
|
140
|
-
this.pubnubListener = {
|
|
141
|
-
status: this.onStatus,
|
|
142
|
-
message: this.onMessage
|
|
143
|
-
};
|
|
144
|
-
this.pubnub.addListener(this.pubnubListener);
|
|
145
|
-
}
|
|
146
|
-
}, {
|
|
147
|
-
key: "destroy",
|
|
148
|
-
value: function destroy() {
|
|
69
|
+
if (started || starting) {
|
|
149
70
|
this.stop();
|
|
71
|
+
}
|
|
150
72
|
|
|
151
|
-
|
|
152
|
-
this.pubnub.removeListener(this.pubnubListener);
|
|
153
|
-
this.pubnubListener = null;
|
|
154
|
-
}
|
|
73
|
+
this.channels = channels;
|
|
155
74
|
|
|
156
|
-
|
|
157
|
-
|
|
75
|
+
if (started || starting || shouldStart) {
|
|
76
|
+
this.shouldStart = false;
|
|
77
|
+
this.start();
|
|
158
78
|
}
|
|
159
|
-
}
|
|
160
|
-
key: "start",
|
|
161
|
-
value: function start() {
|
|
162
|
-
if (this.started) {
|
|
163
|
-
debug$2('Skipping start: Already started.');
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
79
|
+
}
|
|
166
80
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
81
|
+
init() {
|
|
82
|
+
import('pubnub').then(_ref2 => {
|
|
83
|
+
let {
|
|
84
|
+
default: PubNub
|
|
85
|
+
} = _ref2;
|
|
86
|
+
this.PubNub = PubNub;
|
|
87
|
+
}).then(() => this.createPubNub()).then(() => this.onReady());
|
|
88
|
+
}
|
|
171
89
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
90
|
+
createPubNub() {
|
|
91
|
+
const {
|
|
92
|
+
PubNub
|
|
93
|
+
} = this;
|
|
94
|
+
const pubnubOptions = {
|
|
95
|
+
publishKey: this.options.publishKey,
|
|
96
|
+
subscribeKey: this.options.subscribeKey
|
|
97
|
+
};
|
|
177
98
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
this.pubnub.subscribe({
|
|
181
|
-
channels: this.channels
|
|
182
|
-
});
|
|
183
|
-
this.emit('start');
|
|
99
|
+
if (this.options.uuid !== null) {
|
|
100
|
+
pubnubOptions.uuid = this.options.uuid;
|
|
184
101
|
}
|
|
185
|
-
}, {
|
|
186
|
-
key: "stop",
|
|
187
|
-
value: function stop() {
|
|
188
|
-
if (!this.started && !this.starting) {
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
102
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
this.started = false;
|
|
195
|
-
this.starting = false;
|
|
196
|
-
this.pubnub.unsubscribe({
|
|
197
|
-
channels: this.channels
|
|
198
|
-
});
|
|
199
|
-
this.emit('stop');
|
|
103
|
+
if (this.options.secretKey !== null) {
|
|
104
|
+
pubnubOptions.secretKey = this.options.secretKey;
|
|
200
105
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
});
|
|
219
|
-
});
|
|
106
|
+
|
|
107
|
+
this.pubnub = new PubNub(pubnubOptions);
|
|
108
|
+
this.pubnubListener = {
|
|
109
|
+
status: this.onStatus,
|
|
110
|
+
message: this.onMessage
|
|
111
|
+
};
|
|
112
|
+
this.pubnub.addListener(this.pubnubListener);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
destroy() {
|
|
116
|
+
this.stop();
|
|
117
|
+
|
|
118
|
+
if (this.pubnubListener) {
|
|
119
|
+
this.pubnub.removeListener(this.pubnubListener);
|
|
120
|
+
this.pubnubListener = null;
|
|
220
121
|
}
|
|
221
|
-
}]);
|
|
222
122
|
|
|
223
|
-
|
|
224
|
-
|
|
123
|
+
this.ready = false;
|
|
124
|
+
debug$2('Destroyed.');
|
|
125
|
+
}
|
|
225
126
|
|
|
226
|
-
|
|
127
|
+
start() {
|
|
128
|
+
if (this.started) {
|
|
129
|
+
debug$2('Skipping start: Already started.');
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
227
132
|
|
|
228
|
-
|
|
133
|
+
if (this.starting) {
|
|
134
|
+
debug$2('Skipping start: Already starting.');
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
229
137
|
|
|
230
|
-
|
|
138
|
+
if (this.channels.length === 0) {
|
|
139
|
+
debug$2('Skipping start: No channels.');
|
|
140
|
+
this.shouldStart = true;
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
231
143
|
|
|
232
|
-
|
|
144
|
+
this.shouldStart = false;
|
|
145
|
+
this.starting = true;
|
|
146
|
+
this.pubnub.subscribe({
|
|
147
|
+
channels: this.channels
|
|
148
|
+
});
|
|
149
|
+
this.emit('start');
|
|
150
|
+
}
|
|
233
151
|
|
|
234
|
-
|
|
235
|
-
|
|
152
|
+
stop() {
|
|
153
|
+
if (!this.started && !this.starting) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
236
156
|
|
|
237
|
-
|
|
238
|
-
|
|
157
|
+
debug$2('Stopping...');
|
|
158
|
+
this.shouldStart = false;
|
|
159
|
+
this.started = false;
|
|
160
|
+
this.starting = false;
|
|
161
|
+
this.pubnub.unsubscribe({
|
|
162
|
+
channels: this.channels
|
|
163
|
+
});
|
|
164
|
+
this.emit('stop');
|
|
165
|
+
}
|
|
239
166
|
|
|
240
|
-
|
|
167
|
+
send(data) {
|
|
168
|
+
debug$2('Sending', data);
|
|
169
|
+
return new Promise((resolve, reject) => {
|
|
170
|
+
this.pubnub.publish(data, (status, response) => {
|
|
171
|
+
if (status.error) {
|
|
172
|
+
reject(new Error("Error operation:".concat(status.operation, " status:").concat(status.statusCode)));
|
|
173
|
+
} else {
|
|
174
|
+
resolve({
|
|
175
|
+
status,
|
|
176
|
+
response,
|
|
177
|
+
data
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
}
|
|
241
183
|
|
|
242
|
-
|
|
243
|
-
var _this;
|
|
184
|
+
}
|
|
244
185
|
|
|
245
|
-
|
|
186
|
+
const debug$1 = createDebug('folklore:socket:socketio');
|
|
246
187
|
|
|
247
|
-
|
|
248
|
-
|
|
188
|
+
class SocketIOSocket extends EventEmitter {
|
|
189
|
+
constructor(opts) {
|
|
190
|
+
super();
|
|
191
|
+
this.options = {
|
|
249
192
|
uuid: null,
|
|
250
193
|
host: 'http://127.0.0.1',
|
|
251
194
|
path: null,
|
|
252
|
-
query: null
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
return _this;
|
|
195
|
+
query: null,
|
|
196
|
+
...opts
|
|
197
|
+
};
|
|
198
|
+
this.onReady = this.onReady.bind(this);
|
|
199
|
+
this.onConnect = this.onConnect.bind(this);
|
|
200
|
+
this.onMessage = this.onMessage.bind(this);
|
|
201
|
+
this.ready = false;
|
|
202
|
+
this.shouldStart = false;
|
|
203
|
+
this.started = false;
|
|
204
|
+
this.starting = false;
|
|
205
|
+
this.Manager = null;
|
|
206
|
+
this.io = null;
|
|
207
|
+
this.sockets = {};
|
|
208
|
+
this.channels = [];
|
|
209
|
+
this.init();
|
|
269
210
|
}
|
|
270
211
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
this.started = true;
|
|
284
|
-
this.starting = false;
|
|
285
|
-
this.emit('started');
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}, {
|
|
289
|
-
key: "onMessage",
|
|
290
|
-
value: function onMessage(message, channel) {
|
|
291
|
-
debug$1('Message received on %s %o', channel, message);
|
|
292
|
-
this.emit('message', message, channel);
|
|
293
|
-
}
|
|
294
|
-
}, {
|
|
295
|
-
key: "init",
|
|
296
|
-
value: function init() {
|
|
297
|
-
var _this2 = this;
|
|
298
|
-
|
|
299
|
-
import('socket.io-client').then(function (_ref) {
|
|
300
|
-
var IO = _ref.default;
|
|
301
|
-
_this2.Manager = IO.Manager;
|
|
302
|
-
}).then(function () {
|
|
303
|
-
return _this2.createManager();
|
|
304
|
-
}).then(function () {
|
|
305
|
-
return _this2.onReady();
|
|
306
|
-
}).then(function () {
|
|
307
|
-
if (_this2.shouldStart) {
|
|
308
|
-
_this2.start();
|
|
309
|
-
}
|
|
310
|
-
});
|
|
311
|
-
}
|
|
312
|
-
}, {
|
|
313
|
-
key: "createManager",
|
|
314
|
-
value: function createManager() {
|
|
315
|
-
var Manager = this.Manager;
|
|
316
|
-
|
|
317
|
-
var _this$options = this.options,
|
|
318
|
-
host = _this$options.host,
|
|
319
|
-
opts = _objectWithoutProperties(_this$options, _excluded$1);
|
|
320
|
-
|
|
321
|
-
this.io = new Manager(host, _objectSpread$3({
|
|
322
|
-
autoConnect: false
|
|
323
|
-
}, opts));
|
|
212
|
+
onReady() {
|
|
213
|
+
this.ready = true;
|
|
214
|
+
this.emit('ready');
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
onConnect(channel) {
|
|
218
|
+
debug$1('Socket connected on %s', channel);
|
|
219
|
+
|
|
220
|
+
if (!this.started) {
|
|
221
|
+
this.started = true;
|
|
222
|
+
this.starting = false;
|
|
223
|
+
this.emit('started');
|
|
324
224
|
}
|
|
325
|
-
}
|
|
326
|
-
key: "updateChannels",
|
|
327
|
-
value: function updateChannels(channels) {
|
|
328
|
-
debug$1("Updating channels: ".concat(channels.join(', ')));
|
|
329
|
-
var shouldStart = this.shouldStart,
|
|
330
|
-
started = this.started,
|
|
331
|
-
starting = this.starting;
|
|
332
|
-
|
|
333
|
-
if (started || starting) {
|
|
334
|
-
this.stop();
|
|
335
|
-
}
|
|
225
|
+
}
|
|
336
226
|
|
|
337
|
-
|
|
227
|
+
onMessage(message, channel) {
|
|
228
|
+
debug$1('Message received on %s %o', channel, message);
|
|
229
|
+
this.emit('message', message, channel);
|
|
230
|
+
}
|
|
338
231
|
|
|
339
|
-
|
|
232
|
+
init() {
|
|
233
|
+
import('socket.io-client').then(_ref => {
|
|
234
|
+
let {
|
|
235
|
+
default: IO
|
|
236
|
+
} = _ref;
|
|
237
|
+
this.Manager = IO.Manager;
|
|
238
|
+
}).then(() => this.createManager()).then(() => this.onReady()).then(() => {
|
|
239
|
+
if (this.shouldStart) {
|
|
340
240
|
this.start();
|
|
341
241
|
}
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
key: "start",
|
|
345
|
-
value: function start() {
|
|
346
|
-
var _this3 = this;
|
|
347
|
-
|
|
348
|
-
if (this.started) {
|
|
349
|
-
debug$1('Skipping start: Already started.');
|
|
350
|
-
return;
|
|
351
|
-
}
|
|
242
|
+
});
|
|
243
|
+
}
|
|
352
244
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
245
|
+
createManager() {
|
|
246
|
+
const {
|
|
247
|
+
Manager
|
|
248
|
+
} = this;
|
|
249
|
+
const {
|
|
250
|
+
host,
|
|
251
|
+
...opts
|
|
252
|
+
} = this.options;
|
|
253
|
+
this.io = new Manager(host, {
|
|
254
|
+
autoConnect: false,
|
|
255
|
+
...opts
|
|
256
|
+
});
|
|
257
|
+
}
|
|
357
258
|
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
259
|
+
updateChannels(channels) {
|
|
260
|
+
debug$1("Updating channels: ".concat(channels.join(', ')));
|
|
261
|
+
const {
|
|
262
|
+
shouldStart,
|
|
263
|
+
started,
|
|
264
|
+
starting
|
|
265
|
+
} = this;
|
|
363
266
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
return;
|
|
368
|
-
}
|
|
267
|
+
if (started || starting) {
|
|
268
|
+
this.stop();
|
|
269
|
+
}
|
|
369
270
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
}, {});
|
|
375
|
-
this.emit('start');
|
|
271
|
+
this.channels = channels;
|
|
272
|
+
|
|
273
|
+
if (started || starting || shouldStart) {
|
|
274
|
+
this.start();
|
|
376
275
|
}
|
|
377
|
-
}
|
|
378
|
-
key: "stop",
|
|
379
|
-
value: function stop() {
|
|
380
|
-
var _this4 = this;
|
|
276
|
+
}
|
|
381
277
|
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
278
|
+
start() {
|
|
279
|
+
if (this.started) {
|
|
280
|
+
debug$1('Skipping start: Already started.');
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
385
283
|
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
this.starting = false;
|
|
390
|
-
Object.values(this.sockets).forEach(function (socket) {
|
|
391
|
-
return _this4.stopSocket(socket);
|
|
392
|
-
});
|
|
393
|
-
this.emit('stop');
|
|
284
|
+
if (this.starting) {
|
|
285
|
+
debug$1('Skipping start: Already starting.');
|
|
286
|
+
return;
|
|
394
287
|
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
var socket = this.io.socket("/".concat(channel.replace(/^\//, '')));
|
|
401
|
-
socket.on('message', function (message) {
|
|
402
|
-
return _this5.onMessage(message, channel);
|
|
403
|
-
});
|
|
404
|
-
socket.on('connect', function () {
|
|
405
|
-
return _this5.onConnect(channel);
|
|
406
|
-
});
|
|
407
|
-
socket.open();
|
|
408
|
-
return socket;
|
|
409
|
-
} // eslint-disable-next-line class-methods-use-this
|
|
410
|
-
|
|
411
|
-
}, {
|
|
412
|
-
key: "stopSocket",
|
|
413
|
-
value: function stopSocket(socket) {
|
|
414
|
-
socket.off('connect');
|
|
415
|
-
socket.off('message');
|
|
416
|
-
socket.close();
|
|
417
|
-
return socket;
|
|
288
|
+
|
|
289
|
+
if (this.io === null) {
|
|
290
|
+
debug$1('Socket.io not ready.');
|
|
291
|
+
this.shouldStart = true;
|
|
292
|
+
return;
|
|
418
293
|
}
|
|
419
|
-
}, {
|
|
420
|
-
key: "destroy",
|
|
421
|
-
value: function destroy() {
|
|
422
|
-
this.stop();
|
|
423
|
-
this.sockets = {};
|
|
424
294
|
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
295
|
+
if (this.channels.length === 0) {
|
|
296
|
+
debug$1('Skipping start: No channels.');
|
|
297
|
+
this.shouldStart = true;
|
|
298
|
+
return;
|
|
429
299
|
}
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
return
|
|
300
|
+
|
|
301
|
+
this.shouldStart = false;
|
|
302
|
+
this.starting = true;
|
|
303
|
+
this.sockets = this.channels.reduce((map, channel) => ({ ...map,
|
|
304
|
+
[channel]: this.createSocket(channel)
|
|
305
|
+
}), {});
|
|
306
|
+
this.emit('start');
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
stop() {
|
|
310
|
+
if (!this.started && !this.starting) {
|
|
311
|
+
return;
|
|
442
312
|
}
|
|
443
|
-
}]);
|
|
444
313
|
|
|
445
|
-
|
|
446
|
-
|
|
314
|
+
debug$1('Stopping...');
|
|
315
|
+
this.shouldStart = false;
|
|
316
|
+
this.started = false;
|
|
317
|
+
this.starting = false;
|
|
318
|
+
Object.values(this.sockets).forEach(socket => this.stopSocket(socket));
|
|
319
|
+
this.emit('stop');
|
|
320
|
+
}
|
|
447
321
|
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
322
|
+
createSocket(channel) {
|
|
323
|
+
const socket = this.io.socket("/".concat(channel.replace(/^\//, '')));
|
|
324
|
+
socket.on('message', message => this.onMessage(message, channel));
|
|
325
|
+
socket.on('connect', () => this.onConnect(channel));
|
|
326
|
+
socket.open();
|
|
327
|
+
return socket;
|
|
328
|
+
} // eslint-disable-next-line class-methods-use-this
|
|
452
329
|
|
|
453
|
-
var _excluded = ["adapter", "channels"];
|
|
454
330
|
|
|
455
|
-
|
|
331
|
+
stopSocket(socket) {
|
|
332
|
+
socket.off('connect');
|
|
333
|
+
socket.off('message');
|
|
334
|
+
socket.close();
|
|
335
|
+
return socket;
|
|
336
|
+
}
|
|
456
337
|
|
|
457
|
-
|
|
338
|
+
destroy() {
|
|
339
|
+
this.stop();
|
|
340
|
+
this.sockets = {};
|
|
458
341
|
|
|
459
|
-
|
|
342
|
+
if (this.io !== null) {
|
|
343
|
+
this.io.close();
|
|
344
|
+
this.io = null;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
460
347
|
|
|
461
|
-
|
|
348
|
+
send(data) {
|
|
349
|
+
const {
|
|
350
|
+
channel,
|
|
351
|
+
message
|
|
352
|
+
} = data;
|
|
353
|
+
const channels = !isArray(channel) ? [channel] : channel;
|
|
354
|
+
channels.forEach(ch => {
|
|
355
|
+
this.sockets[ch].send(message);
|
|
356
|
+
});
|
|
357
|
+
return Promise.resolve();
|
|
358
|
+
}
|
|
462
359
|
|
|
463
|
-
|
|
464
|
-
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
var SocketAdapters = {
|
|
363
|
+
PubNub: PubNubSocket,
|
|
364
|
+
SocketIO: SocketIOSocket
|
|
465
365
|
};
|
|
466
366
|
|
|
467
|
-
|
|
367
|
+
const normalize = str => str.replace(/[^a-z0-9]+/gi, '').toLowerCase();
|
|
368
|
+
|
|
369
|
+
const debug = createDebug('folklore:socket');
|
|
370
|
+
|
|
371
|
+
class Socket extends EventEmitter {
|
|
372
|
+
static getAdapters() {
|
|
373
|
+
return Socket.adapters;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
static getAdapter(adapter) {
|
|
377
|
+
// prettier-ignore
|
|
378
|
+
const adapterKey = Object.keys(Socket.adapters).find(key => normalize(key) === normalize(adapter)) || null;
|
|
468
379
|
|
|
469
|
-
|
|
470
|
-
|
|
380
|
+
if (adapterKey === null) {
|
|
381
|
+
throw new Error("Adapter ".concat(adapter, " not found"));
|
|
382
|
+
}
|
|
471
383
|
|
|
472
|
-
|
|
384
|
+
return Socket.adapters[adapterKey];
|
|
385
|
+
}
|
|
473
386
|
|
|
474
|
-
|
|
475
|
-
|
|
387
|
+
static addAdapter(name, adapter) {
|
|
388
|
+
Socket.adapters = { ...Socket.adapters,
|
|
389
|
+
[name]: adapter
|
|
390
|
+
};
|
|
391
|
+
return Socket;
|
|
392
|
+
}
|
|
476
393
|
|
|
477
|
-
|
|
394
|
+
static setAdapters(adapters) {
|
|
395
|
+
Socket.adapters = adapters;
|
|
396
|
+
return Socket;
|
|
397
|
+
}
|
|
478
398
|
|
|
479
|
-
|
|
480
|
-
|
|
399
|
+
constructor(opts) {
|
|
400
|
+
super();
|
|
401
|
+
this.options = {
|
|
481
402
|
adapter: 'pubnub',
|
|
482
403
|
namespace: null,
|
|
483
404
|
uuid: null,
|
|
484
405
|
publishKey: null,
|
|
485
406
|
subscribeKey: null,
|
|
486
407
|
secretKey: null,
|
|
487
|
-
channels: []
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
if (
|
|
504
|
-
|
|
408
|
+
channels: [],
|
|
409
|
+
...opts
|
|
410
|
+
};
|
|
411
|
+
this.onAdapterReady = this.onAdapterReady.bind(this);
|
|
412
|
+
this.onAdapterStart = this.onAdapterStart.bind(this);
|
|
413
|
+
this.onAdapterStarted = this.onAdapterStarted.bind(this);
|
|
414
|
+
this.onAdapterMessage = this.onAdapterMessage.bind(this);
|
|
415
|
+
this.onAdapterStop = this.onAdapterStop.bind(this);
|
|
416
|
+
this.shouldStart = false;
|
|
417
|
+
this.started = false;
|
|
418
|
+
this.starting = false;
|
|
419
|
+
this.ready = false;
|
|
420
|
+
this.adapter = null;
|
|
421
|
+
this.channels = [];
|
|
422
|
+
this.init();
|
|
423
|
+
|
|
424
|
+
if (this.options.channels.length) {
|
|
425
|
+
this.setChannels(this.options.channels);
|
|
505
426
|
}
|
|
506
|
-
|
|
507
|
-
return _this;
|
|
508
427
|
}
|
|
509
428
|
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
this.ready = true;
|
|
515
|
-
this.emit('ready');
|
|
429
|
+
onAdapterReady() {
|
|
430
|
+
debug('Adapter ready');
|
|
431
|
+
this.ready = true;
|
|
432
|
+
this.emit('ready');
|
|
516
433
|
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
}, {
|
|
523
|
-
key: "onAdapterStart",
|
|
524
|
-
value: function onAdapterStart() {
|
|
525
|
-
debug('Adapter starting...');
|
|
526
|
-
this.starting = true;
|
|
527
|
-
this.started = false;
|
|
528
|
-
this.emit('start');
|
|
529
|
-
}
|
|
530
|
-
}, {
|
|
531
|
-
key: "onAdapterStarted",
|
|
532
|
-
value: function onAdapterStarted() {
|
|
533
|
-
debug('Adapter started');
|
|
534
|
-
this.starting = false;
|
|
535
|
-
this.started = true;
|
|
536
|
-
this.emit('started');
|
|
537
|
-
}
|
|
538
|
-
}, {
|
|
539
|
-
key: "onAdapterStop",
|
|
540
|
-
value: function onAdapterStop() {
|
|
541
|
-
debug('Adapter stopped');
|
|
542
|
-
this.starting = false;
|
|
543
|
-
this.started = false;
|
|
544
|
-
this.emit('stop');
|
|
545
|
-
}
|
|
546
|
-
}, {
|
|
547
|
-
key: "onAdapterMessage",
|
|
548
|
-
value: function onAdapterMessage(message) {
|
|
549
|
-
debug('Adapter message', message);
|
|
550
|
-
this.emit('message', message);
|
|
434
|
+
if (this.shouldStart) {
|
|
435
|
+
this.shouldStart = false;
|
|
436
|
+
this.start();
|
|
551
437
|
}
|
|
552
|
-
}
|
|
553
|
-
key: "getChannelWithoutNamespace",
|
|
554
|
-
value: function getChannelWithoutNamespace(name) {
|
|
555
|
-
if (this.options.namespace === null) {
|
|
556
|
-
return name;
|
|
557
|
-
}
|
|
438
|
+
}
|
|
558
439
|
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
var parts = [];
|
|
440
|
+
onAdapterStart() {
|
|
441
|
+
debug('Adapter starting...');
|
|
442
|
+
this.starting = true;
|
|
443
|
+
this.started = false;
|
|
444
|
+
this.emit('start');
|
|
445
|
+
}
|
|
566
446
|
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
447
|
+
onAdapterStarted() {
|
|
448
|
+
debug('Adapter started');
|
|
449
|
+
this.starting = false;
|
|
450
|
+
this.started = true;
|
|
451
|
+
this.emit('started');
|
|
452
|
+
}
|
|
570
453
|
|
|
571
|
-
|
|
572
|
-
|
|
454
|
+
onAdapterStop() {
|
|
455
|
+
debug('Adapter stopped');
|
|
456
|
+
this.starting = false;
|
|
457
|
+
this.started = false;
|
|
458
|
+
this.emit('stop');
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
onAdapterMessage(message) {
|
|
462
|
+
debug('Adapter message', message);
|
|
463
|
+
this.emit('message', message);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
getChannelWithoutNamespace(name) {
|
|
467
|
+
if (this.options.namespace === null) {
|
|
468
|
+
return name;
|
|
573
469
|
}
|
|
574
|
-
}, {
|
|
575
|
-
key: "setChannels",
|
|
576
|
-
value: function setChannels(channels) {
|
|
577
|
-
var _this2 = this;
|
|
578
470
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
471
|
+
const regExp = new RegExp("^".concat(this.options.namespace, ":"));
|
|
472
|
+
return name.replace(regExp, '');
|
|
473
|
+
}
|
|
582
474
|
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
}
|
|
475
|
+
getChannelWithNamespace(name) {
|
|
476
|
+
const parts = [];
|
|
586
477
|
|
|
587
|
-
|
|
588
|
-
this.
|
|
478
|
+
if (this.options.namespace !== null) {
|
|
479
|
+
parts.push(this.options.namespace);
|
|
589
480
|
}
|
|
590
|
-
}, {
|
|
591
|
-
key: "addChannel",
|
|
592
|
-
value: function addChannel(channel) {
|
|
593
|
-
var namespacedChannel = this.getChannelWithNamespace(channel);
|
|
594
481
|
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
482
|
+
parts.push(name);
|
|
483
|
+
return parts.join(':');
|
|
484
|
+
}
|
|
598
485
|
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
value: function addChannels(channels) {
|
|
605
|
-
var _this3 = this;
|
|
606
|
-
|
|
607
|
-
var namespacedChannels = channels.map(function (channel) {
|
|
608
|
-
return _this3.getChannelWithNamespace(channel);
|
|
609
|
-
}).sort();
|
|
610
|
-
debug("Adding channels: ".concat(channels.join(',')));
|
|
611
|
-
this.updateChannels([].concat(_toConsumableArray(this.channels), _toConsumableArray(namespacedChannels.filter(function (it) {
|
|
612
|
-
return _this3.channels.indexOf(it) === -1;
|
|
613
|
-
}))));
|
|
486
|
+
setChannels(channels) {
|
|
487
|
+
const namespacedChannels = channels.map(channel => this.getChannelWithNamespace(channel)).sort();
|
|
488
|
+
|
|
489
|
+
if (this.channels.join(',') === namespacedChannels.join(',')) {
|
|
490
|
+
return;
|
|
614
491
|
}
|
|
615
|
-
}, {
|
|
616
|
-
key: "removeChannel",
|
|
617
|
-
value: function removeChannel(channel) {
|
|
618
|
-
var namespacedChannel = this.getChannelWithNamespace(channel);
|
|
619
492
|
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
493
|
+
debug("Set channels: ".concat(namespacedChannels.join(', ')));
|
|
494
|
+
this.updateChannels(namespacedChannels);
|
|
495
|
+
}
|
|
623
496
|
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
return ch !== namespacedChannel;
|
|
627
|
-
}));
|
|
628
|
-
}
|
|
629
|
-
}, {
|
|
630
|
-
key: "removeChannels",
|
|
631
|
-
value: function removeChannels(channels) {
|
|
632
|
-
var _this4 = this;
|
|
633
|
-
|
|
634
|
-
var namespacedChannels = channels.map(function (channel) {
|
|
635
|
-
return _this4.getChannelWithNamespace(channel);
|
|
636
|
-
}).sort();
|
|
637
|
-
debug("Removing channels: ".concat(channels.join(',')));
|
|
638
|
-
this.updateChannels(this.channels.filter(function (it) {
|
|
639
|
-
return namespacedChannels.indexOf(it) === -1;
|
|
640
|
-
}));
|
|
641
|
-
}
|
|
642
|
-
}, {
|
|
643
|
-
key: "updateChannels",
|
|
644
|
-
value: function updateChannels(channels) {
|
|
645
|
-
var sortedChannels = channels.sort();
|
|
646
|
-
debug("Updating channels: ".concat(sortedChannels.join(', ')));
|
|
647
|
-
this.channels = _toConsumableArray(sortedChannels);
|
|
648
|
-
|
|
649
|
-
if (this.adapter !== null) {
|
|
650
|
-
this.adapter.updateChannels(sortedChannels);
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
}, {
|
|
654
|
-
key: "hasChannel",
|
|
655
|
-
value: function hasChannel(channel) {
|
|
656
|
-
var namespacedChannel = this.getChannelWithNamespace(channel);
|
|
657
|
-
return this.channels.reduce(function (found, it) {
|
|
658
|
-
return found || it === namespacedChannel;
|
|
659
|
-
}, false);
|
|
660
|
-
}
|
|
661
|
-
}, {
|
|
662
|
-
key: "getChannels",
|
|
663
|
-
value: function getChannels() {
|
|
664
|
-
var _this5 = this;
|
|
497
|
+
addChannel(channel) {
|
|
498
|
+
const namespacedChannel = this.getChannelWithNamespace(channel);
|
|
665
499
|
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
});
|
|
669
|
-
}
|
|
670
|
-
}, {
|
|
671
|
-
key: "init",
|
|
672
|
-
value: function init() {
|
|
673
|
-
var _this6 = this;
|
|
674
|
-
|
|
675
|
-
var _this$options = this.options,
|
|
676
|
-
adapterKey = _this$options.adapter;
|
|
677
|
-
_this$options.channels;
|
|
678
|
-
var adapterOptions = _objectWithoutProperties(_this$options, _excluded);
|
|
679
|
-
|
|
680
|
-
var SocketAdapter = Socket.getAdapter(adapterKey);
|
|
681
|
-
this.adapter = new SocketAdapter(adapterOptions);
|
|
682
|
-
var methods = ['start', 'stop', 'destroy', 'updateChannels', 'send'];
|
|
683
|
-
methods.forEach(function (method) {
|
|
684
|
-
invariant(isFunction(_this6.adapter[method] || null), "Socket adapter should implement method ".concat(method));
|
|
685
|
-
});
|
|
686
|
-
this.adapter.on('ready', this.onAdapterReady);
|
|
687
|
-
this.adapter.on('start', this.onAdapterStart);
|
|
688
|
-
this.adapter.on('started', this.onAdapterStarted);
|
|
689
|
-
this.adapter.on('message', this.onAdapterMessage);
|
|
690
|
-
this.adapter.on('stop', this.onAdapterStop);
|
|
500
|
+
if (this.channels.indexOf(namespacedChannel) !== -1) {
|
|
501
|
+
return;
|
|
691
502
|
}
|
|
692
|
-
}, {
|
|
693
|
-
key: "destroy",
|
|
694
|
-
value: function destroy() {
|
|
695
|
-
if (this.adapter !== null) {
|
|
696
|
-
this.adapter.removeAllListeners();
|
|
697
|
-
this.adapter.destroy();
|
|
698
|
-
this.adapter = null;
|
|
699
|
-
}
|
|
700
503
|
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
504
|
+
debug("Adding channel: ".concat(channel));
|
|
505
|
+
this.updateChannels([...this.channels, namespacedChannel]);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
addChannels(channels) {
|
|
509
|
+
const namespacedChannels = channels.map(channel => this.getChannelWithNamespace(channel)).sort();
|
|
510
|
+
debug("Adding channels: ".concat(channels.join(',')));
|
|
511
|
+
this.updateChannels([...this.channels, ...namespacedChannels.filter(it => this.channels.indexOf(it) === -1)]);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
removeChannel(channel) {
|
|
515
|
+
const namespacedChannel = this.getChannelWithNamespace(channel);
|
|
516
|
+
|
|
517
|
+
if (this.channels.indexOf(namespacedChannel) === -1) {
|
|
518
|
+
return;
|
|
711
519
|
}
|
|
712
|
-
}, {
|
|
713
|
-
key: "start",
|
|
714
|
-
value: function start() {
|
|
715
|
-
var _this7 = this;
|
|
716
|
-
|
|
717
|
-
if (this.started) {
|
|
718
|
-
debug('Skipping start: Already started.');
|
|
719
|
-
return;
|
|
720
|
-
}
|
|
721
520
|
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
}
|
|
521
|
+
debug("Removing channel: ".concat(channel));
|
|
522
|
+
this.updateChannels(this.channels.filter(ch => ch !== namespacedChannel));
|
|
523
|
+
}
|
|
726
524
|
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
525
|
+
removeChannels(channels) {
|
|
526
|
+
const namespacedChannels = channels.map(channel => this.getChannelWithNamespace(channel)).sort();
|
|
527
|
+
debug("Removing channels: ".concat(channels.join(',')));
|
|
528
|
+
this.updateChannels(this.channels.filter(it => namespacedChannels.indexOf(it) === -1));
|
|
529
|
+
}
|
|
732
530
|
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
531
|
+
updateChannels(channels) {
|
|
532
|
+
const sortedChannels = channels.sort();
|
|
533
|
+
debug("Updating channels: ".concat(sortedChannels.join(', ')));
|
|
534
|
+
this.channels = [...sortedChannels];
|
|
535
|
+
|
|
536
|
+
if (this.adapter !== null) {
|
|
537
|
+
this.adapter.updateChannels(sortedChannels);
|
|
739
538
|
}
|
|
740
|
-
}
|
|
741
|
-
key: "stop",
|
|
742
|
-
value: function stop() {
|
|
743
|
-
this.shouldStart = false;
|
|
539
|
+
}
|
|
744
540
|
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
541
|
+
hasChannel(channel) {
|
|
542
|
+
const namespacedChannel = this.getChannelWithNamespace(channel);
|
|
543
|
+
return this.channels.reduce((found, it) => found || it === namespacedChannel, false);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
getChannels() {
|
|
547
|
+
return this.channels.map(channel => this.getChannelWithoutNamespace(channel));
|
|
548
|
+
}
|
|
748
549
|
|
|
749
|
-
|
|
550
|
+
init() {
|
|
551
|
+
const {
|
|
552
|
+
adapter: adapterKey,
|
|
553
|
+
channels,
|
|
554
|
+
...adapterOptions
|
|
555
|
+
} = this.options;
|
|
556
|
+
const SocketAdapter = Socket.getAdapter(adapterKey);
|
|
557
|
+
this.adapter = new SocketAdapter(adapterOptions);
|
|
558
|
+
const methods = ['start', 'stop', 'destroy', 'updateChannels', 'send'];
|
|
559
|
+
methods.forEach(method => {
|
|
560
|
+
invariant(isFunction(this.adapter[method] || null), "Socket adapter should implement method ".concat(method));
|
|
561
|
+
});
|
|
562
|
+
this.adapter.on('ready', this.onAdapterReady);
|
|
563
|
+
this.adapter.on('start', this.onAdapterStart);
|
|
564
|
+
this.adapter.on('started', this.onAdapterStarted);
|
|
565
|
+
this.adapter.on('message', this.onAdapterMessage);
|
|
566
|
+
this.adapter.on('stop', this.onAdapterStop);
|
|
567
|
+
}
|
|
750
568
|
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
569
|
+
destroy() {
|
|
570
|
+
if (this.adapter !== null) {
|
|
571
|
+
this.adapter.removeAllListeners();
|
|
572
|
+
this.adapter.destroy();
|
|
573
|
+
this.adapter = null;
|
|
754
574
|
}
|
|
755
|
-
}, {
|
|
756
|
-
key: "send",
|
|
757
|
-
value: function send(data, channel) {
|
|
758
|
-
if (!this.started) {
|
|
759
|
-
debug('Abort sending data: Not started');
|
|
760
|
-
return Promise.reject();
|
|
761
|
-
}
|
|
762
575
|
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
576
|
+
this.started = false;
|
|
577
|
+
this.starting = false;
|
|
578
|
+
this.ready = false;
|
|
579
|
+
debug('Destroyed.');
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
restart() {
|
|
583
|
+
this.stop();
|
|
584
|
+
this.start();
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
start() {
|
|
588
|
+
if (this.started) {
|
|
589
|
+
debug('Skipping start: Already started.');
|
|
590
|
+
return;
|
|
769
591
|
}
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
return
|
|
592
|
+
|
|
593
|
+
if (this.starting) {
|
|
594
|
+
debug('Skipping start: Already starting.');
|
|
595
|
+
return;
|
|
774
596
|
}
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
597
|
+
|
|
598
|
+
if (!this.ready) {
|
|
599
|
+
debug('Skipping start: No ready.');
|
|
600
|
+
this.shouldStart = true;
|
|
601
|
+
return;
|
|
779
602
|
}
|
|
780
|
-
}, {
|
|
781
|
-
key: "getAdapter",
|
|
782
|
-
value: function getAdapter(adapter) {
|
|
783
|
-
// prettier-ignore
|
|
784
|
-
var adapterKey = Object.keys(Socket.adapters).find(function (key) {
|
|
785
|
-
return normalize(key) === normalize(adapter);
|
|
786
|
-
}) || null;
|
|
787
|
-
|
|
788
|
-
if (adapterKey === null) {
|
|
789
|
-
throw new Error("Adapter ".concat(adapter, " not found"));
|
|
790
|
-
}
|
|
791
603
|
|
|
792
|
-
|
|
604
|
+
this.shouldStart = false;
|
|
605
|
+
debug('Starting on channels:');
|
|
606
|
+
this.channels.forEach(channel => {
|
|
607
|
+
debug("- ".concat(this.getChannelWithoutNamespace(channel)));
|
|
608
|
+
});
|
|
609
|
+
this.adapter.start();
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
stop() {
|
|
613
|
+
this.shouldStart = false;
|
|
614
|
+
|
|
615
|
+
if (!this.started && !this.starting) {
|
|
616
|
+
return;
|
|
793
617
|
}
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
618
|
+
|
|
619
|
+
debug('Stopping...');
|
|
620
|
+
|
|
621
|
+
if (this.adapter !== null) {
|
|
622
|
+
this.adapter.stop();
|
|
799
623
|
}
|
|
800
|
-
}
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
send(data, channel) {
|
|
627
|
+
if (!this.started) {
|
|
628
|
+
debug('Abort sending data: Not started');
|
|
629
|
+
return Promise.reject();
|
|
805
630
|
}
|
|
806
|
-
}]);
|
|
807
631
|
|
|
808
|
-
|
|
809
|
-
|
|
632
|
+
const publishData = typeof data.channel !== 'undefined' && typeof data.message !== 'undefined' ? data : {
|
|
633
|
+
channel: typeof channel !== 'undefined' ? this.getChannelWithNamespace(channel) : this.channels,
|
|
634
|
+
message: data
|
|
635
|
+
};
|
|
636
|
+
debug('Sending', publishData);
|
|
637
|
+
return this.adapter.send(publishData);
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
isStarted() {
|
|
641
|
+
return this.started;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
}
|
|
810
645
|
|
|
811
|
-
Socket.adapters =
|
|
646
|
+
Socket.adapters = { ...SocketAdapters
|
|
647
|
+
};
|
|
812
648
|
|
|
813
|
-
|
|
649
|
+
const SocketContext = /*#__PURE__*/React.createContext({
|
|
814
650
|
socket: null,
|
|
815
|
-
subscribe:
|
|
816
|
-
unsubscribe:
|
|
651
|
+
subscribe: () => {},
|
|
652
|
+
unsubscribe: () => {}
|
|
817
653
|
});
|
|
818
654
|
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
function _objectSpread$1(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys$1(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys$1(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
822
|
-
var propTypes = {
|
|
655
|
+
const propTypes = {
|
|
823
656
|
socket: PropTypes.instanceOf(Socket),
|
|
824
657
|
adapter: PropTypes.string,
|
|
825
658
|
host: PropTypes.string,
|
|
@@ -832,7 +665,7 @@ var propTypes = {
|
|
|
832
665
|
autoStart: PropTypes.bool,
|
|
833
666
|
children: PropTypes.node
|
|
834
667
|
};
|
|
835
|
-
|
|
668
|
+
const defaultProps = {
|
|
836
669
|
socket: null,
|
|
837
670
|
adapter: 'pubnub',
|
|
838
671
|
host: null,
|
|
@@ -846,82 +679,73 @@ var defaultProps = {
|
|
|
846
679
|
children: null
|
|
847
680
|
};
|
|
848
681
|
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
}, [socket, host, adapter, namespace, uuid, publishKey, subscribeKey, secretKey]);
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
channels = _useState2[0],
|
|
876
|
-
setChannels = _useState2[1];
|
|
877
|
-
|
|
878
|
-
var channelsCountRef = useRef({});
|
|
879
|
-
var updateChannels = useCallback(function (newChannels) {
|
|
682
|
+
const SocketContainer = function (_ref) {
|
|
683
|
+
let {
|
|
684
|
+
children,
|
|
685
|
+
socket,
|
|
686
|
+
autoStart,
|
|
687
|
+
adapter,
|
|
688
|
+
host,
|
|
689
|
+
namespace,
|
|
690
|
+
uuid,
|
|
691
|
+
publishKey,
|
|
692
|
+
subscribeKey,
|
|
693
|
+
secretKey,
|
|
694
|
+
channels: initialChannels
|
|
695
|
+
} = _ref;
|
|
696
|
+
const finalSocket = useMemo(() => socket || new Socket({
|
|
697
|
+
adapter,
|
|
698
|
+
host,
|
|
699
|
+
namespace,
|
|
700
|
+
uuid,
|
|
701
|
+
publishKey,
|
|
702
|
+
subscribeKey,
|
|
703
|
+
secretKey
|
|
704
|
+
}), [socket, host, adapter, namespace, uuid, publishKey, subscribeKey, secretKey]);
|
|
705
|
+
const [channels, setChannels] = useState([]);
|
|
706
|
+
const channelsCountRef = useRef({});
|
|
707
|
+
const updateChannels = useCallback(newChannels => {
|
|
880
708
|
finalSocket.setChannels(newChannels);
|
|
881
709
|
setChannels(newChannels);
|
|
882
710
|
}, [finalSocket, setChannels]);
|
|
883
|
-
|
|
884
|
-
channelsCountRef.current = newChannels.reduce(
|
|
885
|
-
|
|
886
|
-
}, channelsCountRef.current);
|
|
711
|
+
const addToChannelsCount = useCallback(newChannels => {
|
|
712
|
+
channelsCountRef.current = newChannels.reduce((map, channel) => ({ ...map,
|
|
713
|
+
[channel]: (map[channel] || 0) + 1
|
|
714
|
+
}), channelsCountRef.current);
|
|
887
715
|
updateChannels(Object.keys(channelsCountRef.current));
|
|
888
716
|
}, [updateChannels]);
|
|
889
|
-
|
|
890
|
-
channelsCountRef.current = newChannels.reduce(
|
|
891
|
-
|
|
892
|
-
return newCount > 0 ?
|
|
717
|
+
const removeToChannelsCount = useCallback(newChannels => {
|
|
718
|
+
channelsCountRef.current = newChannels.reduce((map, channel) => {
|
|
719
|
+
const newCount = (map[channel] || 0) - 1;
|
|
720
|
+
return newCount > 0 ? { ...map,
|
|
721
|
+
[channel]: newCount
|
|
722
|
+
} : map;
|
|
893
723
|
}, channelsCountRef.current);
|
|
894
724
|
updateChannels(Object.keys(channelsCountRef.current));
|
|
895
725
|
}, [updateChannels]);
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
var unsubscribe = useCallback(function (channelsToRemove) {
|
|
900
|
-
return removeToChannelsCount(channelsToRemove);
|
|
901
|
-
}, [removeToChannelsCount]);
|
|
902
|
-
useEffect(function () {
|
|
726
|
+
const subscribe = useCallback(channelsToAdd => addToChannelsCount(channelsToAdd), [addToChannelsCount]);
|
|
727
|
+
const unsubscribe = useCallback(channelsToRemove => removeToChannelsCount(channelsToRemove), [removeToChannelsCount]);
|
|
728
|
+
useEffect(() => {
|
|
903
729
|
subscribe(initialChannels);
|
|
904
|
-
return
|
|
730
|
+
return () => {
|
|
905
731
|
unsubscribe(initialChannels);
|
|
906
732
|
};
|
|
907
733
|
}, [initialChannels, subscribe, unsubscribe]);
|
|
908
|
-
useEffect(
|
|
734
|
+
useEffect(() => {
|
|
909
735
|
if (autoStart) {
|
|
910
736
|
finalSocket.start();
|
|
911
737
|
}
|
|
912
738
|
|
|
913
|
-
return
|
|
739
|
+
return () => {
|
|
914
740
|
finalSocket.destroy();
|
|
915
741
|
};
|
|
916
742
|
}, [autoStart, finalSocket]);
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
};
|
|
924
|
-
}, [finalSocket, subscribe]);
|
|
743
|
+
const value = useMemo(() => ({
|
|
744
|
+
socket: finalSocket,
|
|
745
|
+
subscribe,
|
|
746
|
+
unsubscribe,
|
|
747
|
+
channels
|
|
748
|
+
}), [finalSocket, subscribe]);
|
|
925
749
|
return /*#__PURE__*/jsx(SocketContext.Provider, {
|
|
926
750
|
value: value,
|
|
927
751
|
children: children
|
|
@@ -931,75 +755,58 @@ var SocketContainer = function SocketContainer(_ref) {
|
|
|
931
755
|
SocketContainer.propTypes = propTypes;
|
|
932
756
|
SocketContainer.defaultProps = defaultProps;
|
|
933
757
|
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
socketUnsubscribe: unsubscribe
|
|
953
|
-
}, props));
|
|
954
|
-
}
|
|
955
|
-
});
|
|
956
|
-
};
|
|
758
|
+
const getDisplayName = WrappedComponent => WrappedComponent.displayName || WrappedComponent.name || 'Component';
|
|
759
|
+
|
|
760
|
+
const withSocket = WrappedComponent => {
|
|
761
|
+
const WithSocketComponent = props => /*#__PURE__*/jsx(SocketContext.Consumer, {
|
|
762
|
+
children: _ref => {
|
|
763
|
+
let {
|
|
764
|
+
socket,
|
|
765
|
+
subscribe,
|
|
766
|
+
unsubscribe
|
|
767
|
+
} = _ref;
|
|
768
|
+
return /*#__PURE__*/jsx(WrappedComponent, {
|
|
769
|
+
socket: socket,
|
|
770
|
+
socketSubscribe: subscribe,
|
|
771
|
+
socketUnsubscribe: unsubscribe,
|
|
772
|
+
...props
|
|
773
|
+
});
|
|
774
|
+
}
|
|
775
|
+
});
|
|
957
776
|
|
|
958
777
|
WithSocketComponent.displayName = "WithSocket(".concat(getDisplayName(WrappedComponent), ")");
|
|
959
778
|
return WithSocketComponent;
|
|
960
779
|
};
|
|
961
780
|
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
var _useState = useState(socket !== null ? socket.isStarted() : false),
|
|
979
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
980
|
-
started = _useState2[0],
|
|
981
|
-
setStarted = _useState2[1];
|
|
982
|
-
|
|
983
|
-
var channels = isString(channelNames) ? [channelNames] : channelNames;
|
|
984
|
-
var channelsKey = (channels || []).sort().join(',');
|
|
985
|
-
useEffect(function () {
|
|
781
|
+
const useSocket = function () {
|
|
782
|
+
let channelNames = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
783
|
+
let {
|
|
784
|
+
socket: customSocket = null,
|
|
785
|
+
onMessage: customOnMessage = null
|
|
786
|
+
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
787
|
+
const {
|
|
788
|
+
socket: contextSocket,
|
|
789
|
+
subscribe,
|
|
790
|
+
unsubscribe
|
|
791
|
+
} = useContext(SocketContext);
|
|
792
|
+
const socket = customSocket || contextSocket || null;
|
|
793
|
+
const [started, setStarted] = useState(socket !== null ? socket.isStarted() : false);
|
|
794
|
+
const channels = isString(channelNames) ? [channelNames] : channelNames;
|
|
795
|
+
const channelsKey = (channels || []).sort().join(',');
|
|
796
|
+
useEffect(() => {
|
|
986
797
|
if (socket === null) {
|
|
987
798
|
if (process.env.NODE_ENV !== 'production') {
|
|
988
799
|
console.warn('Socket context is empty.');
|
|
989
800
|
}
|
|
990
801
|
|
|
991
|
-
return
|
|
802
|
+
return () => {};
|
|
992
803
|
}
|
|
993
804
|
|
|
994
|
-
|
|
805
|
+
const wasStarted = socket.isStarted();
|
|
995
806
|
|
|
996
|
-
|
|
997
|
-
return setStarted(true);
|
|
998
|
-
};
|
|
807
|
+
const onStarted = () => setStarted(true);
|
|
999
808
|
|
|
1000
|
-
|
|
1001
|
-
return setStarted(false);
|
|
1002
|
-
};
|
|
809
|
+
const onStop = () => setStarted(false);
|
|
1003
810
|
|
|
1004
811
|
socket.on('stop', onStop);
|
|
1005
812
|
socket.on('started', onStarted);
|
|
@@ -1012,7 +819,7 @@ var useSocket = function useSocket() {
|
|
|
1012
819
|
socket.start();
|
|
1013
820
|
}
|
|
1014
821
|
|
|
1015
|
-
return
|
|
822
|
+
return () => {
|
|
1016
823
|
socket.off('stop', onStop);
|
|
1017
824
|
socket.off('started', onStarted);
|
|
1018
825
|
|
|
@@ -1025,27 +832,27 @@ var useSocket = function useSocket() {
|
|
|
1025
832
|
}
|
|
1026
833
|
};
|
|
1027
834
|
}, [channelsKey, customSocket]);
|
|
1028
|
-
useEffect(
|
|
835
|
+
useEffect(() => {
|
|
1029
836
|
if (socket === null) {
|
|
1030
|
-
return
|
|
837
|
+
return () => {};
|
|
1031
838
|
}
|
|
1032
839
|
|
|
1033
|
-
|
|
840
|
+
const onMessage = function () {
|
|
1034
841
|
if (customOnMessage !== null) {
|
|
1035
|
-
customOnMessage
|
|
842
|
+
customOnMessage(...arguments);
|
|
1036
843
|
}
|
|
1037
844
|
};
|
|
1038
845
|
|
|
1039
846
|
socket.on('message', onMessage);
|
|
1040
|
-
return
|
|
847
|
+
return () => {
|
|
1041
848
|
socket.off('message', onMessage);
|
|
1042
849
|
};
|
|
1043
850
|
}, [customOnMessage]);
|
|
1044
851
|
return {
|
|
1045
|
-
socket
|
|
1046
|
-
started
|
|
1047
|
-
subscribe
|
|
1048
|
-
unsubscribe
|
|
852
|
+
socket,
|
|
853
|
+
started,
|
|
854
|
+
subscribe,
|
|
855
|
+
unsubscribe
|
|
1049
856
|
};
|
|
1050
857
|
};
|
|
1051
858
|
|