@folklore/socket 0.4.16 → 0.4.17
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 +50 -197
- package/dist/es.js +27 -145
- package/package.json +3 -3
package/dist/cjs.js
CHANGED
|
@@ -12,38 +12,8 @@ var PropTypes = require('prop-types');
|
|
|
12
12
|
var jsxRuntime = require('react/jsx-runtime');
|
|
13
13
|
var isString = require('lodash/isString');
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
function _interopNamespace(e) {
|
|
18
|
-
if (e && e.__esModule) return e;
|
|
19
|
-
var n = Object.create(null);
|
|
20
|
-
if (e) {
|
|
21
|
-
Object.keys(e).forEach(function (k) {
|
|
22
|
-
if (k !== 'default') {
|
|
23
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
24
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
25
|
-
enumerable: true,
|
|
26
|
-
get: function () { return e[k]; }
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
n["default"] = e;
|
|
32
|
-
return Object.freeze(n);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
var createDebug__default = /*#__PURE__*/_interopDefaultLegacy(createDebug);
|
|
36
|
-
var invariant__default = /*#__PURE__*/_interopDefaultLegacy(invariant);
|
|
37
|
-
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);
|
|
38
|
-
var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
|
|
39
|
-
var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray);
|
|
40
|
-
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
41
|
-
var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes);
|
|
42
|
-
var isString__default = /*#__PURE__*/_interopDefaultLegacy(isString);
|
|
43
|
-
|
|
44
|
-
const debug$2 = createDebug__default["default"]('folklore:socket:pubnub');
|
|
45
|
-
|
|
46
|
-
class PubNubSocket extends EventEmitter__default["default"] {
|
|
15
|
+
const debug$2 = createDebug('folklore:socket:pubnub');
|
|
16
|
+
class PubNubSocket extends EventEmitter {
|
|
47
17
|
constructor(opts) {
|
|
48
18
|
super();
|
|
49
19
|
this.options = {
|
|
@@ -67,16 +37,13 @@ class PubNubSocket extends EventEmitter__default["default"] {
|
|
|
67
37
|
this.channels = [];
|
|
68
38
|
this.init();
|
|
69
39
|
}
|
|
70
|
-
|
|
71
40
|
onReady() {
|
|
72
41
|
if (this.destroyed) {
|
|
73
42
|
return;
|
|
74
43
|
}
|
|
75
|
-
|
|
76
44
|
this.ready = true;
|
|
77
45
|
this.emit('ready');
|
|
78
46
|
}
|
|
79
|
-
|
|
80
47
|
onStatus(statusEvent) {
|
|
81
48
|
if (statusEvent.category === 'PNConnectedCategory' && !this.started) {
|
|
82
49
|
this.started = true;
|
|
@@ -84,64 +51,53 @@ class PubNubSocket extends EventEmitter__default["default"] {
|
|
|
84
51
|
this.emit('started');
|
|
85
52
|
}
|
|
86
53
|
}
|
|
87
|
-
|
|
88
54
|
onMessage(_ref) {
|
|
89
55
|
let {
|
|
90
56
|
message
|
|
91
57
|
} = _ref;
|
|
92
58
|
this.emit('message', message);
|
|
93
|
-
|
|
94
59
|
if (typeof message.event !== 'undefined') {
|
|
95
60
|
this.emit(message.event, message.data || message);
|
|
96
61
|
}
|
|
97
62
|
}
|
|
98
|
-
|
|
99
63
|
updateChannels(channels) {
|
|
100
|
-
debug$2(
|
|
64
|
+
debug$2(`Updating channels: ${channels.join(', ')}`);
|
|
101
65
|
const {
|
|
102
66
|
shouldStart,
|
|
103
67
|
started,
|
|
104
68
|
starting
|
|
105
69
|
} = this;
|
|
106
|
-
|
|
107
70
|
if (started || starting) {
|
|
108
71
|
this.stop();
|
|
109
72
|
}
|
|
110
|
-
|
|
111
73
|
this.channels = channels;
|
|
112
|
-
|
|
113
74
|
if (started || starting || shouldStart) {
|
|
114
75
|
this.shouldStart = false;
|
|
115
76
|
this.start();
|
|
116
77
|
}
|
|
117
78
|
}
|
|
118
|
-
|
|
119
79
|
init() {
|
|
120
80
|
if (this.pubnub !== null) {
|
|
121
81
|
return;
|
|
122
82
|
}
|
|
123
|
-
|
|
124
83
|
debug$2('Init');
|
|
125
84
|
this.destroyed = false;
|
|
126
85
|
const loadPubnub = this.PubNub !== null ? Promise.resolve() : this.loadPubNub();
|
|
127
86
|
loadPubnub.then(() => this.createPubNub()).then(() => this.onReady());
|
|
128
87
|
}
|
|
129
|
-
|
|
130
88
|
loadPubNub() {
|
|
131
89
|
debug$2('Load PubNub');
|
|
132
|
-
return
|
|
90
|
+
return import('pubnub').then(_ref2 => {
|
|
133
91
|
let {
|
|
134
92
|
default: PubNub
|
|
135
93
|
} = _ref2;
|
|
136
94
|
this.PubNub = PubNub;
|
|
137
95
|
});
|
|
138
96
|
}
|
|
139
|
-
|
|
140
97
|
createPubNub() {
|
|
141
98
|
if (this.destroyed) {
|
|
142
99
|
return;
|
|
143
100
|
}
|
|
144
|
-
|
|
145
101
|
const {
|
|
146
102
|
PubNub
|
|
147
103
|
} = this;
|
|
@@ -149,15 +105,12 @@ class PubNubSocket extends EventEmitter__default["default"] {
|
|
|
149
105
|
publishKey: this.options.publishKey,
|
|
150
106
|
subscribeKey: this.options.subscribeKey
|
|
151
107
|
};
|
|
152
|
-
|
|
153
108
|
if (this.options.uuid !== null) {
|
|
154
109
|
pubnubOptions.uuid = this.options.uuid;
|
|
155
110
|
}
|
|
156
|
-
|
|
157
111
|
if (this.options.secretKey !== null) {
|
|
158
112
|
pubnubOptions.secretKey = this.options.secretKey;
|
|
159
113
|
}
|
|
160
|
-
|
|
161
114
|
this.pubnub = new PubNub(pubnubOptions);
|
|
162
115
|
this.pubnubListener = {
|
|
163
116
|
status: this.onStatus,
|
|
@@ -165,38 +118,31 @@ class PubNubSocket extends EventEmitter__default["default"] {
|
|
|
165
118
|
};
|
|
166
119
|
this.pubnub.addListener(this.pubnubListener);
|
|
167
120
|
}
|
|
168
|
-
|
|
169
121
|
destroy() {
|
|
170
122
|
this.destroyed = true;
|
|
171
123
|
this.stop();
|
|
172
|
-
|
|
173
124
|
if (this.pubnubListener) {
|
|
174
125
|
this.pubnub.removeListener(this.pubnubListener);
|
|
175
126
|
this.pubnubListener = null;
|
|
176
127
|
}
|
|
177
|
-
|
|
178
128
|
this.pubnub = null;
|
|
179
129
|
this.ready = false;
|
|
180
130
|
debug$2('Destroyed.');
|
|
181
131
|
}
|
|
182
|
-
|
|
183
132
|
start() {
|
|
184
133
|
if (this.started) {
|
|
185
134
|
debug$2('Skipping start: Already started.');
|
|
186
135
|
return;
|
|
187
136
|
}
|
|
188
|
-
|
|
189
137
|
if (this.starting) {
|
|
190
138
|
debug$2('Skipping start: Already starting.');
|
|
191
139
|
return;
|
|
192
140
|
}
|
|
193
|
-
|
|
194
141
|
if (this.channels.length === 0) {
|
|
195
142
|
debug$2('Skipping start: No channels.');
|
|
196
143
|
this.shouldStart = true;
|
|
197
144
|
return;
|
|
198
145
|
}
|
|
199
|
-
|
|
200
146
|
this.shouldStart = false;
|
|
201
147
|
this.starting = true;
|
|
202
148
|
this.pubnub.subscribe({
|
|
@@ -204,12 +150,10 @@ class PubNubSocket extends EventEmitter__default["default"] {
|
|
|
204
150
|
});
|
|
205
151
|
this.emit('start');
|
|
206
152
|
}
|
|
207
|
-
|
|
208
153
|
stop() {
|
|
209
154
|
if (!this.started && !this.starting) {
|
|
210
155
|
return;
|
|
211
156
|
}
|
|
212
|
-
|
|
213
157
|
debug$2('Stopping...');
|
|
214
158
|
this.shouldStart = false;
|
|
215
159
|
this.started = false;
|
|
@@ -219,13 +163,12 @@ class PubNubSocket extends EventEmitter__default["default"] {
|
|
|
219
163
|
});
|
|
220
164
|
this.emit('stop');
|
|
221
165
|
}
|
|
222
|
-
|
|
223
166
|
send(data) {
|
|
224
167
|
debug$2('Sending', data);
|
|
225
168
|
return new Promise((resolve, reject) => {
|
|
226
169
|
this.pubnub.publish(data, (status, response) => {
|
|
227
170
|
if (status.error) {
|
|
228
|
-
reject(new Error(
|
|
171
|
+
reject(new Error(`Error operation:${status.operation} status:${status.statusCode}`));
|
|
229
172
|
} else {
|
|
230
173
|
resolve({
|
|
231
174
|
status,
|
|
@@ -236,12 +179,10 @@ class PubNubSocket extends EventEmitter__default["default"] {
|
|
|
236
179
|
});
|
|
237
180
|
});
|
|
238
181
|
}
|
|
239
|
-
|
|
240
182
|
}
|
|
241
183
|
|
|
242
|
-
const debug$1 =
|
|
243
|
-
|
|
244
|
-
class SocketIOSocket extends EventEmitter__default["default"] {
|
|
184
|
+
const debug$1 = createDebug('folklore:socket:socketio');
|
|
185
|
+
class SocketIOSocket extends EventEmitter {
|
|
245
186
|
constructor(opts) {
|
|
246
187
|
super();
|
|
247
188
|
this.options = {
|
|
@@ -264,29 +205,24 @@ class SocketIOSocket extends EventEmitter__default["default"] {
|
|
|
264
205
|
this.channels = [];
|
|
265
206
|
this.init();
|
|
266
207
|
}
|
|
267
|
-
|
|
268
208
|
onReady() {
|
|
269
209
|
this.ready = true;
|
|
270
210
|
this.emit('ready');
|
|
271
211
|
}
|
|
272
|
-
|
|
273
212
|
onConnect(channel) {
|
|
274
213
|
debug$1('Socket connected on %s', channel);
|
|
275
|
-
|
|
276
214
|
if (!this.started) {
|
|
277
215
|
this.started = true;
|
|
278
216
|
this.starting = false;
|
|
279
217
|
this.emit('started');
|
|
280
218
|
}
|
|
281
219
|
}
|
|
282
|
-
|
|
283
220
|
onMessage(message, channel) {
|
|
284
221
|
debug$1('Message received on %s %o', channel, message);
|
|
285
222
|
this.emit('message', message, channel);
|
|
286
223
|
}
|
|
287
|
-
|
|
288
224
|
init() {
|
|
289
|
-
|
|
225
|
+
import('socket.io-client').then(_ref => {
|
|
290
226
|
let {
|
|
291
227
|
default: IO
|
|
292
228
|
} = _ref;
|
|
@@ -297,7 +233,6 @@ class SocketIOSocket extends EventEmitter__default["default"] {
|
|
|
297
233
|
}
|
|
298
234
|
});
|
|
299
235
|
}
|
|
300
|
-
|
|
301
236
|
createManager() {
|
|
302
237
|
const {
|
|
303
238
|
Manager
|
|
@@ -311,62 +246,52 @@ class SocketIOSocket extends EventEmitter__default["default"] {
|
|
|
311
246
|
...opts
|
|
312
247
|
});
|
|
313
248
|
}
|
|
314
|
-
|
|
315
249
|
updateChannels(channels) {
|
|
316
|
-
debug$1(
|
|
250
|
+
debug$1(`Updating channels: ${channels.join(', ')}`);
|
|
317
251
|
const {
|
|
318
252
|
shouldStart,
|
|
319
253
|
started,
|
|
320
254
|
starting
|
|
321
255
|
} = this;
|
|
322
|
-
|
|
323
256
|
if (started || starting) {
|
|
324
257
|
this.stop();
|
|
325
258
|
}
|
|
326
|
-
|
|
327
259
|
this.channels = channels;
|
|
328
|
-
|
|
329
260
|
if (started || starting || shouldStart) {
|
|
330
261
|
this.start();
|
|
331
262
|
}
|
|
332
263
|
}
|
|
333
|
-
|
|
334
264
|
start() {
|
|
335
265
|
if (this.started) {
|
|
336
266
|
debug$1('Skipping start: Already started.');
|
|
337
267
|
return;
|
|
338
268
|
}
|
|
339
|
-
|
|
340
269
|
if (this.starting) {
|
|
341
270
|
debug$1('Skipping start: Already starting.');
|
|
342
271
|
return;
|
|
343
272
|
}
|
|
344
|
-
|
|
345
273
|
if (this.io === null) {
|
|
346
274
|
debug$1('Socket.io not ready.');
|
|
347
275
|
this.shouldStart = true;
|
|
348
276
|
return;
|
|
349
277
|
}
|
|
350
|
-
|
|
351
278
|
if (this.channels.length === 0) {
|
|
352
279
|
debug$1('Skipping start: No channels.');
|
|
353
280
|
this.shouldStart = true;
|
|
354
281
|
return;
|
|
355
282
|
}
|
|
356
|
-
|
|
357
283
|
this.shouldStart = false;
|
|
358
284
|
this.starting = true;
|
|
359
|
-
this.sockets = this.channels.reduce((map, channel) => ({
|
|
285
|
+
this.sockets = this.channels.reduce((map, channel) => ({
|
|
286
|
+
...map,
|
|
360
287
|
[channel]: this.createSocket(channel)
|
|
361
288
|
}), {});
|
|
362
289
|
this.emit('start');
|
|
363
290
|
}
|
|
364
|
-
|
|
365
291
|
stop() {
|
|
366
292
|
if (!this.started && !this.starting) {
|
|
367
293
|
return;
|
|
368
294
|
}
|
|
369
|
-
|
|
370
295
|
debug$1('Stopping...');
|
|
371
296
|
this.shouldStart = false;
|
|
372
297
|
this.started = false;
|
|
@@ -374,45 +299,40 @@ class SocketIOSocket extends EventEmitter__default["default"] {
|
|
|
374
299
|
Object.values(this.sockets).forEach(socket => this.stopSocket(socket));
|
|
375
300
|
this.emit('stop');
|
|
376
301
|
}
|
|
377
|
-
|
|
378
302
|
createSocket(channel) {
|
|
379
|
-
const socket = this.io.socket(
|
|
303
|
+
const socket = this.io.socket(`/${channel.replace(/^\//, '')}`);
|
|
380
304
|
socket.on('message', message => this.onMessage(message, channel));
|
|
381
305
|
socket.on('connect', () => this.onConnect(channel));
|
|
382
306
|
socket.open();
|
|
383
307
|
return socket;
|
|
384
|
-
}
|
|
385
|
-
|
|
308
|
+
}
|
|
386
309
|
|
|
310
|
+
// eslint-disable-next-line class-methods-use-this
|
|
387
311
|
stopSocket(socket) {
|
|
388
312
|
socket.off('connect');
|
|
389
313
|
socket.off('message');
|
|
390
314
|
socket.close();
|
|
391
315
|
return socket;
|
|
392
316
|
}
|
|
393
|
-
|
|
394
317
|
destroy() {
|
|
395
318
|
this.stop();
|
|
396
319
|
this.sockets = {};
|
|
397
|
-
|
|
398
320
|
if (this.io !== null) {
|
|
399
321
|
this.io.close();
|
|
400
322
|
this.io = null;
|
|
401
323
|
}
|
|
402
324
|
}
|
|
403
|
-
|
|
404
325
|
send(data) {
|
|
405
326
|
const {
|
|
406
327
|
channel,
|
|
407
328
|
message
|
|
408
329
|
} = data;
|
|
409
|
-
const channels = !
|
|
330
|
+
const channels = !isArray(channel) ? [channel] : channel;
|
|
410
331
|
channels.forEach(ch => {
|
|
411
332
|
this.sockets[ch].send(message);
|
|
412
333
|
});
|
|
413
334
|
return Promise.resolve();
|
|
414
335
|
}
|
|
415
|
-
|
|
416
336
|
}
|
|
417
337
|
|
|
418
338
|
var SocketAdapters = {
|
|
@@ -421,37 +341,30 @@ var SocketAdapters = {
|
|
|
421
341
|
};
|
|
422
342
|
|
|
423
343
|
const normalize = str => str.replace(/[^a-z0-9]+/gi, '').toLowerCase();
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
class Socket extends EventEmitter__default["default"] {
|
|
344
|
+
const debug = createDebug('folklore:socket');
|
|
345
|
+
class Socket extends EventEmitter {
|
|
428
346
|
static getAdapters() {
|
|
429
347
|
return Socket.adapters;
|
|
430
348
|
}
|
|
431
|
-
|
|
432
349
|
static getAdapter(adapter) {
|
|
433
350
|
// prettier-ignore
|
|
434
351
|
const adapterKey = Object.keys(Socket.adapters).find(key => normalize(key) === normalize(adapter)) || null;
|
|
435
|
-
|
|
436
352
|
if (adapterKey === null) {
|
|
437
|
-
throw new Error(
|
|
353
|
+
throw new Error(`Adapter ${adapter} not found`);
|
|
438
354
|
}
|
|
439
|
-
|
|
440
355
|
return Socket.adapters[adapterKey];
|
|
441
356
|
}
|
|
442
|
-
|
|
443
357
|
static addAdapter(name, adapter) {
|
|
444
|
-
Socket.adapters = {
|
|
358
|
+
Socket.adapters = {
|
|
359
|
+
...Socket.adapters,
|
|
445
360
|
[name]: adapter
|
|
446
361
|
};
|
|
447
362
|
return Socket;
|
|
448
363
|
}
|
|
449
|
-
|
|
450
364
|
static setAdapters(adapters) {
|
|
451
365
|
Socket.adapters = adapters;
|
|
452
366
|
return Socket;
|
|
453
367
|
}
|
|
454
|
-
|
|
455
368
|
constructor(opts) {
|
|
456
369
|
super();
|
|
457
370
|
this.options = {
|
|
@@ -476,139 +389,110 @@ class Socket extends EventEmitter__default["default"] {
|
|
|
476
389
|
this.adapter = null;
|
|
477
390
|
this.channels = [];
|
|
478
391
|
this.init();
|
|
479
|
-
|
|
480
392
|
if (this.options.channels.length) {
|
|
481
393
|
this.setChannels(this.options.channels);
|
|
482
394
|
}
|
|
483
395
|
}
|
|
484
|
-
|
|
485
396
|
onAdapterReady() {
|
|
486
397
|
debug('Adapter ready');
|
|
487
398
|
this.ready = true;
|
|
488
399
|
this.emit('ready');
|
|
489
|
-
|
|
490
400
|
if (this.shouldStart) {
|
|
491
401
|
this.shouldStart = false;
|
|
492
402
|
this.start();
|
|
493
403
|
}
|
|
494
404
|
}
|
|
495
|
-
|
|
496
405
|
onAdapterStart() {
|
|
497
406
|
debug('Adapter starting...');
|
|
498
407
|
this.starting = true;
|
|
499
408
|
this.started = false;
|
|
500
409
|
this.emit('start');
|
|
501
410
|
}
|
|
502
|
-
|
|
503
411
|
onAdapterStarted() {
|
|
504
412
|
debug('Adapter started');
|
|
505
413
|
this.starting = false;
|
|
506
414
|
this.started = true;
|
|
507
415
|
this.emit('started');
|
|
508
416
|
}
|
|
509
|
-
|
|
510
417
|
onAdapterStop() {
|
|
511
418
|
debug('Adapter stopped');
|
|
512
419
|
this.starting = false;
|
|
513
420
|
this.started = false;
|
|
514
421
|
this.emit('stop');
|
|
515
422
|
}
|
|
516
|
-
|
|
517
423
|
onAdapterMessage(message) {
|
|
518
424
|
debug('Adapter message', message);
|
|
519
425
|
this.emit('message', message);
|
|
520
426
|
}
|
|
521
|
-
|
|
522
427
|
getChannelWithoutNamespace(name) {
|
|
523
428
|
if (this.options.namespace === null) {
|
|
524
429
|
return name;
|
|
525
430
|
}
|
|
526
|
-
|
|
527
|
-
const regExp = new RegExp("^".concat(this.options.namespace, ":"));
|
|
431
|
+
const regExp = new RegExp(`^${this.options.namespace}:`);
|
|
528
432
|
return name.replace(regExp, '');
|
|
529
433
|
}
|
|
530
|
-
|
|
531
434
|
getChannelWithNamespace(name) {
|
|
532
435
|
const parts = [];
|
|
533
|
-
|
|
534
436
|
if (this.options.namespace !== null) {
|
|
535
437
|
parts.push(this.options.namespace);
|
|
536
438
|
}
|
|
537
|
-
|
|
538
439
|
parts.push(name);
|
|
539
440
|
return parts.join(':');
|
|
540
441
|
}
|
|
541
|
-
|
|
542
442
|
setChannels(channels) {
|
|
543
443
|
const namespacedChannels = channels.map(channel => this.getChannelWithNamespace(channel)).sort();
|
|
544
|
-
|
|
545
444
|
if (this.channels.join(',') === namespacedChannels.join(',')) {
|
|
546
445
|
return;
|
|
547
446
|
}
|
|
548
|
-
|
|
549
|
-
debug("Set channels: ".concat(namespacedChannels.join(', ')));
|
|
447
|
+
debug(`Set channels: ${namespacedChannels.join(', ')}`);
|
|
550
448
|
this.updateChannels(namespacedChannels);
|
|
551
449
|
}
|
|
552
|
-
|
|
553
450
|
addChannel(channel) {
|
|
554
451
|
const namespacedChannel = this.getChannelWithNamespace(channel);
|
|
555
|
-
|
|
556
452
|
if (this.channels.indexOf(namespacedChannel) !== -1) {
|
|
557
453
|
return;
|
|
558
454
|
}
|
|
559
|
-
|
|
560
|
-
debug("Adding channel: ".concat(channel));
|
|
455
|
+
debug(`Adding channel: ${channel}`);
|
|
561
456
|
this.updateChannels([...this.channels, namespacedChannel]);
|
|
562
457
|
}
|
|
563
|
-
|
|
564
458
|
addChannels(channels) {
|
|
565
459
|
const namespacedChannels = channels.map(channel => this.getChannelWithNamespace(channel)).sort();
|
|
566
|
-
debug(
|
|
460
|
+
debug(`Adding channels: ${channels.join(',')}`);
|
|
567
461
|
this.updateChannels([...this.channels, ...namespacedChannels.filter(it => this.channels.indexOf(it) === -1)]);
|
|
568
462
|
}
|
|
569
|
-
|
|
570
463
|
removeChannel(channel) {
|
|
571
464
|
const namespacedChannel = this.getChannelWithNamespace(channel);
|
|
572
|
-
|
|
573
465
|
if (this.channels.indexOf(namespacedChannel) === -1) {
|
|
574
466
|
return;
|
|
575
467
|
}
|
|
576
|
-
|
|
577
|
-
debug("Removing channel: ".concat(channel));
|
|
468
|
+
debug(`Removing channel: ${channel}`);
|
|
578
469
|
this.updateChannels(this.channels.filter(ch => ch !== namespacedChannel));
|
|
579
470
|
}
|
|
580
|
-
|
|
581
471
|
removeChannels(channels) {
|
|
582
472
|
const namespacedChannels = channels.map(channel => this.getChannelWithNamespace(channel)).sort();
|
|
583
|
-
debug(
|
|
473
|
+
debug(`Removing channels: ${channels.join(',')}`);
|
|
584
474
|
this.updateChannels(this.channels.filter(it => namespacedChannels.indexOf(it) === -1));
|
|
585
475
|
}
|
|
586
|
-
|
|
587
476
|
updateChannels(channels) {
|
|
588
477
|
const sortedChannels = channels.sort();
|
|
589
|
-
debug(
|
|
478
|
+
debug(`Updating channels: ${sortedChannels.join(', ')}`);
|
|
590
479
|
this.channels = [...sortedChannels];
|
|
591
|
-
|
|
592
480
|
if (this.adapter !== null) {
|
|
593
481
|
this.adapter.updateChannels(sortedChannels);
|
|
594
482
|
}
|
|
595
483
|
}
|
|
596
|
-
|
|
597
484
|
hasChannel(channel) {
|
|
598
485
|
const namespacedChannel = this.getChannelWithNamespace(channel);
|
|
599
486
|
return this.channels.reduce((found, it) => found || it === namespacedChannel, false);
|
|
600
487
|
}
|
|
601
|
-
|
|
602
488
|
getChannels() {
|
|
603
489
|
return this.channels.map(channel => this.getChannelWithoutNamespace(channel));
|
|
604
490
|
}
|
|
605
|
-
|
|
606
491
|
init() {
|
|
607
492
|
if (this.adapter !== null) {
|
|
608
493
|
debug('Already initialized');
|
|
609
494
|
return;
|
|
610
495
|
}
|
|
611
|
-
|
|
612
496
|
debug('Init');
|
|
613
497
|
const {
|
|
614
498
|
adapter: adapterKey,
|
|
@@ -619,7 +503,7 @@ class Socket extends EventEmitter__default["default"] {
|
|
|
619
503
|
this.adapter = new SocketAdapter(adapterOptions);
|
|
620
504
|
const methods = ['start', 'stop', 'destroy', 'updateChannels', 'send'];
|
|
621
505
|
methods.forEach(method => {
|
|
622
|
-
|
|
506
|
+
invariant(isFunction(this.adapter[method] || null), `Socket adapter should implement method ${method}`);
|
|
623
507
|
});
|
|
624
508
|
this.adapter.on('ready', this.onAdapterReady);
|
|
625
509
|
this.adapter.on('start', this.onAdapterStart);
|
|
@@ -628,70 +512,57 @@ class Socket extends EventEmitter__default["default"] {
|
|
|
628
512
|
this.adapter.on('stop', this.onAdapterStop);
|
|
629
513
|
this.adapter.updateChannels(this.channels);
|
|
630
514
|
}
|
|
631
|
-
|
|
632
515
|
destroy() {
|
|
633
516
|
if (this.adapter !== null) {
|
|
634
517
|
this.adapter.removeAllListeners();
|
|
635
518
|
this.adapter.destroy();
|
|
636
519
|
this.adapter = null;
|
|
637
520
|
}
|
|
638
|
-
|
|
639
521
|
this.started = false;
|
|
640
522
|
this.starting = false;
|
|
641
523
|
this.ready = false;
|
|
642
524
|
debug('Destroyed.');
|
|
643
525
|
}
|
|
644
|
-
|
|
645
526
|
restart() {
|
|
646
527
|
this.stop();
|
|
647
528
|
this.start();
|
|
648
529
|
}
|
|
649
|
-
|
|
650
530
|
start() {
|
|
651
531
|
if (this.started) {
|
|
652
532
|
debug('Skipping start: Already started.');
|
|
653
533
|
return;
|
|
654
534
|
}
|
|
655
|
-
|
|
656
535
|
if (this.starting) {
|
|
657
536
|
debug('Skipping start: Already starting.');
|
|
658
537
|
return;
|
|
659
538
|
}
|
|
660
|
-
|
|
661
539
|
if (!this.ready) {
|
|
662
540
|
debug('Skipping start: No ready.');
|
|
663
541
|
this.shouldStart = true;
|
|
664
542
|
return;
|
|
665
543
|
}
|
|
666
|
-
|
|
667
544
|
this.shouldStart = false;
|
|
668
545
|
debug('Starting on channels:');
|
|
669
546
|
this.channels.forEach(channel => {
|
|
670
|
-
debug(
|
|
547
|
+
debug(`- ${this.getChannelWithoutNamespace(channel)}`);
|
|
671
548
|
});
|
|
672
549
|
this.adapter.start();
|
|
673
550
|
}
|
|
674
|
-
|
|
675
551
|
stop() {
|
|
676
552
|
this.shouldStart = false;
|
|
677
|
-
|
|
678
553
|
if (!this.started && !this.starting) {
|
|
679
554
|
return;
|
|
680
555
|
}
|
|
681
|
-
|
|
682
556
|
debug('Stopping...');
|
|
683
|
-
|
|
684
557
|
if (this.adapter !== null) {
|
|
685
558
|
this.adapter.stop();
|
|
686
559
|
}
|
|
687
560
|
}
|
|
688
|
-
|
|
689
561
|
send(data, channel) {
|
|
690
562
|
if (!this.started) {
|
|
691
563
|
debug('Abort sending data: Not started');
|
|
692
564
|
return Promise.reject();
|
|
693
565
|
}
|
|
694
|
-
|
|
695
566
|
const publishData = typeof data.channel !== 'undefined' && typeof data.message !== 'undefined' ? data : {
|
|
696
567
|
channel: typeof channel !== 'undefined' ? this.getChannelWithNamespace(channel) : this.channels,
|
|
697
568
|
message: data
|
|
@@ -699,34 +570,32 @@ class Socket extends EventEmitter__default["default"] {
|
|
|
699
570
|
debug('Sending', publishData);
|
|
700
571
|
return this.adapter.send(publishData);
|
|
701
572
|
}
|
|
702
|
-
|
|
703
573
|
isStarted() {
|
|
704
574
|
return this.started;
|
|
705
575
|
}
|
|
706
|
-
|
|
707
576
|
}
|
|
708
|
-
|
|
709
|
-
|
|
577
|
+
Socket.adapters = {
|
|
578
|
+
...SocketAdapters
|
|
710
579
|
};
|
|
711
580
|
|
|
712
|
-
const SocketContext = /*#__PURE__*/
|
|
581
|
+
const SocketContext = /*#__PURE__*/React.createContext({
|
|
713
582
|
socket: null,
|
|
714
583
|
subscribe: () => {},
|
|
715
584
|
unsubscribe: () => {}
|
|
716
585
|
});
|
|
717
586
|
|
|
718
587
|
const propTypes = {
|
|
719
|
-
socket:
|
|
720
|
-
adapter:
|
|
721
|
-
host:
|
|
722
|
-
namespace:
|
|
723
|
-
uuid:
|
|
724
|
-
publishKey:
|
|
725
|
-
subscribeKey:
|
|
726
|
-
secretKey:
|
|
727
|
-
channels:
|
|
728
|
-
autoStart:
|
|
729
|
-
children:
|
|
588
|
+
socket: PropTypes.instanceOf(Socket),
|
|
589
|
+
adapter: PropTypes.string,
|
|
590
|
+
host: PropTypes.string,
|
|
591
|
+
namespace: PropTypes.string,
|
|
592
|
+
uuid: PropTypes.string,
|
|
593
|
+
publishKey: PropTypes.string,
|
|
594
|
+
subscribeKey: PropTypes.string,
|
|
595
|
+
secretKey: PropTypes.string,
|
|
596
|
+
channels: PropTypes.arrayOf(PropTypes.string),
|
|
597
|
+
autoStart: PropTypes.bool,
|
|
598
|
+
children: PropTypes.node
|
|
730
599
|
};
|
|
731
600
|
const defaultProps = {
|
|
732
601
|
socket: null,
|
|
@@ -741,7 +610,6 @@ const defaultProps = {
|
|
|
741
610
|
autoStart: false,
|
|
742
611
|
children: null
|
|
743
612
|
};
|
|
744
|
-
|
|
745
613
|
const SocketContainer = function (_ref) {
|
|
746
614
|
let {
|
|
747
615
|
children,
|
|
@@ -772,7 +640,8 @@ const SocketContainer = function (_ref) {
|
|
|
772
640
|
setChannels(newChannels);
|
|
773
641
|
}, [finalSocket, setChannels]);
|
|
774
642
|
const addToChannelsCount = React.useCallback(newChannels => {
|
|
775
|
-
channelsCountRef.current = newChannels.reduce((map, channel) => ({
|
|
643
|
+
channelsCountRef.current = newChannels.reduce((map, channel) => ({
|
|
644
|
+
...map,
|
|
776
645
|
[channel]: (map[channel] || 0) + 1
|
|
777
646
|
}), channelsCountRef.current);
|
|
778
647
|
updateChannels(Object.keys(channelsCountRef.current));
|
|
@@ -780,7 +649,8 @@ const SocketContainer = function (_ref) {
|
|
|
780
649
|
const removeToChannelsCount = React.useCallback(newChannels => {
|
|
781
650
|
channelsCountRef.current = newChannels.reduce((map, channel) => {
|
|
782
651
|
const newCount = (map[channel] || 0) - 1;
|
|
783
|
-
return newCount > 0 ? {
|
|
652
|
+
return newCount > 0 ? {
|
|
653
|
+
...map,
|
|
784
654
|
[channel]: newCount
|
|
785
655
|
} : map;
|
|
786
656
|
}, channelsCountRef.current);
|
|
@@ -796,11 +666,9 @@ const SocketContainer = function (_ref) {
|
|
|
796
666
|
}, [initialChannels, subscribe, unsubscribe]);
|
|
797
667
|
React.useEffect(() => {
|
|
798
668
|
finalSocket.init();
|
|
799
|
-
|
|
800
669
|
if (autoStart) {
|
|
801
670
|
finalSocket.start();
|
|
802
671
|
}
|
|
803
|
-
|
|
804
672
|
return () => {
|
|
805
673
|
finalSocket.destroy();
|
|
806
674
|
};
|
|
@@ -816,12 +684,10 @@ const SocketContainer = function (_ref) {
|
|
|
816
684
|
children: children
|
|
817
685
|
});
|
|
818
686
|
};
|
|
819
|
-
|
|
820
687
|
SocketContainer.propTypes = propTypes;
|
|
821
688
|
SocketContainer.defaultProps = defaultProps;
|
|
822
689
|
|
|
823
690
|
const getDisplayName = WrappedComponent => WrappedComponent.displayName || WrappedComponent.name || 'Component';
|
|
824
|
-
|
|
825
691
|
const withSocket = WrappedComponent => {
|
|
826
692
|
const WithSocketComponent = props => /*#__PURE__*/jsxRuntime.jsx(SocketContext.Consumer, {
|
|
827
693
|
children: _ref => {
|
|
@@ -838,8 +704,7 @@ const withSocket = WrappedComponent => {
|
|
|
838
704
|
});
|
|
839
705
|
}
|
|
840
706
|
});
|
|
841
|
-
|
|
842
|
-
WithSocketComponent.displayName = "WithSocket(".concat(getDisplayName(WrappedComponent), ")");
|
|
707
|
+
WithSocketComponent.displayName = `WithSocket(${getDisplayName(WrappedComponent)})`;
|
|
843
708
|
return WithSocketComponent;
|
|
844
709
|
};
|
|
845
710
|
|
|
@@ -856,42 +721,32 @@ const useSocket = function () {
|
|
|
856
721
|
} = React.useContext(SocketContext);
|
|
857
722
|
const socket = customSocket || contextSocket || null;
|
|
858
723
|
const [started, setStarted] = React.useState(socket !== null ? socket.isStarted() : false);
|
|
859
|
-
const channels =
|
|
724
|
+
const channels = isString(channelNames) ? [channelNames] : channelNames;
|
|
860
725
|
const channelsKey = (channels || []).sort().join(',');
|
|
861
726
|
React.useEffect(() => {
|
|
862
727
|
if (socket === null) {
|
|
863
728
|
if (process.env.NODE_ENV !== 'production') {
|
|
864
729
|
console.warn('Socket context is empty.');
|
|
865
730
|
}
|
|
866
|
-
|
|
867
731
|
return () => {};
|
|
868
732
|
}
|
|
869
|
-
|
|
870
733
|
const wasStarted = socket.isStarted();
|
|
871
|
-
|
|
872
734
|
const onStarted = () => setStarted(true);
|
|
873
|
-
|
|
874
735
|
const onStop = () => setStarted(false);
|
|
875
|
-
|
|
876
736
|
socket.on('stop', onStop);
|
|
877
737
|
socket.on('started', onStarted);
|
|
878
|
-
|
|
879
738
|
if (channels !== null) {
|
|
880
739
|
subscribe(channels);
|
|
881
740
|
}
|
|
882
|
-
|
|
883
741
|
if (!wasStarted) {
|
|
884
742
|
socket.start();
|
|
885
743
|
}
|
|
886
|
-
|
|
887
744
|
return () => {
|
|
888
745
|
socket.off('stop', onStop);
|
|
889
746
|
socket.off('started', onStarted);
|
|
890
|
-
|
|
891
747
|
if (channels !== null) {
|
|
892
748
|
unsubscribe(channels);
|
|
893
749
|
}
|
|
894
|
-
|
|
895
750
|
if (!wasStarted) {
|
|
896
751
|
socket.stop();
|
|
897
752
|
}
|
|
@@ -901,13 +756,11 @@ const useSocket = function () {
|
|
|
901
756
|
if (socket === null) {
|
|
902
757
|
return () => {};
|
|
903
758
|
}
|
|
904
|
-
|
|
905
759
|
const onMessage = function () {
|
|
906
760
|
if (customOnMessage !== null) {
|
|
907
761
|
customOnMessage(...arguments);
|
|
908
762
|
}
|
|
909
763
|
};
|
|
910
|
-
|
|
911
764
|
socket.on('message', onMessage);
|
|
912
765
|
return () => {
|
|
913
766
|
socket.off('message', onMessage);
|
|
@@ -924,6 +777,6 @@ const useSocket = function () {
|
|
|
924
777
|
exports.Socket = Socket;
|
|
925
778
|
exports.SocketContainer = SocketContainer;
|
|
926
779
|
exports.SocketContext = SocketContext;
|
|
927
|
-
exports
|
|
780
|
+
exports.default = Socket;
|
|
928
781
|
exports.useSocket = useSocket;
|
|
929
782
|
exports.withSocket = withSocket;
|