@folklore/socket 0.4.30 → 0.4.33
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/index.d.ts +129 -0
- package/dist/{es.js → index.js} +213 -239
- package/package.json +18 -10
- package/dist/cjs.js +0 -961
package/dist/{es.js → index.js}
RENAMED
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { EventEmitter } from '@folklore/events';
|
|
2
2
|
import invariant from 'invariant';
|
|
3
3
|
import isFunction from 'lodash/isFunction';
|
|
4
|
-
import EventEmitter from 'wolfy87-eventemitter';
|
|
5
4
|
import isString from 'lodash/isString';
|
|
5
|
+
import createDebug from 'debug';
|
|
6
6
|
import isArray from 'lodash/isArray';
|
|
7
|
-
import
|
|
8
|
-
import PropTypes from 'prop-types';
|
|
7
|
+
import { createContext, useMemo, useState, useRef, useCallback, useEffect, useContext } from 'react';
|
|
9
8
|
import { jsx } from 'react/jsx-runtime';
|
|
10
9
|
|
|
11
|
-
|
|
10
|
+
let debug = createDebug('folklore:socket');
|
|
11
|
+
function setDebug(newDebug) {
|
|
12
|
+
debug = isString(newDebug) ? createDebug(newDebug) : newDebug;
|
|
13
|
+
}
|
|
14
|
+
|
|
12
15
|
class PubNubSocket extends EventEmitter {
|
|
13
16
|
constructor(opts) {
|
|
14
17
|
super();
|
|
@@ -58,7 +61,7 @@ class PubNubSocket extends EventEmitter {
|
|
|
58
61
|
}
|
|
59
62
|
}
|
|
60
63
|
updateChannels(channels) {
|
|
61
|
-
debug
|
|
64
|
+
debug(`[PubNub] Updating channels: ${channels.join(', ')}`);
|
|
62
65
|
const {
|
|
63
66
|
shouldStart,
|
|
64
67
|
started,
|
|
@@ -77,13 +80,13 @@ class PubNubSocket extends EventEmitter {
|
|
|
77
80
|
if (this.pubnub !== null) {
|
|
78
81
|
return;
|
|
79
82
|
}
|
|
80
|
-
debug
|
|
83
|
+
debug('[PubNub] Init');
|
|
81
84
|
this.destroyed = false;
|
|
82
85
|
const loadPubnub = this.PubNub !== null ? Promise.resolve() : this.loadPubNub();
|
|
83
86
|
loadPubnub.then(() => this.createPubNub()).then(() => this.onReady());
|
|
84
87
|
}
|
|
85
88
|
loadPubNub() {
|
|
86
|
-
debug
|
|
89
|
+
debug('[PubNub] Load library');
|
|
87
90
|
return import('pubnub').then(({
|
|
88
91
|
default: PubNub
|
|
89
92
|
}) => {
|
|
@@ -94,6 +97,7 @@ class PubNubSocket extends EventEmitter {
|
|
|
94
97
|
if (this.destroyed) {
|
|
95
98
|
return;
|
|
96
99
|
}
|
|
100
|
+
debug('[PubNub] Create client');
|
|
97
101
|
const {
|
|
98
102
|
PubNub
|
|
99
103
|
} = this;
|
|
@@ -127,19 +131,19 @@ class PubNubSocket extends EventEmitter {
|
|
|
127
131
|
}
|
|
128
132
|
this.pubnub = null;
|
|
129
133
|
this.ready = false;
|
|
130
|
-
debug
|
|
134
|
+
debug('[PubNub] Destroyed.');
|
|
131
135
|
}
|
|
132
136
|
start() {
|
|
133
137
|
if (this.started) {
|
|
134
|
-
debug
|
|
138
|
+
debug('[PubNub] Skipping start: Already started.');
|
|
135
139
|
return;
|
|
136
140
|
}
|
|
137
141
|
if (this.starting) {
|
|
138
|
-
debug
|
|
142
|
+
debug('[PubNub] Skipping start: Already starting.');
|
|
139
143
|
return;
|
|
140
144
|
}
|
|
141
145
|
if (this.channels.length === 0) {
|
|
142
|
-
debug
|
|
146
|
+
debug('[PubNub] Skipping start: No channels.');
|
|
143
147
|
this.shouldStart = true;
|
|
144
148
|
return;
|
|
145
149
|
}
|
|
@@ -151,7 +155,7 @@ class PubNubSocket extends EventEmitter {
|
|
|
151
155
|
this.starting = true;
|
|
152
156
|
this.pubnub.subscribe({
|
|
153
157
|
channels: this.channels,
|
|
154
|
-
subscriptionOptions,
|
|
158
|
+
...(subscriptionOptions || {}),
|
|
155
159
|
withPresence
|
|
156
160
|
});
|
|
157
161
|
this.emit('start');
|
|
@@ -160,7 +164,7 @@ class PubNubSocket extends EventEmitter {
|
|
|
160
164
|
if (!this.started && !this.starting) {
|
|
161
165
|
return;
|
|
162
166
|
}
|
|
163
|
-
debug
|
|
167
|
+
debug('[PubNub] Stopping...');
|
|
164
168
|
this.shouldStart = false;
|
|
165
169
|
this.started = false;
|
|
166
170
|
this.starting = false;
|
|
@@ -170,7 +174,7 @@ class PubNubSocket extends EventEmitter {
|
|
|
170
174
|
this.emit('stop');
|
|
171
175
|
}
|
|
172
176
|
send(data) {
|
|
173
|
-
debug
|
|
177
|
+
debug('[PubNub] Sending', data);
|
|
174
178
|
return new Promise((resolve, reject) => {
|
|
175
179
|
this.pubnub.publish(data, (status, response) => {
|
|
176
180
|
if (status.error) {
|
|
@@ -187,160 +191,6 @@ class PubNubSocket extends EventEmitter {
|
|
|
187
191
|
}
|
|
188
192
|
}
|
|
189
193
|
|
|
190
|
-
const debug$2 = createDebug('folklore:socket:socketio');
|
|
191
|
-
class SocketIOSocket extends EventEmitter {
|
|
192
|
-
constructor(opts) {
|
|
193
|
-
super();
|
|
194
|
-
this.options = {
|
|
195
|
-
uuid: null,
|
|
196
|
-
host: 'http://127.0.0.1',
|
|
197
|
-
path: null,
|
|
198
|
-
query: null,
|
|
199
|
-
...opts
|
|
200
|
-
};
|
|
201
|
-
this.onReady = this.onReady.bind(this);
|
|
202
|
-
this.onConnect = this.onConnect.bind(this);
|
|
203
|
-
this.onMessage = this.onMessage.bind(this);
|
|
204
|
-
this.ready = false;
|
|
205
|
-
this.shouldStart = false;
|
|
206
|
-
this.started = false;
|
|
207
|
-
this.starting = false;
|
|
208
|
-
this.Manager = null;
|
|
209
|
-
this.io = null;
|
|
210
|
-
this.sockets = {};
|
|
211
|
-
this.channels = [];
|
|
212
|
-
this.init();
|
|
213
|
-
}
|
|
214
|
-
onReady() {
|
|
215
|
-
this.ready = true;
|
|
216
|
-
this.emit('ready');
|
|
217
|
-
}
|
|
218
|
-
onConnect(channel) {
|
|
219
|
-
debug$2('Socket connected on %s', channel);
|
|
220
|
-
if (!this.started) {
|
|
221
|
-
this.started = true;
|
|
222
|
-
this.starting = false;
|
|
223
|
-
this.emit('started');
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
onMessage(message, channel) {
|
|
227
|
-
debug$2('Message received on %s %o', channel, message);
|
|
228
|
-
this.emit('message', message, channel);
|
|
229
|
-
}
|
|
230
|
-
init() {
|
|
231
|
-
import('socket.io-client').then(({
|
|
232
|
-
default: IO
|
|
233
|
-
}) => {
|
|
234
|
-
this.Manager = IO.Manager;
|
|
235
|
-
}).then(() => this.createManager()).then(() => this.onReady()).then(() => {
|
|
236
|
-
if (this.shouldStart) {
|
|
237
|
-
this.start();
|
|
238
|
-
}
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
createManager() {
|
|
242
|
-
const {
|
|
243
|
-
Manager
|
|
244
|
-
} = this;
|
|
245
|
-
const {
|
|
246
|
-
host,
|
|
247
|
-
...opts
|
|
248
|
-
} = this.options;
|
|
249
|
-
this.io = new Manager(host, {
|
|
250
|
-
autoConnect: false,
|
|
251
|
-
...opts
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
updateChannels(channels) {
|
|
255
|
-
debug$2(`Updating channels: ${channels.join(', ')}`);
|
|
256
|
-
const {
|
|
257
|
-
shouldStart,
|
|
258
|
-
started,
|
|
259
|
-
starting
|
|
260
|
-
} = this;
|
|
261
|
-
if (started || starting) {
|
|
262
|
-
this.stop();
|
|
263
|
-
}
|
|
264
|
-
this.channels = channels;
|
|
265
|
-
if (started || starting || shouldStart) {
|
|
266
|
-
this.start();
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
start() {
|
|
270
|
-
if (this.started) {
|
|
271
|
-
debug$2('Skipping start: Already started.');
|
|
272
|
-
return;
|
|
273
|
-
}
|
|
274
|
-
if (this.starting) {
|
|
275
|
-
debug$2('Skipping start: Already starting.');
|
|
276
|
-
return;
|
|
277
|
-
}
|
|
278
|
-
if (this.io === null) {
|
|
279
|
-
debug$2('Socket.io not ready.');
|
|
280
|
-
this.shouldStart = true;
|
|
281
|
-
return;
|
|
282
|
-
}
|
|
283
|
-
if (this.channels.length === 0) {
|
|
284
|
-
debug$2('Skipping start: No channels.');
|
|
285
|
-
this.shouldStart = true;
|
|
286
|
-
return;
|
|
287
|
-
}
|
|
288
|
-
this.shouldStart = false;
|
|
289
|
-
this.starting = true;
|
|
290
|
-
this.sockets = this.channels.reduce((map, channel) => ({
|
|
291
|
-
...map,
|
|
292
|
-
[channel]: this.createSocket(channel)
|
|
293
|
-
}), {});
|
|
294
|
-
this.emit('start');
|
|
295
|
-
}
|
|
296
|
-
stop() {
|
|
297
|
-
if (!this.started && !this.starting) {
|
|
298
|
-
return;
|
|
299
|
-
}
|
|
300
|
-
debug$2('Stopping...');
|
|
301
|
-
this.shouldStart = false;
|
|
302
|
-
this.started = false;
|
|
303
|
-
this.starting = false;
|
|
304
|
-
Object.values(this.sockets).forEach(socket => this.stopSocket(socket));
|
|
305
|
-
this.emit('stop');
|
|
306
|
-
}
|
|
307
|
-
createSocket(channel) {
|
|
308
|
-
const socket = this.io.socket(`/${channel.replace(/^\//, '')}`);
|
|
309
|
-
socket.on('message', message => this.onMessage(message, channel));
|
|
310
|
-
socket.on('connect', () => this.onConnect(channel));
|
|
311
|
-
socket.open();
|
|
312
|
-
return socket;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
// eslint-disable-next-line class-methods-use-this
|
|
316
|
-
stopSocket(socket) {
|
|
317
|
-
socket.off('connect');
|
|
318
|
-
socket.off('message');
|
|
319
|
-
socket.close();
|
|
320
|
-
return socket;
|
|
321
|
-
}
|
|
322
|
-
destroy() {
|
|
323
|
-
this.stop();
|
|
324
|
-
this.sockets = {};
|
|
325
|
-
if (this.io !== null) {
|
|
326
|
-
this.io.close();
|
|
327
|
-
this.io = null;
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
send(data) {
|
|
331
|
-
const {
|
|
332
|
-
channel,
|
|
333
|
-
message
|
|
334
|
-
} = data;
|
|
335
|
-
const channels = !isArray(channel) ? [channel] : channel;
|
|
336
|
-
channels.forEach(ch => {
|
|
337
|
-
this.sockets[ch].send(message);
|
|
338
|
-
});
|
|
339
|
-
return Promise.resolve();
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
const debug$1 = createDebug('folklore:socket:pusher');
|
|
344
194
|
class PusherSocket extends EventEmitter {
|
|
345
195
|
constructor(opts) {
|
|
346
196
|
super();
|
|
@@ -384,7 +234,7 @@ class PusherSocket extends EventEmitter {
|
|
|
384
234
|
}
|
|
385
235
|
}
|
|
386
236
|
updateChannels(channels) {
|
|
387
|
-
debug
|
|
237
|
+
debug(`Updating channels: ${channels.join(', ')}`);
|
|
388
238
|
const {
|
|
389
239
|
shouldStart,
|
|
390
240
|
started,
|
|
@@ -403,13 +253,13 @@ class PusherSocket extends EventEmitter {
|
|
|
403
253
|
if (this.pusher !== null) {
|
|
404
254
|
return;
|
|
405
255
|
}
|
|
406
|
-
debug
|
|
256
|
+
debug('[Pusher] Init');
|
|
407
257
|
this.destroyed = false;
|
|
408
258
|
const loadPusher = this.Pusher !== null ? Promise.resolve() : this.loadPusher();
|
|
409
259
|
loadPusher.then(() => this.createPusher()).then(() => this.onReady());
|
|
410
260
|
}
|
|
411
261
|
loadPusher() {
|
|
412
|
-
debug
|
|
262
|
+
debug('[Pusher] Load Pusher');
|
|
413
263
|
return import('pusher-js').then(({
|
|
414
264
|
default: Pusher
|
|
415
265
|
}) => {
|
|
@@ -420,7 +270,7 @@ class PusherSocket extends EventEmitter {
|
|
|
420
270
|
if (this.destroyed) {
|
|
421
271
|
return;
|
|
422
272
|
}
|
|
423
|
-
debug
|
|
273
|
+
debug('[Pusher] Create Pusher appKey: %s', this.options.appKey);
|
|
424
274
|
const {
|
|
425
275
|
Pusher
|
|
426
276
|
} = this;
|
|
@@ -436,24 +286,24 @@ class PusherSocket extends EventEmitter {
|
|
|
436
286
|
this.pusher = null;
|
|
437
287
|
this.clients = {};
|
|
438
288
|
this.ready = false;
|
|
439
|
-
debug
|
|
289
|
+
debug('[Pusher] Destroyed.');
|
|
440
290
|
}
|
|
441
291
|
start() {
|
|
442
292
|
if (this.started) {
|
|
443
|
-
debug
|
|
293
|
+
debug('[Pusher] Skipping start: Already started.');
|
|
444
294
|
return;
|
|
445
295
|
}
|
|
446
296
|
if (this.starting) {
|
|
447
|
-
debug
|
|
297
|
+
debug('[Pusher] Skipping start: Already starting.');
|
|
448
298
|
return;
|
|
449
299
|
}
|
|
450
300
|
if (this.pusher === null) {
|
|
451
|
-
debug
|
|
301
|
+
debug('[Pusher] Socket.io not ready.');
|
|
452
302
|
this.shouldStart = true;
|
|
453
303
|
return;
|
|
454
304
|
}
|
|
455
305
|
if (this.channels.length === 0) {
|
|
456
|
-
debug
|
|
306
|
+
debug('[Pusher] Skipping start: No channels.');
|
|
457
307
|
this.shouldStart = true;
|
|
458
308
|
return;
|
|
459
309
|
}
|
|
@@ -470,7 +320,7 @@ class PusherSocket extends EventEmitter {
|
|
|
470
320
|
if (!this.started && !this.starting) {
|
|
471
321
|
return;
|
|
472
322
|
}
|
|
473
|
-
debug
|
|
323
|
+
debug('[Pusher] Stopping...');
|
|
474
324
|
this.shouldStart = false;
|
|
475
325
|
this.started = false;
|
|
476
326
|
this.starting = false;
|
|
@@ -483,28 +333,176 @@ class PusherSocket extends EventEmitter {
|
|
|
483
333
|
const channel = this.pusher.subscribe(channelName);
|
|
484
334
|
channel.bind_global((event, data) => this.onMessage({
|
|
485
335
|
event,
|
|
486
|
-
data
|
|
487
|
-
|
|
336
|
+
data,
|
|
337
|
+
channel
|
|
338
|
+
}));
|
|
488
339
|
return channel;
|
|
489
340
|
}
|
|
490
|
-
|
|
491
|
-
// eslint-disable-next-line class-methods-use-this
|
|
492
341
|
stopClient(channelName, channel) {
|
|
493
342
|
channel.unbind_global();
|
|
494
343
|
this.pusher.unsubscribe(channelName);
|
|
495
344
|
return channel;
|
|
496
345
|
}
|
|
497
346
|
send(data) {
|
|
498
|
-
debug
|
|
347
|
+
debug('[Pusher] Sending', data);
|
|
499
348
|
return new Promise(resolve => {
|
|
500
349
|
const {
|
|
501
350
|
channel,
|
|
502
351
|
event = null,
|
|
503
352
|
data: eventData
|
|
504
353
|
} = data;
|
|
505
|
-
this.pusher.
|
|
506
|
-
resolve();
|
|
354
|
+
this.pusher.send_event(event || 'message', eventData, channel);
|
|
355
|
+
resolve(data);
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
class SocketIOSocket extends EventEmitter {
|
|
361
|
+
constructor(opts) {
|
|
362
|
+
super();
|
|
363
|
+
this.options = {
|
|
364
|
+
uuid: null,
|
|
365
|
+
host: 'http://127.0.0.1',
|
|
366
|
+
path: null,
|
|
367
|
+
query: null,
|
|
368
|
+
...opts
|
|
369
|
+
};
|
|
370
|
+
this.onReady = this.onReady.bind(this);
|
|
371
|
+
this.onConnect = this.onConnect.bind(this);
|
|
372
|
+
this.onMessage = this.onMessage.bind(this);
|
|
373
|
+
this.ready = false;
|
|
374
|
+
this.shouldStart = false;
|
|
375
|
+
this.started = false;
|
|
376
|
+
this.starting = false;
|
|
377
|
+
this.Manager = null;
|
|
378
|
+
this.io = null;
|
|
379
|
+
this.sockets = {};
|
|
380
|
+
this.channels = [];
|
|
381
|
+
this.init();
|
|
382
|
+
}
|
|
383
|
+
onReady() {
|
|
384
|
+
this.ready = true;
|
|
385
|
+
this.emit('ready');
|
|
386
|
+
}
|
|
387
|
+
onConnect(channel) {
|
|
388
|
+
debug('[socket.io] Socket connected on %s', channel);
|
|
389
|
+
if (!this.started) {
|
|
390
|
+
this.started = true;
|
|
391
|
+
this.starting = false;
|
|
392
|
+
this.emit('started');
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
onMessage(message, channel) {
|
|
396
|
+
debug('[socket.io] Message received on %s %o', channel, message);
|
|
397
|
+
this.emit('message', message);
|
|
398
|
+
}
|
|
399
|
+
init() {
|
|
400
|
+
import('socket.io-client').then(({
|
|
401
|
+
Manager
|
|
402
|
+
}) => {
|
|
403
|
+
this.Manager = Manager;
|
|
404
|
+
}).then(() => this.createManager()).then(() => this.onReady()).then(() => {
|
|
405
|
+
if (this.shouldStart) {
|
|
406
|
+
this.start();
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
createManager() {
|
|
411
|
+
const {
|
|
412
|
+
Manager
|
|
413
|
+
} = this;
|
|
414
|
+
const {
|
|
415
|
+
host,
|
|
416
|
+
...opts
|
|
417
|
+
} = this.options;
|
|
418
|
+
this.io = new Manager(host, {
|
|
419
|
+
autoConnect: false,
|
|
420
|
+
...opts
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
updateChannels(channels) {
|
|
424
|
+
debug(`[socket.io] Updating channels: ${channels.join(', ')}`);
|
|
425
|
+
const {
|
|
426
|
+
shouldStart,
|
|
427
|
+
started,
|
|
428
|
+
starting
|
|
429
|
+
} = this;
|
|
430
|
+
if (started || starting) {
|
|
431
|
+
this.stop();
|
|
432
|
+
}
|
|
433
|
+
this.channels = channels;
|
|
434
|
+
if (started || starting || shouldStart) {
|
|
435
|
+
this.start();
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
start() {
|
|
439
|
+
if (this.started) {
|
|
440
|
+
debug('[socket.io] Skipping start: Already started.');
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
if (this.starting) {
|
|
444
|
+
debug('[socket.io] Skipping start: Already starting.');
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
if (this.io === null) {
|
|
448
|
+
debug('[socket.io] not ready.');
|
|
449
|
+
this.shouldStart = true;
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
if (this.channels.length === 0) {
|
|
453
|
+
debug('[socket.io] Skipping start: No channels.');
|
|
454
|
+
this.shouldStart = true;
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
this.shouldStart = false;
|
|
458
|
+
this.starting = true;
|
|
459
|
+
this.sockets = this.channels.reduce((map, channel) => ({
|
|
460
|
+
...map,
|
|
461
|
+
[channel]: this.createSocket(channel)
|
|
462
|
+
}), {});
|
|
463
|
+
this.emit('start');
|
|
464
|
+
}
|
|
465
|
+
stop() {
|
|
466
|
+
if (!this.started && !this.starting) {
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
debug('[socket.io] Stopping...');
|
|
470
|
+
this.shouldStart = false;
|
|
471
|
+
this.started = false;
|
|
472
|
+
this.starting = false;
|
|
473
|
+
Object.values(this.sockets).forEach(socket => this.stopSocket(socket));
|
|
474
|
+
this.emit('stop');
|
|
475
|
+
}
|
|
476
|
+
createSocket(channel) {
|
|
477
|
+
const socket = this.io.socket(`/${channel.replace(/^\//, '')}`);
|
|
478
|
+
socket.on('message', message => this.onMessage(message, channel));
|
|
479
|
+
socket.on('connect', () => this.onConnect(channel));
|
|
480
|
+
socket.open();
|
|
481
|
+
return socket;
|
|
482
|
+
}
|
|
483
|
+
stopSocket(socket) {
|
|
484
|
+
socket.off('connect');
|
|
485
|
+
socket.off('message');
|
|
486
|
+
socket.close();
|
|
487
|
+
return socket;
|
|
488
|
+
}
|
|
489
|
+
destroy() {
|
|
490
|
+
this.stop();
|
|
491
|
+
this.sockets = {};
|
|
492
|
+
if (this.io !== null) {
|
|
493
|
+
this.io = null;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
send(data) {
|
|
497
|
+
const {
|
|
498
|
+
channel,
|
|
499
|
+
message
|
|
500
|
+
} = data;
|
|
501
|
+
const channels = !isArray(channel) ? [channel] : channel;
|
|
502
|
+
channels.forEach(ch => {
|
|
503
|
+
this.sockets[ch].send(message);
|
|
507
504
|
});
|
|
505
|
+
return Promise.resolve();
|
|
508
506
|
}
|
|
509
507
|
}
|
|
510
508
|
|
|
@@ -515,8 +513,8 @@ var SocketAdapters = {
|
|
|
515
513
|
};
|
|
516
514
|
|
|
517
515
|
const normalize = str => str.replace(/[^a-z0-9]+/gi, '').toLowerCase();
|
|
518
|
-
const debug = createDebug('folklore:socket');
|
|
519
516
|
class Socket extends EventEmitter {
|
|
517
|
+
static adapters = {};
|
|
520
518
|
static getAdapters() {
|
|
521
519
|
return Socket.adapters;
|
|
522
520
|
}
|
|
@@ -668,6 +666,8 @@ class Socket extends EventEmitter {
|
|
|
668
666
|
return;
|
|
669
667
|
}
|
|
670
668
|
debug('Init');
|
|
669
|
+
|
|
670
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
671
671
|
const {
|
|
672
672
|
adapter: adapterKey,
|
|
673
673
|
channels,
|
|
@@ -752,49 +752,24 @@ Socket.adapters = {
|
|
|
752
752
|
...SocketAdapters
|
|
753
753
|
};
|
|
754
754
|
|
|
755
|
-
const SocketContext = /*#__PURE__*/
|
|
755
|
+
const SocketContext = /*#__PURE__*/createContext({
|
|
756
756
|
socket: null,
|
|
757
757
|
subscribe: () => {},
|
|
758
|
-
unsubscribe: () => {}
|
|
758
|
+
unsubscribe: () => {},
|
|
759
|
+
channels: []
|
|
759
760
|
});
|
|
760
761
|
|
|
761
|
-
const propTypes = {
|
|
762
|
-
socket: PropTypes.instanceOf(Socket),
|
|
763
|
-
adapter: PropTypes.string,
|
|
764
|
-
host: PropTypes.string,
|
|
765
|
-
namespace: PropTypes.string,
|
|
766
|
-
uuid: PropTypes.string,
|
|
767
|
-
publishKey: PropTypes.string,
|
|
768
|
-
subscribeKey: PropTypes.string,
|
|
769
|
-
secretKey: PropTypes.string,
|
|
770
|
-
channels: PropTypes.arrayOf(PropTypes.string),
|
|
771
|
-
autoStart: PropTypes.bool,
|
|
772
|
-
children: PropTypes.node
|
|
773
|
-
};
|
|
774
|
-
const defaultProps = {
|
|
775
|
-
socket: null,
|
|
776
|
-
adapter: 'pubnub',
|
|
777
|
-
host: null,
|
|
778
|
-
namespace: null,
|
|
779
|
-
uuid: null,
|
|
780
|
-
publishKey: null,
|
|
781
|
-
subscribeKey: null,
|
|
782
|
-
secretKey: null,
|
|
783
|
-
channels: [],
|
|
784
|
-
autoStart: false,
|
|
785
|
-
children: null
|
|
786
|
-
};
|
|
787
762
|
function SocketContainer({
|
|
788
|
-
children,
|
|
789
|
-
socket,
|
|
790
|
-
autoStart,
|
|
791
|
-
adapter,
|
|
792
|
-
host,
|
|
793
|
-
namespace,
|
|
794
|
-
uuid,
|
|
795
|
-
publishKey,
|
|
796
|
-
subscribeKey,
|
|
797
|
-
secretKey,
|
|
763
|
+
children = null,
|
|
764
|
+
socket = null,
|
|
765
|
+
autoStart = false,
|
|
766
|
+
adapter = 'pubnub',
|
|
767
|
+
host = null,
|
|
768
|
+
namespace = null,
|
|
769
|
+
uuid = null,
|
|
770
|
+
publishKey = null,
|
|
771
|
+
subscribeKey = null,
|
|
772
|
+
secretKey = null,
|
|
798
773
|
channels: initialChannels,
|
|
799
774
|
...props
|
|
800
775
|
}) {
|
|
@@ -835,14 +810,14 @@ function SocketContainer({
|
|
|
835
810
|
}, channelsCountRef.current);
|
|
836
811
|
updateChannels(Object.keys(channelsCountRef.current));
|
|
837
812
|
}, [updateChannels]);
|
|
838
|
-
const subscribe = useCallback(channelsToAdd => addToChannelsCount(channelsToAdd), [addToChannelsCount]);
|
|
839
|
-
const unsubscribe = useCallback(channelsToRemove => removeToChannelsCount(channelsToRemove), [removeToChannelsCount]);
|
|
813
|
+
const subscribe = useCallback(channelsToAdd => addToChannelsCount(isString(channelsToAdd) ? [channelsToAdd] : channelsToAdd), [addToChannelsCount]);
|
|
814
|
+
const unsubscribe = useCallback(channelsToRemove => removeToChannelsCount(isString(channelsToRemove) ? [channelsToRemove] : channelsToRemove), [removeToChannelsCount]);
|
|
840
815
|
useEffect(() => {
|
|
841
|
-
subscribe(initialChannels);
|
|
816
|
+
subscribe(initialChannels || []);
|
|
842
817
|
return () => {
|
|
843
|
-
unsubscribe(initialChannels);
|
|
818
|
+
unsubscribe(initialChannels || []);
|
|
844
819
|
};
|
|
845
|
-
}, [initialChannels, subscribe, unsubscribe]);
|
|
820
|
+
}, [initialChannels || [], subscribe, unsubscribe]);
|
|
846
821
|
useEffect(() => {
|
|
847
822
|
finalSocket.init();
|
|
848
823
|
if (autoStart) {
|
|
@@ -863,8 +838,6 @@ function SocketContainer({
|
|
|
863
838
|
children: children
|
|
864
839
|
});
|
|
865
840
|
}
|
|
866
|
-
SocketContainer.propTypes = propTypes;
|
|
867
|
-
SocketContainer.defaultProps = defaultProps;
|
|
868
841
|
|
|
869
842
|
const getDisplayName = WrappedComponent => WrappedComponent.displayName || WrappedComponent.name || 'Component';
|
|
870
843
|
const withSocket = WrappedComponent => {
|
|
@@ -884,11 +857,12 @@ const withSocket = WrappedComponent => {
|
|
|
884
857
|
return WithSocketComponent;
|
|
885
858
|
};
|
|
886
859
|
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
860
|
+
function useSocket(channelNames = null, opts = {}) {
|
|
861
|
+
const {
|
|
862
|
+
socket: customSocket = null,
|
|
863
|
+
onMessage: customOnMessage = null,
|
|
864
|
+
keepAlive = true
|
|
865
|
+
} = opts || {};
|
|
892
866
|
const {
|
|
893
867
|
socket: contextSocket,
|
|
894
868
|
subscribe,
|
|
@@ -947,6 +921,6 @@ const useSocket = (channelNames = null, {
|
|
|
947
921
|
subscribe,
|
|
948
922
|
unsubscribe
|
|
949
923
|
};
|
|
950
|
-
}
|
|
924
|
+
}
|
|
951
925
|
|
|
952
|
-
export { Socket, SocketContainer, SocketContext, Socket as default, useSocket, withSocket };
|
|
926
|
+
export { Socket, SocketContainer, SocketContext, debug, Socket as default, setDebug, useSocket, withSocket };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@folklore/socket",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.33",
|
|
4
4
|
"description": "Socket utilities",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"javascript",
|
|
@@ -27,26 +27,34 @@
|
|
|
27
27
|
"email": "nrb@folklore.email"
|
|
28
28
|
}
|
|
29
29
|
],
|
|
30
|
-
"
|
|
31
|
-
"module": "dist/
|
|
30
|
+
"type": "module",
|
|
31
|
+
"module": "dist/index.js",
|
|
32
|
+
"types": "dist/index.d.ts",
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"default": "./dist/index.js",
|
|
36
|
+
"types": "./dist/index.d.ts"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
32
39
|
"files": [
|
|
33
40
|
"dist"
|
|
34
41
|
],
|
|
35
42
|
"scripts": {
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
|
|
43
|
+
"build": "../../scripts/prepare-package.sh --types",
|
|
44
|
+
"prepublishOnly": "npm run build"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
39
48
|
},
|
|
40
49
|
"dependencies": {
|
|
41
50
|
"@babel/runtime": "^7.4.3",
|
|
51
|
+
"@folklore/events": "^0.0.12",
|
|
42
52
|
"debug": "^3.1.0",
|
|
43
53
|
"invariant": "^2.2.2",
|
|
44
54
|
"lodash": "^4.17.4",
|
|
45
|
-
"prop-types": "^15.6.0",
|
|
46
55
|
"pubnub": "^10.1.0",
|
|
47
56
|
"pusher-js": "^8.0.0",
|
|
48
|
-
"socket.io-client": "^4.1.2"
|
|
49
|
-
"wolfy87-eventemitter": "^5.2.4"
|
|
57
|
+
"socket.io-client": "^4.1.2"
|
|
50
58
|
},
|
|
51
59
|
"devDependencies": {
|
|
52
60
|
"react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
|
|
@@ -56,5 +64,5 @@
|
|
|
56
64
|
"react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
|
|
57
65
|
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
|
|
58
66
|
},
|
|
59
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "560661f2ec3b2be074323a5501b55ab452728168"
|
|
60
68
|
}
|