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