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