@folklore/socket 0.4.9 → 0.4.12

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