@folklore/socket 0.4.17 → 0.4.19
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 +196 -25
- package/dist/es.js +196 -25
- package/package.json +3 -2
package/dist/cjs.js
CHANGED
|
@@ -6,13 +6,13 @@ var createDebug = require('debug');
|
|
|
6
6
|
var invariant = require('invariant');
|
|
7
7
|
var isFunction = require('lodash/isFunction');
|
|
8
8
|
var EventEmitter = require('wolfy87-eventemitter');
|
|
9
|
+
var isString = require('lodash/isString');
|
|
9
10
|
var isArray = require('lodash/isArray');
|
|
10
11
|
var React = require('react');
|
|
11
12
|
var PropTypes = require('prop-types');
|
|
12
13
|
var jsxRuntime = require('react/jsx-runtime');
|
|
13
|
-
var isString = require('lodash/isString');
|
|
14
14
|
|
|
15
|
-
const debug$
|
|
15
|
+
const debug$3 = createDebug('folklore:socket:pubnub');
|
|
16
16
|
class PubNubSocket extends EventEmitter {
|
|
17
17
|
constructor(opts) {
|
|
18
18
|
super();
|
|
@@ -61,7 +61,7 @@ class PubNubSocket extends EventEmitter {
|
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
updateChannels(channels) {
|
|
64
|
-
debug$
|
|
64
|
+
debug$3(`Updating channels: ${channels.join(', ')}`);
|
|
65
65
|
const {
|
|
66
66
|
shouldStart,
|
|
67
67
|
started,
|
|
@@ -80,13 +80,13 @@ class PubNubSocket extends EventEmitter {
|
|
|
80
80
|
if (this.pubnub !== null) {
|
|
81
81
|
return;
|
|
82
82
|
}
|
|
83
|
-
debug$
|
|
83
|
+
debug$3('Init');
|
|
84
84
|
this.destroyed = false;
|
|
85
85
|
const loadPubnub = this.PubNub !== null ? Promise.resolve() : this.loadPubNub();
|
|
86
86
|
loadPubnub.then(() => this.createPubNub()).then(() => this.onReady());
|
|
87
87
|
}
|
|
88
88
|
loadPubNub() {
|
|
89
|
-
debug$
|
|
89
|
+
debug$3('Load PubNub');
|
|
90
90
|
return import('pubnub').then(_ref2 => {
|
|
91
91
|
let {
|
|
92
92
|
default: PubNub
|
|
@@ -127,19 +127,19 @@ class PubNubSocket extends EventEmitter {
|
|
|
127
127
|
}
|
|
128
128
|
this.pubnub = null;
|
|
129
129
|
this.ready = false;
|
|
130
|
-
debug$
|
|
130
|
+
debug$3('Destroyed.');
|
|
131
131
|
}
|
|
132
132
|
start() {
|
|
133
133
|
if (this.started) {
|
|
134
|
-
debug$
|
|
134
|
+
debug$3('Skipping start: Already started.');
|
|
135
135
|
return;
|
|
136
136
|
}
|
|
137
137
|
if (this.starting) {
|
|
138
|
-
debug$
|
|
138
|
+
debug$3('Skipping start: Already starting.');
|
|
139
139
|
return;
|
|
140
140
|
}
|
|
141
141
|
if (this.channels.length === 0) {
|
|
142
|
-
debug$
|
|
142
|
+
debug$3('Skipping start: No channels.');
|
|
143
143
|
this.shouldStart = true;
|
|
144
144
|
return;
|
|
145
145
|
}
|
|
@@ -154,7 +154,7 @@ class PubNubSocket extends EventEmitter {
|
|
|
154
154
|
if (!this.started && !this.starting) {
|
|
155
155
|
return;
|
|
156
156
|
}
|
|
157
|
-
debug$
|
|
157
|
+
debug$3('Stopping...');
|
|
158
158
|
this.shouldStart = false;
|
|
159
159
|
this.started = false;
|
|
160
160
|
this.starting = false;
|
|
@@ -164,7 +164,7 @@ class PubNubSocket extends EventEmitter {
|
|
|
164
164
|
this.emit('stop');
|
|
165
165
|
}
|
|
166
166
|
send(data) {
|
|
167
|
-
debug$
|
|
167
|
+
debug$3('Sending', data);
|
|
168
168
|
return new Promise((resolve, reject) => {
|
|
169
169
|
this.pubnub.publish(data, (status, response) => {
|
|
170
170
|
if (status.error) {
|
|
@@ -181,7 +181,7 @@ class PubNubSocket extends EventEmitter {
|
|
|
181
181
|
}
|
|
182
182
|
}
|
|
183
183
|
|
|
184
|
-
const debug$
|
|
184
|
+
const debug$2 = createDebug('folklore:socket:socketio');
|
|
185
185
|
class SocketIOSocket extends EventEmitter {
|
|
186
186
|
constructor(opts) {
|
|
187
187
|
super();
|
|
@@ -210,7 +210,7 @@ class SocketIOSocket extends EventEmitter {
|
|
|
210
210
|
this.emit('ready');
|
|
211
211
|
}
|
|
212
212
|
onConnect(channel) {
|
|
213
|
-
debug$
|
|
213
|
+
debug$2('Socket connected on %s', channel);
|
|
214
214
|
if (!this.started) {
|
|
215
215
|
this.started = true;
|
|
216
216
|
this.starting = false;
|
|
@@ -218,7 +218,7 @@ class SocketIOSocket extends EventEmitter {
|
|
|
218
218
|
}
|
|
219
219
|
}
|
|
220
220
|
onMessage(message, channel) {
|
|
221
|
-
debug$
|
|
221
|
+
debug$2('Message received on %s %o', channel, message);
|
|
222
222
|
this.emit('message', message, channel);
|
|
223
223
|
}
|
|
224
224
|
init() {
|
|
@@ -247,7 +247,7 @@ class SocketIOSocket extends EventEmitter {
|
|
|
247
247
|
});
|
|
248
248
|
}
|
|
249
249
|
updateChannels(channels) {
|
|
250
|
-
debug$
|
|
250
|
+
debug$2(`Updating channels: ${channels.join(', ')}`);
|
|
251
251
|
const {
|
|
252
252
|
shouldStart,
|
|
253
253
|
started,
|
|
@@ -263,20 +263,20 @@ class SocketIOSocket extends EventEmitter {
|
|
|
263
263
|
}
|
|
264
264
|
start() {
|
|
265
265
|
if (this.started) {
|
|
266
|
-
debug$
|
|
266
|
+
debug$2('Skipping start: Already started.');
|
|
267
267
|
return;
|
|
268
268
|
}
|
|
269
269
|
if (this.starting) {
|
|
270
|
-
debug$
|
|
270
|
+
debug$2('Skipping start: Already starting.');
|
|
271
271
|
return;
|
|
272
272
|
}
|
|
273
273
|
if (this.io === null) {
|
|
274
|
-
debug$
|
|
274
|
+
debug$2('Socket.io not ready.');
|
|
275
275
|
this.shouldStart = true;
|
|
276
276
|
return;
|
|
277
277
|
}
|
|
278
278
|
if (this.channels.length === 0) {
|
|
279
|
-
debug$
|
|
279
|
+
debug$2('Skipping start: No channels.');
|
|
280
280
|
this.shouldStart = true;
|
|
281
281
|
return;
|
|
282
282
|
}
|
|
@@ -292,7 +292,7 @@ class SocketIOSocket extends EventEmitter {
|
|
|
292
292
|
if (!this.started && !this.starting) {
|
|
293
293
|
return;
|
|
294
294
|
}
|
|
295
|
-
debug$
|
|
295
|
+
debug$2('Stopping...');
|
|
296
296
|
this.shouldStart = false;
|
|
297
297
|
this.started = false;
|
|
298
298
|
this.starting = false;
|
|
@@ -335,9 +335,178 @@ class SocketIOSocket extends EventEmitter {
|
|
|
335
335
|
}
|
|
336
336
|
}
|
|
337
337
|
|
|
338
|
+
const debug$1 = createDebug('folklore:socket:pusher');
|
|
339
|
+
class PusherSocker extends EventEmitter {
|
|
340
|
+
constructor(opts) {
|
|
341
|
+
super();
|
|
342
|
+
this.options = {
|
|
343
|
+
uuid: null,
|
|
344
|
+
publishKey: null,
|
|
345
|
+
subscribeKey: null,
|
|
346
|
+
secretKey: null,
|
|
347
|
+
...opts
|
|
348
|
+
};
|
|
349
|
+
this.onReady = this.onReady.bind(this);
|
|
350
|
+
this.onConnected = this.onConnected.bind(this);
|
|
351
|
+
this.onMessage = this.onMessage.bind(this);
|
|
352
|
+
this.destroyed = false;
|
|
353
|
+
this.ready = false;
|
|
354
|
+
this.shouldStart = false;
|
|
355
|
+
this.started = false;
|
|
356
|
+
this.starting = false;
|
|
357
|
+
this.Pusher = null;
|
|
358
|
+
this.pusher = null;
|
|
359
|
+
this.channels = [];
|
|
360
|
+
this.clients = {};
|
|
361
|
+
this.init();
|
|
362
|
+
}
|
|
363
|
+
onReady() {
|
|
364
|
+
if (this.destroyed) {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
this.ready = true;
|
|
368
|
+
this.emit('ready');
|
|
369
|
+
}
|
|
370
|
+
onConnected() {
|
|
371
|
+
this.started = true;
|
|
372
|
+
this.starting = false;
|
|
373
|
+
this.emit('started');
|
|
374
|
+
}
|
|
375
|
+
onMessage(message) {
|
|
376
|
+
this.emit('message', message);
|
|
377
|
+
if (typeof message.event !== 'undefined') {
|
|
378
|
+
this.emit(message.event, message.data || message);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
updateChannels(channels) {
|
|
382
|
+
debug$1(`Updating channels: ${channels.join(', ')}`);
|
|
383
|
+
const {
|
|
384
|
+
shouldStart,
|
|
385
|
+
started,
|
|
386
|
+
starting
|
|
387
|
+
} = this;
|
|
388
|
+
if (started || starting) {
|
|
389
|
+
this.stop();
|
|
390
|
+
}
|
|
391
|
+
this.channels = channels;
|
|
392
|
+
if (started || starting || shouldStart) {
|
|
393
|
+
this.shouldStart = false;
|
|
394
|
+
this.start();
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
init() {
|
|
398
|
+
if (this.pusher !== null) {
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
debug$1('Init');
|
|
402
|
+
this.destroyed = false;
|
|
403
|
+
const loadPusher = this.Pusher !== null ? Promise.resolve() : this.loadPusher();
|
|
404
|
+
loadPusher.then(() => this.createPusher()).then(() => this.onReady());
|
|
405
|
+
}
|
|
406
|
+
loadPusher() {
|
|
407
|
+
debug$1('Load PusherJs');
|
|
408
|
+
return import('pusher-js').then(_ref => {
|
|
409
|
+
let {
|
|
410
|
+
default: Pusher
|
|
411
|
+
} = _ref;
|
|
412
|
+
this.Pusher = Pusher;
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
createPusher() {
|
|
416
|
+
if (this.destroyed) {
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
const {
|
|
420
|
+
Pusher
|
|
421
|
+
} = this;
|
|
422
|
+
const {
|
|
423
|
+
appKey,
|
|
424
|
+
...options
|
|
425
|
+
} = this.options;
|
|
426
|
+
this.pusher = new Pusher(appKey, options);
|
|
427
|
+
}
|
|
428
|
+
destroy() {
|
|
429
|
+
this.destroyed = true;
|
|
430
|
+
this.stop();
|
|
431
|
+
this.pusher = null;
|
|
432
|
+
this.clients = {};
|
|
433
|
+
this.ready = false;
|
|
434
|
+
debug$1('Destroyed.');
|
|
435
|
+
}
|
|
436
|
+
start() {
|
|
437
|
+
if (this.started) {
|
|
438
|
+
debug$1('Skipping start: Already started.');
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
if (this.starting) {
|
|
442
|
+
debug$1('Skipping start: Already starting.');
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
if (this.io === null) {
|
|
446
|
+
debug$1('Socket.io not ready.');
|
|
447
|
+
this.shouldStart = true;
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
if (this.channels.length === 0) {
|
|
451
|
+
debug$1('Skipping start: No channels.');
|
|
452
|
+
this.shouldStart = true;
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
this.shouldStart = false;
|
|
456
|
+
this.starting = true;
|
|
457
|
+
this.pusher.connection.bind('connected', this.onConnected);
|
|
458
|
+
this.clients = this.channels.reduce((map, channel) => ({
|
|
459
|
+
...map,
|
|
460
|
+
[channel]: this.createClient(channel)
|
|
461
|
+
}), {});
|
|
462
|
+
this.emit('start');
|
|
463
|
+
}
|
|
464
|
+
stop() {
|
|
465
|
+
if (!this.started && !this.starting) {
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
debug$1('Stopping...');
|
|
469
|
+
this.shouldStart = false;
|
|
470
|
+
this.started = false;
|
|
471
|
+
this.starting = false;
|
|
472
|
+
this.pusher.connection.unbind('connected');
|
|
473
|
+
Object.keys(this.clients).forEach(channelName => this.stopClient(channelName, this.clients[channelName]));
|
|
474
|
+
this.clients = {};
|
|
475
|
+
this.emit('stop');
|
|
476
|
+
}
|
|
477
|
+
createClient(channelName) {
|
|
478
|
+
const channel = this.pusher.subscribe(channelName);
|
|
479
|
+
channel.bind_global((event, data) => this.onMessage({
|
|
480
|
+
event,
|
|
481
|
+
data
|
|
482
|
+
}, channel));
|
|
483
|
+
return channel;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// eslint-disable-next-line class-methods-use-this
|
|
487
|
+
stopClient(channelName, channel) {
|
|
488
|
+
channel.unbind_global();
|
|
489
|
+
this.pusher.unsubscribe(channelName);
|
|
490
|
+
return channel;
|
|
491
|
+
}
|
|
492
|
+
send(data) {
|
|
493
|
+
debug$1('Sending', data);
|
|
494
|
+
return new Promise(resolve => {
|
|
495
|
+
const {
|
|
496
|
+
channel,
|
|
497
|
+
event = null,
|
|
498
|
+
data: eventData
|
|
499
|
+
} = data;
|
|
500
|
+
this.pusher.trigger(channel, event || 'message', eventData);
|
|
501
|
+
resolve();
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
338
506
|
var SocketAdapters = {
|
|
339
507
|
PubNub: PubNubSocket,
|
|
340
|
-
SocketIO: SocketIOSocket
|
|
508
|
+
SocketIO: SocketIOSocket,
|
|
509
|
+
Pusher: PusherSocker
|
|
341
510
|
};
|
|
342
511
|
|
|
343
512
|
const normalize = str => str.replace(/[^a-z0-9]+/gi, '').toLowerCase();
|
|
@@ -499,7 +668,7 @@ class Socket extends EventEmitter {
|
|
|
499
668
|
channels,
|
|
500
669
|
...adapterOptions
|
|
501
670
|
} = this.options;
|
|
502
|
-
const SocketAdapter = Socket.getAdapter(adapterKey);
|
|
671
|
+
const SocketAdapter = isString(adapterKey) ? Socket.getAdapter(adapterKey) : adapterKey;
|
|
503
672
|
this.adapter = new SocketAdapter(adapterOptions);
|
|
504
673
|
const methods = ['start', 'stop', 'destroy', 'updateChannels', 'send'];
|
|
505
674
|
methods.forEach(method => {
|
|
@@ -610,7 +779,7 @@ const defaultProps = {
|
|
|
610
779
|
autoStart: false,
|
|
611
780
|
children: null
|
|
612
781
|
};
|
|
613
|
-
|
|
782
|
+
function SocketContainer(_ref) {
|
|
614
783
|
let {
|
|
615
784
|
children,
|
|
616
785
|
socket,
|
|
@@ -622,9 +791,11 @@ const SocketContainer = function (_ref) {
|
|
|
622
791
|
publishKey,
|
|
623
792
|
subscribeKey,
|
|
624
793
|
secretKey,
|
|
625
|
-
channels: initialChannels
|
|
794
|
+
channels: initialChannels,
|
|
795
|
+
...props
|
|
626
796
|
} = _ref;
|
|
627
797
|
const finalSocket = React.useMemo(() => socket || new Socket({
|
|
798
|
+
...props,
|
|
628
799
|
adapter,
|
|
629
800
|
host,
|
|
630
801
|
namespace,
|
|
@@ -683,7 +854,7 @@ const SocketContainer = function (_ref) {
|
|
|
683
854
|
value: value,
|
|
684
855
|
children: children
|
|
685
856
|
});
|
|
686
|
-
}
|
|
857
|
+
}
|
|
687
858
|
SocketContainer.propTypes = propTypes;
|
|
688
859
|
SocketContainer.defaultProps = defaultProps;
|
|
689
860
|
|
package/dist/es.js
CHANGED
|
@@ -2,13 +2,13 @@ import createDebug from 'debug';
|
|
|
2
2
|
import invariant from 'invariant';
|
|
3
3
|
import isFunction from 'lodash/isFunction';
|
|
4
4
|
import EventEmitter from 'wolfy87-eventemitter';
|
|
5
|
+
import isString from 'lodash/isString';
|
|
5
6
|
import isArray from 'lodash/isArray';
|
|
6
7
|
import React, { useMemo, useState, useRef, useCallback, useEffect, useContext } from 'react';
|
|
7
8
|
import PropTypes from 'prop-types';
|
|
8
9
|
import { jsx } from 'react/jsx-runtime';
|
|
9
|
-
import isString from 'lodash/isString';
|
|
10
10
|
|
|
11
|
-
const debug$
|
|
11
|
+
const debug$3 = createDebug('folklore:socket:pubnub');
|
|
12
12
|
class PubNubSocket extends EventEmitter {
|
|
13
13
|
constructor(opts) {
|
|
14
14
|
super();
|
|
@@ -57,7 +57,7 @@ class PubNubSocket extends EventEmitter {
|
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
updateChannels(channels) {
|
|
60
|
-
debug$
|
|
60
|
+
debug$3(`Updating channels: ${channels.join(', ')}`);
|
|
61
61
|
const {
|
|
62
62
|
shouldStart,
|
|
63
63
|
started,
|
|
@@ -76,13 +76,13 @@ class PubNubSocket extends EventEmitter {
|
|
|
76
76
|
if (this.pubnub !== null) {
|
|
77
77
|
return;
|
|
78
78
|
}
|
|
79
|
-
debug$
|
|
79
|
+
debug$3('Init');
|
|
80
80
|
this.destroyed = false;
|
|
81
81
|
const loadPubnub = this.PubNub !== null ? Promise.resolve() : this.loadPubNub();
|
|
82
82
|
loadPubnub.then(() => this.createPubNub()).then(() => this.onReady());
|
|
83
83
|
}
|
|
84
84
|
loadPubNub() {
|
|
85
|
-
debug$
|
|
85
|
+
debug$3('Load PubNub');
|
|
86
86
|
return import('pubnub').then(_ref2 => {
|
|
87
87
|
let {
|
|
88
88
|
default: PubNub
|
|
@@ -123,19 +123,19 @@ class PubNubSocket extends EventEmitter {
|
|
|
123
123
|
}
|
|
124
124
|
this.pubnub = null;
|
|
125
125
|
this.ready = false;
|
|
126
|
-
debug$
|
|
126
|
+
debug$3('Destroyed.');
|
|
127
127
|
}
|
|
128
128
|
start() {
|
|
129
129
|
if (this.started) {
|
|
130
|
-
debug$
|
|
130
|
+
debug$3('Skipping start: Already started.');
|
|
131
131
|
return;
|
|
132
132
|
}
|
|
133
133
|
if (this.starting) {
|
|
134
|
-
debug$
|
|
134
|
+
debug$3('Skipping start: Already starting.');
|
|
135
135
|
return;
|
|
136
136
|
}
|
|
137
137
|
if (this.channels.length === 0) {
|
|
138
|
-
debug$
|
|
138
|
+
debug$3('Skipping start: No channels.');
|
|
139
139
|
this.shouldStart = true;
|
|
140
140
|
return;
|
|
141
141
|
}
|
|
@@ -150,7 +150,7 @@ class PubNubSocket extends EventEmitter {
|
|
|
150
150
|
if (!this.started && !this.starting) {
|
|
151
151
|
return;
|
|
152
152
|
}
|
|
153
|
-
debug$
|
|
153
|
+
debug$3('Stopping...');
|
|
154
154
|
this.shouldStart = false;
|
|
155
155
|
this.started = false;
|
|
156
156
|
this.starting = false;
|
|
@@ -160,7 +160,7 @@ class PubNubSocket extends EventEmitter {
|
|
|
160
160
|
this.emit('stop');
|
|
161
161
|
}
|
|
162
162
|
send(data) {
|
|
163
|
-
debug$
|
|
163
|
+
debug$3('Sending', data);
|
|
164
164
|
return new Promise((resolve, reject) => {
|
|
165
165
|
this.pubnub.publish(data, (status, response) => {
|
|
166
166
|
if (status.error) {
|
|
@@ -177,7 +177,7 @@ class PubNubSocket extends EventEmitter {
|
|
|
177
177
|
}
|
|
178
178
|
}
|
|
179
179
|
|
|
180
|
-
const debug$
|
|
180
|
+
const debug$2 = createDebug('folklore:socket:socketio');
|
|
181
181
|
class SocketIOSocket extends EventEmitter {
|
|
182
182
|
constructor(opts) {
|
|
183
183
|
super();
|
|
@@ -206,7 +206,7 @@ class SocketIOSocket extends EventEmitter {
|
|
|
206
206
|
this.emit('ready');
|
|
207
207
|
}
|
|
208
208
|
onConnect(channel) {
|
|
209
|
-
debug$
|
|
209
|
+
debug$2('Socket connected on %s', channel);
|
|
210
210
|
if (!this.started) {
|
|
211
211
|
this.started = true;
|
|
212
212
|
this.starting = false;
|
|
@@ -214,7 +214,7 @@ class SocketIOSocket extends EventEmitter {
|
|
|
214
214
|
}
|
|
215
215
|
}
|
|
216
216
|
onMessage(message, channel) {
|
|
217
|
-
debug$
|
|
217
|
+
debug$2('Message received on %s %o', channel, message);
|
|
218
218
|
this.emit('message', message, channel);
|
|
219
219
|
}
|
|
220
220
|
init() {
|
|
@@ -243,7 +243,7 @@ class SocketIOSocket extends EventEmitter {
|
|
|
243
243
|
});
|
|
244
244
|
}
|
|
245
245
|
updateChannels(channels) {
|
|
246
|
-
debug$
|
|
246
|
+
debug$2(`Updating channels: ${channels.join(', ')}`);
|
|
247
247
|
const {
|
|
248
248
|
shouldStart,
|
|
249
249
|
started,
|
|
@@ -259,20 +259,20 @@ class SocketIOSocket extends EventEmitter {
|
|
|
259
259
|
}
|
|
260
260
|
start() {
|
|
261
261
|
if (this.started) {
|
|
262
|
-
debug$
|
|
262
|
+
debug$2('Skipping start: Already started.');
|
|
263
263
|
return;
|
|
264
264
|
}
|
|
265
265
|
if (this.starting) {
|
|
266
|
-
debug$
|
|
266
|
+
debug$2('Skipping start: Already starting.');
|
|
267
267
|
return;
|
|
268
268
|
}
|
|
269
269
|
if (this.io === null) {
|
|
270
|
-
debug$
|
|
270
|
+
debug$2('Socket.io not ready.');
|
|
271
271
|
this.shouldStart = true;
|
|
272
272
|
return;
|
|
273
273
|
}
|
|
274
274
|
if (this.channels.length === 0) {
|
|
275
|
-
debug$
|
|
275
|
+
debug$2('Skipping start: No channels.');
|
|
276
276
|
this.shouldStart = true;
|
|
277
277
|
return;
|
|
278
278
|
}
|
|
@@ -288,7 +288,7 @@ class SocketIOSocket extends EventEmitter {
|
|
|
288
288
|
if (!this.started && !this.starting) {
|
|
289
289
|
return;
|
|
290
290
|
}
|
|
291
|
-
debug$
|
|
291
|
+
debug$2('Stopping...');
|
|
292
292
|
this.shouldStart = false;
|
|
293
293
|
this.started = false;
|
|
294
294
|
this.starting = false;
|
|
@@ -331,9 +331,178 @@ class SocketIOSocket extends EventEmitter {
|
|
|
331
331
|
}
|
|
332
332
|
}
|
|
333
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
|
+
}
|
|
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
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
334
502
|
var SocketAdapters = {
|
|
335
503
|
PubNub: PubNubSocket,
|
|
336
|
-
SocketIO: SocketIOSocket
|
|
504
|
+
SocketIO: SocketIOSocket,
|
|
505
|
+
Pusher: PusherSocker
|
|
337
506
|
};
|
|
338
507
|
|
|
339
508
|
const normalize = str => str.replace(/[^a-z0-9]+/gi, '').toLowerCase();
|
|
@@ -495,7 +664,7 @@ class Socket extends EventEmitter {
|
|
|
495
664
|
channels,
|
|
496
665
|
...adapterOptions
|
|
497
666
|
} = this.options;
|
|
498
|
-
const SocketAdapter = Socket.getAdapter(adapterKey);
|
|
667
|
+
const SocketAdapter = isString(adapterKey) ? Socket.getAdapter(adapterKey) : adapterKey;
|
|
499
668
|
this.adapter = new SocketAdapter(adapterOptions);
|
|
500
669
|
const methods = ['start', 'stop', 'destroy', 'updateChannels', 'send'];
|
|
501
670
|
methods.forEach(method => {
|
|
@@ -606,7 +775,7 @@ const defaultProps = {
|
|
|
606
775
|
autoStart: false,
|
|
607
776
|
children: null
|
|
608
777
|
};
|
|
609
|
-
|
|
778
|
+
function SocketContainer(_ref) {
|
|
610
779
|
let {
|
|
611
780
|
children,
|
|
612
781
|
socket,
|
|
@@ -618,9 +787,11 @@ const SocketContainer = function (_ref) {
|
|
|
618
787
|
publishKey,
|
|
619
788
|
subscribeKey,
|
|
620
789
|
secretKey,
|
|
621
|
-
channels: initialChannels
|
|
790
|
+
channels: initialChannels,
|
|
791
|
+
...props
|
|
622
792
|
} = _ref;
|
|
623
793
|
const finalSocket = useMemo(() => socket || new Socket({
|
|
794
|
+
...props,
|
|
624
795
|
adapter,
|
|
625
796
|
host,
|
|
626
797
|
namespace,
|
|
@@ -679,7 +850,7 @@ const SocketContainer = function (_ref) {
|
|
|
679
850
|
value: value,
|
|
680
851
|
children: children
|
|
681
852
|
});
|
|
682
|
-
}
|
|
853
|
+
}
|
|
683
854
|
SocketContainer.propTypes = propTypes;
|
|
684
855
|
SocketContainer.defaultProps = defaultProps;
|
|
685
856
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@folklore/socket",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.19",
|
|
4
4
|
"description": "Socket utilities",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"javascript",
|
|
@@ -44,6 +44,7 @@
|
|
|
44
44
|
"lodash": "^4.17.4",
|
|
45
45
|
"prop-types": "^15.6.0",
|
|
46
46
|
"pubnub": "^4.18.0",
|
|
47
|
+
"pusher-js": "^8.0.0",
|
|
47
48
|
"socket.io-client": "^4.1.2",
|
|
48
49
|
"wolfy87-eventemitter": "^5.2.4"
|
|
49
50
|
},
|
|
@@ -55,5 +56,5 @@
|
|
|
55
56
|
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
|
|
56
57
|
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0"
|
|
57
58
|
},
|
|
58
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "1f1b7ab751c4497df25a3dda97cd84f793ff720f"
|
|
59
60
|
}
|