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