@folklore/socket 0.4.16 → 0.4.18
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 +239 -215
- package/dist/es.js +218 -165
- package/package.json +4 -3
package/dist/es.js
CHANGED
|
@@ -8,8 +8,7 @@ import PropTypes from 'prop-types';
|
|
|
8
8
|
import { jsx } from 'react/jsx-runtime';
|
|
9
9
|
import isString from 'lodash/isString';
|
|
10
10
|
|
|
11
|
-
const debug$
|
|
12
|
-
|
|
11
|
+
const debug$3 = createDebug('folklore:socket:pubnub');
|
|
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,51 +47,42 @@ 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$
|
|
60
|
+
debug$3(`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
|
-
debug$2('Init');
|
|
79
|
+
debug$3('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
|
-
debug$
|
|
85
|
+
debug$3('Load PubNub');
|
|
99
86
|
return import('pubnub').then(_ref2 => {
|
|
100
87
|
let {
|
|
101
88
|
default: PubNub
|
|
@@ -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
|
-
debug$
|
|
126
|
+
debug$3('Destroyed.');
|
|
148
127
|
}
|
|
149
|
-
|
|
150
128
|
start() {
|
|
151
129
|
if (this.started) {
|
|
152
|
-
debug$
|
|
130
|
+
debug$3('Skipping start: Already started.');
|
|
153
131
|
return;
|
|
154
132
|
}
|
|
155
|
-
|
|
156
133
|
if (this.starting) {
|
|
157
|
-
debug$
|
|
134
|
+
debug$3('Skipping start: Already starting.');
|
|
158
135
|
return;
|
|
159
136
|
}
|
|
160
|
-
|
|
161
137
|
if (this.channels.length === 0) {
|
|
162
|
-
debug$
|
|
138
|
+
debug$3('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,13 +146,11 @@ 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
|
-
debug$2('Stopping...');
|
|
153
|
+
debug$3('Stopping...');
|
|
181
154
|
this.shouldStart = false;
|
|
182
155
|
this.started = false;
|
|
183
156
|
this.starting = false;
|
|
@@ -186,13 +159,12 @@ class PubNubSocket extends EventEmitter {
|
|
|
186
159
|
});
|
|
187
160
|
this.emit('stop');
|
|
188
161
|
}
|
|
189
|
-
|
|
190
162
|
send(data) {
|
|
191
|
-
debug$
|
|
163
|
+
debug$3('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
|
-
const debug$
|
|
210
|
-
|
|
180
|
+
const debug$2 = createDebug('folklore:socket:socketio');
|
|
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
|
-
debug$
|
|
242
|
-
|
|
209
|
+
debug$2('Socket connected on %s', channel);
|
|
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
|
-
debug$
|
|
217
|
+
debug$2('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,96 +242,82 @@ class SocketIOSocket extends EventEmitter {
|
|
|
278
242
|
...opts
|
|
279
243
|
});
|
|
280
244
|
}
|
|
281
|
-
|
|
282
245
|
updateChannels(channels) {
|
|
283
|
-
debug$
|
|
246
|
+
debug$2(`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
|
-
debug$
|
|
262
|
+
debug$2('Skipping start: Already started.');
|
|
304
263
|
return;
|
|
305
264
|
}
|
|
306
|
-
|
|
307
265
|
if (this.starting) {
|
|
308
|
-
debug$
|
|
266
|
+
debug$2('Skipping start: Already starting.');
|
|
309
267
|
return;
|
|
310
268
|
}
|
|
311
|
-
|
|
312
269
|
if (this.io === null) {
|
|
313
|
-
debug$
|
|
270
|
+
debug$2('Socket.io not ready.');
|
|
314
271
|
this.shouldStart = true;
|
|
315
272
|
return;
|
|
316
273
|
}
|
|
317
|
-
|
|
318
274
|
if (this.channels.length === 0) {
|
|
319
|
-
debug$
|
|
275
|
+
debug$2('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
|
-
debug$1('Stopping...');
|
|
291
|
+
debug$2('Stopping...');
|
|
338
292
|
this.shouldStart = false;
|
|
339
293
|
this.started = false;
|
|
340
294
|
this.starting = false;
|
|
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,46 +329,207 @@ class SocketIOSocket extends EventEmitter {
|
|
|
379
329
|
});
|
|
380
330
|
return Promise.resolve();
|
|
381
331
|
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const debug$1 = createDebug('folklore:socket:pusher');
|
|
335
|
+
class PusherSocker extends EventEmitter {
|
|
336
|
+
constructor(opts) {
|
|
337
|
+
super();
|
|
338
|
+
this.options = {
|
|
339
|
+
uuid: null,
|
|
340
|
+
publishKey: null,
|
|
341
|
+
subscribeKey: null,
|
|
342
|
+
secretKey: null,
|
|
343
|
+
...opts
|
|
344
|
+
};
|
|
345
|
+
this.onReady = this.onReady.bind(this);
|
|
346
|
+
this.onConnected = this.onConnected.bind(this);
|
|
347
|
+
this.onMessage = this.onMessage.bind(this);
|
|
348
|
+
this.destroyed = false;
|
|
349
|
+
this.ready = false;
|
|
350
|
+
this.shouldStart = false;
|
|
351
|
+
this.started = false;
|
|
352
|
+
this.starting = false;
|
|
353
|
+
this.Pusher = null;
|
|
354
|
+
this.pusher = null;
|
|
355
|
+
this.channels = [];
|
|
356
|
+
this.clients = {};
|
|
357
|
+
this.init();
|
|
358
|
+
}
|
|
359
|
+
onReady() {
|
|
360
|
+
if (this.destroyed) {
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
this.ready = true;
|
|
364
|
+
this.emit('ready');
|
|
365
|
+
}
|
|
366
|
+
onConnected() {
|
|
367
|
+
this.started = true;
|
|
368
|
+
this.starting = false;
|
|
369
|
+
this.emit('started');
|
|
370
|
+
}
|
|
371
|
+
onMessage(message) {
|
|
372
|
+
this.emit('message', message);
|
|
373
|
+
if (typeof message.event !== 'undefined') {
|
|
374
|
+
this.emit(message.event, message.data || message);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
updateChannels(channels) {
|
|
378
|
+
debug$1(`Updating channels: ${channels.join(', ')}`);
|
|
379
|
+
const {
|
|
380
|
+
shouldStart,
|
|
381
|
+
started,
|
|
382
|
+
starting
|
|
383
|
+
} = this;
|
|
384
|
+
if (started || starting) {
|
|
385
|
+
this.stop();
|
|
386
|
+
}
|
|
387
|
+
this.channels = channels;
|
|
388
|
+
if (started || starting || shouldStart) {
|
|
389
|
+
this.shouldStart = false;
|
|
390
|
+
this.start();
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
init() {
|
|
394
|
+
if (this.pusher !== null) {
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
debug$1('Init');
|
|
398
|
+
this.destroyed = false;
|
|
399
|
+
const loadPusher = this.Pusher !== null ? Promise.resolve() : this.loadPusher();
|
|
400
|
+
loadPusher.then(() => this.createPusher()).then(() => this.onReady());
|
|
401
|
+
}
|
|
402
|
+
loadPusher() {
|
|
403
|
+
debug$1('Load PusherJs');
|
|
404
|
+
return import('pusher-js').then(_ref => {
|
|
405
|
+
let {
|
|
406
|
+
default: Pusher
|
|
407
|
+
} = _ref;
|
|
408
|
+
this.Pusher = Pusher;
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
createPusher() {
|
|
412
|
+
if (this.destroyed) {
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
const {
|
|
416
|
+
Pusher
|
|
417
|
+
} = this;
|
|
418
|
+
const {
|
|
419
|
+
appKey,
|
|
420
|
+
...options
|
|
421
|
+
} = this.options;
|
|
422
|
+
this.pusher = new Pusher(appKey, options);
|
|
423
|
+
}
|
|
424
|
+
destroy() {
|
|
425
|
+
this.destroyed = true;
|
|
426
|
+
this.stop();
|
|
427
|
+
this.pusher = null;
|
|
428
|
+
this.clients = {};
|
|
429
|
+
this.ready = false;
|
|
430
|
+
debug$1('Destroyed.');
|
|
431
|
+
}
|
|
432
|
+
start() {
|
|
433
|
+
if (this.started) {
|
|
434
|
+
debug$1('Skipping start: Already started.');
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
if (this.starting) {
|
|
438
|
+
debug$1('Skipping start: Already starting.');
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
if (this.io === null) {
|
|
442
|
+
debug$1('Socket.io not ready.');
|
|
443
|
+
this.shouldStart = true;
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
if (this.channels.length === 0) {
|
|
447
|
+
debug$1('Skipping start: No channels.');
|
|
448
|
+
this.shouldStart = true;
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
this.shouldStart = false;
|
|
452
|
+
this.starting = true;
|
|
453
|
+
this.pusher.connection.bind('connected', this.onConnected);
|
|
454
|
+
this.clients = this.channels.reduce((map, channel) => ({
|
|
455
|
+
...map,
|
|
456
|
+
[channel]: this.createClient(channel)
|
|
457
|
+
}), {});
|
|
458
|
+
this.emit('start');
|
|
459
|
+
}
|
|
460
|
+
stop() {
|
|
461
|
+
if (!this.started && !this.starting) {
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
debug$1('Stopping...');
|
|
465
|
+
this.shouldStart = false;
|
|
466
|
+
this.started = false;
|
|
467
|
+
this.starting = false;
|
|
468
|
+
this.pusher.connection.unbind('connected');
|
|
469
|
+
Object.keys(this.clients).forEach(channelName => this.stopClient(channelName, this.clients[channelName]));
|
|
470
|
+
this.clients = {};
|
|
471
|
+
this.emit('stop');
|
|
472
|
+
}
|
|
473
|
+
createClient(channelName) {
|
|
474
|
+
const channel = this.pusher.subscribe(channelName);
|
|
475
|
+
channel.bind_global((event, data) => this.onMessage({
|
|
476
|
+
event,
|
|
477
|
+
data
|
|
478
|
+
}, channel));
|
|
479
|
+
return channel;
|
|
480
|
+
}
|
|
382
481
|
|
|
482
|
+
// eslint-disable-next-line class-methods-use-this
|
|
483
|
+
stopClient(channelName, channel) {
|
|
484
|
+
channel.unbind_global();
|
|
485
|
+
this.pusher.unsubscribe(channelName);
|
|
486
|
+
return channel;
|
|
487
|
+
}
|
|
488
|
+
send(data) {
|
|
489
|
+
debug$1('Sending', data);
|
|
490
|
+
return new Promise(resolve => {
|
|
491
|
+
const {
|
|
492
|
+
channel,
|
|
493
|
+
event = null,
|
|
494
|
+
data: eventData
|
|
495
|
+
} = data;
|
|
496
|
+
this.pusher.trigger(channel, event || 'message', eventData);
|
|
497
|
+
resolve();
|
|
498
|
+
});
|
|
499
|
+
}
|
|
383
500
|
}
|
|
384
501
|
|
|
385
502
|
var SocketAdapters = {
|
|
386
503
|
PubNub: PubNubSocket,
|
|
387
|
-
SocketIO: SocketIOSocket
|
|
504
|
+
SocketIO: SocketIOSocket,
|
|
505
|
+
Pusher: PusherSocker
|
|
388
506
|
};
|
|
389
507
|
|
|
390
508
|
const normalize = str => str.replace(/[^a-z0-9]+/gi, '').toLowerCase();
|
|
391
|
-
|
|
392
509
|
const debug = createDebug('folklore:socket');
|
|
393
|
-
|
|
394
510
|
class Socket extends EventEmitter {
|
|
395
511
|
static getAdapters() {
|
|
396
512
|
return Socket.adapters;
|
|
397
513
|
}
|
|
398
|
-
|
|
399
514
|
static getAdapter(adapter) {
|
|
400
515
|
// prettier-ignore
|
|
401
516
|
const adapterKey = Object.keys(Socket.adapters).find(key => normalize(key) === normalize(adapter)) || null;
|
|
402
|
-
|
|
403
517
|
if (adapterKey === null) {
|
|
404
|
-
throw new Error(
|
|
518
|
+
throw new Error(`Adapter ${adapter} not found`);
|
|
405
519
|
}
|
|
406
|
-
|
|
407
520
|
return Socket.adapters[adapterKey];
|
|
408
521
|
}
|
|
409
|
-
|
|
410
522
|
static addAdapter(name, adapter) {
|
|
411
|
-
Socket.adapters = {
|
|
523
|
+
Socket.adapters = {
|
|
524
|
+
...Socket.adapters,
|
|
412
525
|
[name]: adapter
|
|
413
526
|
};
|
|
414
527
|
return Socket;
|
|
415
528
|
}
|
|
416
|
-
|
|
417
529
|
static setAdapters(adapters) {
|
|
418
530
|
Socket.adapters = adapters;
|
|
419
531
|
return Socket;
|
|
420
532
|
}
|
|
421
|
-
|
|
422
533
|
constructor(opts) {
|
|
423
534
|
super();
|
|
424
535
|
this.options = {
|
|
@@ -443,139 +554,110 @@ class Socket extends EventEmitter {
|
|
|
443
554
|
this.adapter = null;
|
|
444
555
|
this.channels = [];
|
|
445
556
|
this.init();
|
|
446
|
-
|
|
447
557
|
if (this.options.channels.length) {
|
|
448
558
|
this.setChannels(this.options.channels);
|
|
449
559
|
}
|
|
450
560
|
}
|
|
451
|
-
|
|
452
561
|
onAdapterReady() {
|
|
453
562
|
debug('Adapter ready');
|
|
454
563
|
this.ready = true;
|
|
455
564
|
this.emit('ready');
|
|
456
|
-
|
|
457
565
|
if (this.shouldStart) {
|
|
458
566
|
this.shouldStart = false;
|
|
459
567
|
this.start();
|
|
460
568
|
}
|
|
461
569
|
}
|
|
462
|
-
|
|
463
570
|
onAdapterStart() {
|
|
464
571
|
debug('Adapter starting...');
|
|
465
572
|
this.starting = true;
|
|
466
573
|
this.started = false;
|
|
467
574
|
this.emit('start');
|
|
468
575
|
}
|
|
469
|
-
|
|
470
576
|
onAdapterStarted() {
|
|
471
577
|
debug('Adapter started');
|
|
472
578
|
this.starting = false;
|
|
473
579
|
this.started = true;
|
|
474
580
|
this.emit('started');
|
|
475
581
|
}
|
|
476
|
-
|
|
477
582
|
onAdapterStop() {
|
|
478
583
|
debug('Adapter stopped');
|
|
479
584
|
this.starting = false;
|
|
480
585
|
this.started = false;
|
|
481
586
|
this.emit('stop');
|
|
482
587
|
}
|
|
483
|
-
|
|
484
588
|
onAdapterMessage(message) {
|
|
485
589
|
debug('Adapter message', message);
|
|
486
590
|
this.emit('message', message);
|
|
487
591
|
}
|
|
488
|
-
|
|
489
592
|
getChannelWithoutNamespace(name) {
|
|
490
593
|
if (this.options.namespace === null) {
|
|
491
594
|
return name;
|
|
492
595
|
}
|
|
493
|
-
|
|
494
|
-
const regExp = new RegExp("^".concat(this.options.namespace, ":"));
|
|
596
|
+
const regExp = new RegExp(`^${this.options.namespace}:`);
|
|
495
597
|
return name.replace(regExp, '');
|
|
496
598
|
}
|
|
497
|
-
|
|
498
599
|
getChannelWithNamespace(name) {
|
|
499
600
|
const parts = [];
|
|
500
|
-
|
|
501
601
|
if (this.options.namespace !== null) {
|
|
502
602
|
parts.push(this.options.namespace);
|
|
503
603
|
}
|
|
504
|
-
|
|
505
604
|
parts.push(name);
|
|
506
605
|
return parts.join(':');
|
|
507
606
|
}
|
|
508
|
-
|
|
509
607
|
setChannels(channels) {
|
|
510
608
|
const namespacedChannels = channels.map(channel => this.getChannelWithNamespace(channel)).sort();
|
|
511
|
-
|
|
512
609
|
if (this.channels.join(',') === namespacedChannels.join(',')) {
|
|
513
610
|
return;
|
|
514
611
|
}
|
|
515
|
-
|
|
516
|
-
debug("Set channels: ".concat(namespacedChannels.join(', ')));
|
|
612
|
+
debug(`Set channels: ${namespacedChannels.join(', ')}`);
|
|
517
613
|
this.updateChannels(namespacedChannels);
|
|
518
614
|
}
|
|
519
|
-
|
|
520
615
|
addChannel(channel) {
|
|
521
616
|
const namespacedChannel = this.getChannelWithNamespace(channel);
|
|
522
|
-
|
|
523
617
|
if (this.channels.indexOf(namespacedChannel) !== -1) {
|
|
524
618
|
return;
|
|
525
619
|
}
|
|
526
|
-
|
|
527
|
-
debug("Adding channel: ".concat(channel));
|
|
620
|
+
debug(`Adding channel: ${channel}`);
|
|
528
621
|
this.updateChannels([...this.channels, namespacedChannel]);
|
|
529
622
|
}
|
|
530
|
-
|
|
531
623
|
addChannels(channels) {
|
|
532
624
|
const namespacedChannels = channels.map(channel => this.getChannelWithNamespace(channel)).sort();
|
|
533
|
-
debug(
|
|
625
|
+
debug(`Adding channels: ${channels.join(',')}`);
|
|
534
626
|
this.updateChannels([...this.channels, ...namespacedChannels.filter(it => this.channels.indexOf(it) === -1)]);
|
|
535
627
|
}
|
|
536
|
-
|
|
537
628
|
removeChannel(channel) {
|
|
538
629
|
const namespacedChannel = this.getChannelWithNamespace(channel);
|
|
539
|
-
|
|
540
630
|
if (this.channels.indexOf(namespacedChannel) === -1) {
|
|
541
631
|
return;
|
|
542
632
|
}
|
|
543
|
-
|
|
544
|
-
debug("Removing channel: ".concat(channel));
|
|
633
|
+
debug(`Removing channel: ${channel}`);
|
|
545
634
|
this.updateChannels(this.channels.filter(ch => ch !== namespacedChannel));
|
|
546
635
|
}
|
|
547
|
-
|
|
548
636
|
removeChannels(channels) {
|
|
549
637
|
const namespacedChannels = channels.map(channel => this.getChannelWithNamespace(channel)).sort();
|
|
550
|
-
debug(
|
|
638
|
+
debug(`Removing channels: ${channels.join(',')}`);
|
|
551
639
|
this.updateChannels(this.channels.filter(it => namespacedChannels.indexOf(it) === -1));
|
|
552
640
|
}
|
|
553
|
-
|
|
554
641
|
updateChannels(channels) {
|
|
555
642
|
const sortedChannels = channels.sort();
|
|
556
|
-
debug(
|
|
643
|
+
debug(`Updating channels: ${sortedChannels.join(', ')}`);
|
|
557
644
|
this.channels = [...sortedChannels];
|
|
558
|
-
|
|
559
645
|
if (this.adapter !== null) {
|
|
560
646
|
this.adapter.updateChannels(sortedChannels);
|
|
561
647
|
}
|
|
562
648
|
}
|
|
563
|
-
|
|
564
649
|
hasChannel(channel) {
|
|
565
650
|
const namespacedChannel = this.getChannelWithNamespace(channel);
|
|
566
651
|
return this.channels.reduce((found, it) => found || it === namespacedChannel, false);
|
|
567
652
|
}
|
|
568
|
-
|
|
569
653
|
getChannels() {
|
|
570
654
|
return this.channels.map(channel => this.getChannelWithoutNamespace(channel));
|
|
571
655
|
}
|
|
572
|
-
|
|
573
656
|
init() {
|
|
574
657
|
if (this.adapter !== null) {
|
|
575
658
|
debug('Already initialized');
|
|
576
659
|
return;
|
|
577
660
|
}
|
|
578
|
-
|
|
579
661
|
debug('Init');
|
|
580
662
|
const {
|
|
581
663
|
adapter: adapterKey,
|
|
@@ -586,7 +668,7 @@ class Socket extends EventEmitter {
|
|
|
586
668
|
this.adapter = new SocketAdapter(adapterOptions);
|
|
587
669
|
const methods = ['start', 'stop', 'destroy', 'updateChannels', 'send'];
|
|
588
670
|
methods.forEach(method => {
|
|
589
|
-
invariant(isFunction(this.adapter[method] || null),
|
|
671
|
+
invariant(isFunction(this.adapter[method] || null), `Socket adapter should implement method ${method}`);
|
|
590
672
|
});
|
|
591
673
|
this.adapter.on('ready', this.onAdapterReady);
|
|
592
674
|
this.adapter.on('start', this.onAdapterStart);
|
|
@@ -595,70 +677,57 @@ class Socket extends EventEmitter {
|
|
|
595
677
|
this.adapter.on('stop', this.onAdapterStop);
|
|
596
678
|
this.adapter.updateChannels(this.channels);
|
|
597
679
|
}
|
|
598
|
-
|
|
599
680
|
destroy() {
|
|
600
681
|
if (this.adapter !== null) {
|
|
601
682
|
this.adapter.removeAllListeners();
|
|
602
683
|
this.adapter.destroy();
|
|
603
684
|
this.adapter = null;
|
|
604
685
|
}
|
|
605
|
-
|
|
606
686
|
this.started = false;
|
|
607
687
|
this.starting = false;
|
|
608
688
|
this.ready = false;
|
|
609
689
|
debug('Destroyed.');
|
|
610
690
|
}
|
|
611
|
-
|
|
612
691
|
restart() {
|
|
613
692
|
this.stop();
|
|
614
693
|
this.start();
|
|
615
694
|
}
|
|
616
|
-
|
|
617
695
|
start() {
|
|
618
696
|
if (this.started) {
|
|
619
697
|
debug('Skipping start: Already started.');
|
|
620
698
|
return;
|
|
621
699
|
}
|
|
622
|
-
|
|
623
700
|
if (this.starting) {
|
|
624
701
|
debug('Skipping start: Already starting.');
|
|
625
702
|
return;
|
|
626
703
|
}
|
|
627
|
-
|
|
628
704
|
if (!this.ready) {
|
|
629
705
|
debug('Skipping start: No ready.');
|
|
630
706
|
this.shouldStart = true;
|
|
631
707
|
return;
|
|
632
708
|
}
|
|
633
|
-
|
|
634
709
|
this.shouldStart = false;
|
|
635
710
|
debug('Starting on channels:');
|
|
636
711
|
this.channels.forEach(channel => {
|
|
637
|
-
debug(
|
|
712
|
+
debug(`- ${this.getChannelWithoutNamespace(channel)}`);
|
|
638
713
|
});
|
|
639
714
|
this.adapter.start();
|
|
640
715
|
}
|
|
641
|
-
|
|
642
716
|
stop() {
|
|
643
717
|
this.shouldStart = false;
|
|
644
|
-
|
|
645
718
|
if (!this.started && !this.starting) {
|
|
646
719
|
return;
|
|
647
720
|
}
|
|
648
|
-
|
|
649
721
|
debug('Stopping...');
|
|
650
|
-
|
|
651
722
|
if (this.adapter !== null) {
|
|
652
723
|
this.adapter.stop();
|
|
653
724
|
}
|
|
654
725
|
}
|
|
655
|
-
|
|
656
726
|
send(data, channel) {
|
|
657
727
|
if (!this.started) {
|
|
658
728
|
debug('Abort sending data: Not started');
|
|
659
729
|
return Promise.reject();
|
|
660
730
|
}
|
|
661
|
-
|
|
662
731
|
const publishData = typeof data.channel !== 'undefined' && typeof data.message !== 'undefined' ? data : {
|
|
663
732
|
channel: typeof channel !== 'undefined' ? this.getChannelWithNamespace(channel) : this.channels,
|
|
664
733
|
message: data
|
|
@@ -666,14 +735,12 @@ class Socket extends EventEmitter {
|
|
|
666
735
|
debug('Sending', publishData);
|
|
667
736
|
return this.adapter.send(publishData);
|
|
668
737
|
}
|
|
669
|
-
|
|
670
738
|
isStarted() {
|
|
671
739
|
return this.started;
|
|
672
740
|
}
|
|
673
|
-
|
|
674
741
|
}
|
|
675
|
-
|
|
676
|
-
|
|
742
|
+
Socket.adapters = {
|
|
743
|
+
...SocketAdapters
|
|
677
744
|
};
|
|
678
745
|
|
|
679
746
|
const SocketContext = /*#__PURE__*/React.createContext({
|
|
@@ -708,8 +775,7 @@ const defaultProps = {
|
|
|
708
775
|
autoStart: false,
|
|
709
776
|
children: null
|
|
710
777
|
};
|
|
711
|
-
|
|
712
|
-
const SocketContainer = function (_ref) {
|
|
778
|
+
function SocketContainer(_ref) {
|
|
713
779
|
let {
|
|
714
780
|
children,
|
|
715
781
|
socket,
|
|
@@ -721,9 +787,11 @@ const SocketContainer = function (_ref) {
|
|
|
721
787
|
publishKey,
|
|
722
788
|
subscribeKey,
|
|
723
789
|
secretKey,
|
|
724
|
-
channels: initialChannels
|
|
790
|
+
channels: initialChannels,
|
|
791
|
+
...props
|
|
725
792
|
} = _ref;
|
|
726
793
|
const finalSocket = useMemo(() => socket || new Socket({
|
|
794
|
+
...props,
|
|
727
795
|
adapter,
|
|
728
796
|
host,
|
|
729
797
|
namespace,
|
|
@@ -739,7 +807,8 @@ const SocketContainer = function (_ref) {
|
|
|
739
807
|
setChannels(newChannels);
|
|
740
808
|
}, [finalSocket, setChannels]);
|
|
741
809
|
const addToChannelsCount = useCallback(newChannels => {
|
|
742
|
-
channelsCountRef.current = newChannels.reduce((map, channel) => ({
|
|
810
|
+
channelsCountRef.current = newChannels.reduce((map, channel) => ({
|
|
811
|
+
...map,
|
|
743
812
|
[channel]: (map[channel] || 0) + 1
|
|
744
813
|
}), channelsCountRef.current);
|
|
745
814
|
updateChannels(Object.keys(channelsCountRef.current));
|
|
@@ -747,7 +816,8 @@ const SocketContainer = function (_ref) {
|
|
|
747
816
|
const removeToChannelsCount = useCallback(newChannels => {
|
|
748
817
|
channelsCountRef.current = newChannels.reduce((map, channel) => {
|
|
749
818
|
const newCount = (map[channel] || 0) - 1;
|
|
750
|
-
return newCount > 0 ? {
|
|
819
|
+
return newCount > 0 ? {
|
|
820
|
+
...map,
|
|
751
821
|
[channel]: newCount
|
|
752
822
|
} : map;
|
|
753
823
|
}, channelsCountRef.current);
|
|
@@ -763,11 +833,9 @@ const SocketContainer = function (_ref) {
|
|
|
763
833
|
}, [initialChannels, subscribe, unsubscribe]);
|
|
764
834
|
useEffect(() => {
|
|
765
835
|
finalSocket.init();
|
|
766
|
-
|
|
767
836
|
if (autoStart) {
|
|
768
837
|
finalSocket.start();
|
|
769
838
|
}
|
|
770
|
-
|
|
771
839
|
return () => {
|
|
772
840
|
finalSocket.destroy();
|
|
773
841
|
};
|
|
@@ -782,13 +850,11 @@ const SocketContainer = function (_ref) {
|
|
|
782
850
|
value: value,
|
|
783
851
|
children: children
|
|
784
852
|
});
|
|
785
|
-
}
|
|
786
|
-
|
|
853
|
+
}
|
|
787
854
|
SocketContainer.propTypes = propTypes;
|
|
788
855
|
SocketContainer.defaultProps = defaultProps;
|
|
789
856
|
|
|
790
857
|
const getDisplayName = WrappedComponent => WrappedComponent.displayName || WrappedComponent.name || 'Component';
|
|
791
|
-
|
|
792
858
|
const withSocket = WrappedComponent => {
|
|
793
859
|
const WithSocketComponent = props => /*#__PURE__*/jsx(SocketContext.Consumer, {
|
|
794
860
|
children: _ref => {
|
|
@@ -805,8 +871,7 @@ const withSocket = WrappedComponent => {
|
|
|
805
871
|
});
|
|
806
872
|
}
|
|
807
873
|
});
|
|
808
|
-
|
|
809
|
-
WithSocketComponent.displayName = "WithSocket(".concat(getDisplayName(WrappedComponent), ")");
|
|
874
|
+
WithSocketComponent.displayName = `WithSocket(${getDisplayName(WrappedComponent)})`;
|
|
810
875
|
return WithSocketComponent;
|
|
811
876
|
};
|
|
812
877
|
|
|
@@ -830,35 +895,25 @@ const useSocket = function () {
|
|
|
830
895
|
if (process.env.NODE_ENV !== 'production') {
|
|
831
896
|
console.warn('Socket context is empty.');
|
|
832
897
|
}
|
|
833
|
-
|
|
834
898
|
return () => {};
|
|
835
899
|
}
|
|
836
|
-
|
|
837
900
|
const wasStarted = socket.isStarted();
|
|
838
|
-
|
|
839
901
|
const onStarted = () => setStarted(true);
|
|
840
|
-
|
|
841
902
|
const onStop = () => setStarted(false);
|
|
842
|
-
|
|
843
903
|
socket.on('stop', onStop);
|
|
844
904
|
socket.on('started', onStarted);
|
|
845
|
-
|
|
846
905
|
if (channels !== null) {
|
|
847
906
|
subscribe(channels);
|
|
848
907
|
}
|
|
849
|
-
|
|
850
908
|
if (!wasStarted) {
|
|
851
909
|
socket.start();
|
|
852
910
|
}
|
|
853
|
-
|
|
854
911
|
return () => {
|
|
855
912
|
socket.off('stop', onStop);
|
|
856
913
|
socket.off('started', onStarted);
|
|
857
|
-
|
|
858
914
|
if (channels !== null) {
|
|
859
915
|
unsubscribe(channels);
|
|
860
916
|
}
|
|
861
|
-
|
|
862
917
|
if (!wasStarted) {
|
|
863
918
|
socket.stop();
|
|
864
919
|
}
|
|
@@ -868,13 +923,11 @@ const useSocket = function () {
|
|
|
868
923
|
if (socket === null) {
|
|
869
924
|
return () => {};
|
|
870
925
|
}
|
|
871
|
-
|
|
872
926
|
const onMessage = function () {
|
|
873
927
|
if (customOnMessage !== null) {
|
|
874
928
|
customOnMessage(...arguments);
|
|
875
929
|
}
|
|
876
930
|
};
|
|
877
|
-
|
|
878
931
|
socket.on('message', onMessage);
|
|
879
932
|
return () => {
|
|
880
933
|
socket.off('message', onMessage);
|