@folklore/socket 0.4.1 → 0.4.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs.js +1100 -0
- package/dist/es.js +1052 -0
- package/package.json +8 -17
- package/dist/1.chunk.js +0 -1
- package/dist/2.chunk.js +0 -1
- package/dist/3.chunk.js +0 -1
- package/dist/socket.js +0 -1
- package/es/Socket.js +0 -372
- package/es/SocketContainer.js +0 -122
- package/es/SocketContext.js +0 -7
- package/es/adapters/PubNub.js +0 -213
- package/es/adapters/SocketIO.js +0 -226
- package/es/adapters/index.js +0 -6
- package/es/index.js +0 -5
- package/es/useSocket.js +0 -96
- package/es/withSocket.js +0 -27
- package/lib/Socket.js +0 -390
- package/lib/SocketContainer.js +0 -139
- package/lib/SocketContext.js +0 -19
- package/lib/adapters/PubNub.js +0 -233
- package/lib/adapters/SocketIO.js +0 -246
- package/lib/adapters/index.js +0 -18
- package/lib/index.js +0 -53
- package/lib/useSocket.js +0 -109
- package/lib/withSocket.js +0 -39
package/dist/es.js
ADDED
|
@@ -0,0 +1,1052 @@
|
|
|
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
|
+
import createDebug from 'debug';
|
|
11
|
+
import EventEmitter from 'wolfy87-eventemitter';
|
|
12
|
+
import invariant from 'invariant';
|
|
13
|
+
import isFunction from 'lodash/isFunction';
|
|
14
|
+
import isArray from 'lodash/isArray';
|
|
15
|
+
import React, { useMemo, useState, useRef, useCallback, useEffect, useContext } from 'react';
|
|
16
|
+
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
17
|
+
import PropTypes from 'prop-types';
|
|
18
|
+
import { jsx } from 'react/jsx-runtime';
|
|
19
|
+
import isString from 'lodash/isString';
|
|
20
|
+
|
|
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; }
|
|
22
|
+
|
|
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({
|
|
42
|
+
uuid: null,
|
|
43
|
+
publishKey: null,
|
|
44
|
+
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;
|
|
62
|
+
}
|
|
63
|
+
|
|
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
|
+
}
|
|
88
|
+
}
|
|
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
|
+
}
|
|
100
|
+
|
|
101
|
+
this.channels = channels;
|
|
102
|
+
|
|
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
|
+
});
|
|
121
|
+
}
|
|
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
|
+
}
|
|
134
|
+
|
|
135
|
+
if (this.options.secretKey !== null) {
|
|
136
|
+
pubnubOptions.secretKey = this.options.secretKey;
|
|
137
|
+
}
|
|
138
|
+
|
|
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() {
|
|
149
|
+
this.stop();
|
|
150
|
+
|
|
151
|
+
if (this.pubnubListener) {
|
|
152
|
+
this.pubnub.removeListener(this.pubnubListener);
|
|
153
|
+
this.pubnubListener = null;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
this.ready = false;
|
|
157
|
+
debug$2('Destroyed.');
|
|
158
|
+
}
|
|
159
|
+
}, {
|
|
160
|
+
key: "start",
|
|
161
|
+
value: function start() {
|
|
162
|
+
if (this.started) {
|
|
163
|
+
debug$2('Skipping start: Already started.');
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (this.starting) {
|
|
168
|
+
debug$2('Skipping start: Already starting.');
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (this.channels.length === 0) {
|
|
173
|
+
debug$2('Skipping start: No channels.');
|
|
174
|
+
this.shouldStart = true;
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
this.shouldStart = false;
|
|
179
|
+
this.starting = true;
|
|
180
|
+
this.pubnub.subscribe({
|
|
181
|
+
channels: this.channels
|
|
182
|
+
});
|
|
183
|
+
this.emit('start');
|
|
184
|
+
}
|
|
185
|
+
}, {
|
|
186
|
+
key: "stop",
|
|
187
|
+
value: function stop() {
|
|
188
|
+
if (!this.started && !this.starting) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
|
|
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');
|
|
200
|
+
}
|
|
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
|
+
});
|
|
220
|
+
}
|
|
221
|
+
}]);
|
|
222
|
+
|
|
223
|
+
return PubNubSocket;
|
|
224
|
+
}(EventEmitter);
|
|
225
|
+
|
|
226
|
+
var _excluded$1 = ["host"];
|
|
227
|
+
|
|
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; }
|
|
229
|
+
|
|
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; }
|
|
231
|
+
|
|
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); }; }
|
|
233
|
+
|
|
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');
|
|
236
|
+
|
|
237
|
+
var SocketIOSocket = /*#__PURE__*/function (_EventEmitter) {
|
|
238
|
+
_inherits(SocketIOSocket, _EventEmitter);
|
|
239
|
+
|
|
240
|
+
var _super = _createSuper$1(SocketIOSocket);
|
|
241
|
+
|
|
242
|
+
function SocketIOSocket(opts) {
|
|
243
|
+
var _this;
|
|
244
|
+
|
|
245
|
+
_classCallCheck(this, SocketIOSocket);
|
|
246
|
+
|
|
247
|
+
_this = _super.call(this);
|
|
248
|
+
_this.options = _objectSpread$3({
|
|
249
|
+
uuid: null,
|
|
250
|
+
host: 'http://127.0.0.1',
|
|
251
|
+
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;
|
|
269
|
+
}
|
|
270
|
+
|
|
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));
|
|
324
|
+
}
|
|
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
|
+
}
|
|
336
|
+
|
|
337
|
+
this.channels = channels;
|
|
338
|
+
|
|
339
|
+
if (started || starting || shouldStart) {
|
|
340
|
+
this.start();
|
|
341
|
+
}
|
|
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
|
+
}
|
|
352
|
+
|
|
353
|
+
if (this.starting) {
|
|
354
|
+
debug$1('Skipping start: Already starting.');
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
if (this.io === null) {
|
|
359
|
+
debug$1('Socket.io not ready.');
|
|
360
|
+
this.shouldStart = true;
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
if (this.channels.length === 0) {
|
|
365
|
+
debug$1('Skipping start: No channels.');
|
|
366
|
+
this.shouldStart = true;
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
|
|
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');
|
|
376
|
+
}
|
|
377
|
+
}, {
|
|
378
|
+
key: "stop",
|
|
379
|
+
value: function stop() {
|
|
380
|
+
var _this4 = this;
|
|
381
|
+
|
|
382
|
+
if (!this.started && !this.starting) {
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
|
|
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');
|
|
394
|
+
}
|
|
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;
|
|
418
|
+
}
|
|
419
|
+
}, {
|
|
420
|
+
key: "destroy",
|
|
421
|
+
value: function destroy() {
|
|
422
|
+
this.stop();
|
|
423
|
+
this.sockets = {};
|
|
424
|
+
|
|
425
|
+
if (this.io !== null) {
|
|
426
|
+
this.io.close();
|
|
427
|
+
this.io = null;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
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();
|
|
442
|
+
}
|
|
443
|
+
}]);
|
|
444
|
+
|
|
445
|
+
return SocketIOSocket;
|
|
446
|
+
}(EventEmitter);
|
|
447
|
+
|
|
448
|
+
var SocketAdapters = {
|
|
449
|
+
PubNub: PubNubSocket,
|
|
450
|
+
SocketIO: SocketIOSocket
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
var _excluded = ["adapter", "channels"];
|
|
454
|
+
|
|
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; }
|
|
456
|
+
|
|
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; }
|
|
458
|
+
|
|
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); }; }
|
|
460
|
+
|
|
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; } }
|
|
462
|
+
|
|
463
|
+
var normalize = function normalize(str) {
|
|
464
|
+
return str.replace(/[^a-z0-9]+/gi, '').toLowerCase();
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
var debug = createDebug('folklore:socket');
|
|
468
|
+
|
|
469
|
+
var Socket = /*#__PURE__*/function (_EventEmitter) {
|
|
470
|
+
_inherits(Socket, _EventEmitter);
|
|
471
|
+
|
|
472
|
+
var _super = _createSuper(Socket);
|
|
473
|
+
|
|
474
|
+
function Socket(opts) {
|
|
475
|
+
var _this;
|
|
476
|
+
|
|
477
|
+
_classCallCheck(this, Socket);
|
|
478
|
+
|
|
479
|
+
_this = _super.call(this);
|
|
480
|
+
_this.options = _objectSpread$2({
|
|
481
|
+
adapter: 'pubnub',
|
|
482
|
+
namespace: null,
|
|
483
|
+
uuid: null,
|
|
484
|
+
publishKey: null,
|
|
485
|
+
subscribeKey: null,
|
|
486
|
+
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);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
return _this;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
_createClass(Socket, [{
|
|
511
|
+
key: "onAdapterReady",
|
|
512
|
+
value: function onAdapterReady() {
|
|
513
|
+
debug('Adapter ready');
|
|
514
|
+
this.ready = true;
|
|
515
|
+
this.emit('ready');
|
|
516
|
+
|
|
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);
|
|
551
|
+
}
|
|
552
|
+
}, {
|
|
553
|
+
key: "getChannelWithoutNamespace",
|
|
554
|
+
value: function getChannelWithoutNamespace(name) {
|
|
555
|
+
if (this.options.namespace === null) {
|
|
556
|
+
return name;
|
|
557
|
+
}
|
|
558
|
+
|
|
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 = [];
|
|
566
|
+
|
|
567
|
+
if (this.options.namespace !== null) {
|
|
568
|
+
parts.push(this.options.namespace);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
parts.push(name);
|
|
572
|
+
return parts.join(':');
|
|
573
|
+
}
|
|
574
|
+
}, {
|
|
575
|
+
key: "setChannels",
|
|
576
|
+
value: function setChannels(channels) {
|
|
577
|
+
var _this2 = this;
|
|
578
|
+
|
|
579
|
+
var namespacedChannels = channels.map(function (channel) {
|
|
580
|
+
return _this2.getChannelWithNamespace(channel);
|
|
581
|
+
}).sort();
|
|
582
|
+
|
|
583
|
+
if (this.channels.join(',') === namespacedChannels.join(',')) {
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
debug("Set channels: ".concat(namespacedChannels.join(', ')));
|
|
588
|
+
this.updateChannels(namespacedChannels);
|
|
589
|
+
}
|
|
590
|
+
}, {
|
|
591
|
+
key: "addChannel",
|
|
592
|
+
value: function addChannel(channel) {
|
|
593
|
+
var namespacedChannel = this.getChannelWithNamespace(channel);
|
|
594
|
+
|
|
595
|
+
if (this.channels.indexOf(namespacedChannel) !== -1) {
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
598
|
+
|
|
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
|
+
}))));
|
|
614
|
+
}
|
|
615
|
+
}, {
|
|
616
|
+
key: "removeChannel",
|
|
617
|
+
value: function removeChannel(channel) {
|
|
618
|
+
var namespacedChannel = this.getChannelWithNamespace(channel);
|
|
619
|
+
|
|
620
|
+
if (this.channels.indexOf(namespacedChannel) === -1) {
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
|
|
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;
|
|
665
|
+
|
|
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);
|
|
691
|
+
}
|
|
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
|
+
|
|
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();
|
|
711
|
+
}
|
|
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
|
+
|
|
722
|
+
if (this.starting) {
|
|
723
|
+
debug('Skipping start: Already starting.');
|
|
724
|
+
return;
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
if (!this.ready) {
|
|
728
|
+
debug('Skipping start: No ready.');
|
|
729
|
+
this.shouldStart = true;
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
732
|
+
|
|
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();
|
|
739
|
+
}
|
|
740
|
+
}, {
|
|
741
|
+
key: "stop",
|
|
742
|
+
value: function stop() {
|
|
743
|
+
this.shouldStart = false;
|
|
744
|
+
|
|
745
|
+
if (!this.started && !this.starting) {
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
debug('Stopping...');
|
|
750
|
+
|
|
751
|
+
if (this.adapter !== null) {
|
|
752
|
+
this.adapter.stop();
|
|
753
|
+
}
|
|
754
|
+
}
|
|
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
|
+
|
|
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);
|
|
769
|
+
}
|
|
770
|
+
}, {
|
|
771
|
+
key: "isStarted",
|
|
772
|
+
value: function isStarted() {
|
|
773
|
+
return this.started;
|
|
774
|
+
}
|
|
775
|
+
}], [{
|
|
776
|
+
key: "getAdapters",
|
|
777
|
+
value: function getAdapters() {
|
|
778
|
+
return Socket.adapters;
|
|
779
|
+
}
|
|
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
|
+
|
|
792
|
+
return Socket.adapters[adapterKey];
|
|
793
|
+
}
|
|
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;
|
|
799
|
+
}
|
|
800
|
+
}, {
|
|
801
|
+
key: "setAdapters",
|
|
802
|
+
value: function setAdapters(adapters) {
|
|
803
|
+
Socket.adapters = adapters;
|
|
804
|
+
return Socket;
|
|
805
|
+
}
|
|
806
|
+
}]);
|
|
807
|
+
|
|
808
|
+
return Socket;
|
|
809
|
+
}(EventEmitter);
|
|
810
|
+
|
|
811
|
+
Socket.adapters = _objectSpread$2({}, SocketAdapters);
|
|
812
|
+
|
|
813
|
+
var SocketContext = /*#__PURE__*/React.createContext({
|
|
814
|
+
socket: null,
|
|
815
|
+
subscribe: function subscribe() {},
|
|
816
|
+
unsubscribe: function unsubscribe() {}
|
|
817
|
+
});
|
|
818
|
+
|
|
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 = {
|
|
823
|
+
socket: PropTypes.instanceOf(Socket),
|
|
824
|
+
adapter: PropTypes.string,
|
|
825
|
+
host: PropTypes.string,
|
|
826
|
+
namespace: PropTypes.string,
|
|
827
|
+
uuid: PropTypes.string,
|
|
828
|
+
publishKey: PropTypes.string,
|
|
829
|
+
subscribeKey: PropTypes.string,
|
|
830
|
+
secretKey: PropTypes.string,
|
|
831
|
+
channels: PropTypes.arrayOf(PropTypes.string),
|
|
832
|
+
autoStart: PropTypes.bool,
|
|
833
|
+
children: PropTypes.node
|
|
834
|
+
};
|
|
835
|
+
var defaultProps = {
|
|
836
|
+
socket: null,
|
|
837
|
+
adapter: 'pubnub',
|
|
838
|
+
host: null,
|
|
839
|
+
namespace: null,
|
|
840
|
+
uuid: null,
|
|
841
|
+
publishKey: null,
|
|
842
|
+
subscribeKey: null,
|
|
843
|
+
secretKey: null,
|
|
844
|
+
channels: [],
|
|
845
|
+
autoStart: false,
|
|
846
|
+
children: null
|
|
847
|
+
};
|
|
848
|
+
|
|
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) {
|
|
880
|
+
finalSocket.setChannels(newChannels);
|
|
881
|
+
setChannels(newChannels);
|
|
882
|
+
}, [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);
|
|
887
|
+
updateChannels(Object.keys(channelsCountRef.current));
|
|
888
|
+
}, [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;
|
|
893
|
+
}, channelsCountRef.current);
|
|
894
|
+
updateChannels(Object.keys(channelsCountRef.current));
|
|
895
|
+
}, [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 () {
|
|
903
|
+
subscribe(initialChannels);
|
|
904
|
+
return function () {
|
|
905
|
+
unsubscribe(initialChannels);
|
|
906
|
+
};
|
|
907
|
+
}, [initialChannels, subscribe, unsubscribe]);
|
|
908
|
+
useEffect(function () {
|
|
909
|
+
if (autoStart) {
|
|
910
|
+
finalSocket.start();
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
return function () {
|
|
914
|
+
finalSocket.destroy();
|
|
915
|
+
};
|
|
916
|
+
}, [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]);
|
|
925
|
+
return /*#__PURE__*/jsx(SocketContext.Provider, {
|
|
926
|
+
value: value,
|
|
927
|
+
children: children
|
|
928
|
+
});
|
|
929
|
+
};
|
|
930
|
+
|
|
931
|
+
SocketContainer.propTypes = propTypes;
|
|
932
|
+
SocketContainer.defaultProps = defaultProps;
|
|
933
|
+
|
|
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
|
+
};
|
|
957
|
+
|
|
958
|
+
WithSocketComponent.displayName = "WithSocket(".concat(getDisplayName(WrappedComponent), ")");
|
|
959
|
+
return WithSocketComponent;
|
|
960
|
+
};
|
|
961
|
+
|
|
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 () {
|
|
986
|
+
if (socket === null) {
|
|
987
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
988
|
+
console.warn('Socket context is empty.');
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
return function () {};
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
var wasStarted = socket.isStarted();
|
|
995
|
+
|
|
996
|
+
var onStarted = function onStarted() {
|
|
997
|
+
return setStarted(true);
|
|
998
|
+
};
|
|
999
|
+
|
|
1000
|
+
var onStop = function onStop() {
|
|
1001
|
+
return setStarted(false);
|
|
1002
|
+
};
|
|
1003
|
+
|
|
1004
|
+
socket.on('stop', onStop);
|
|
1005
|
+
socket.on('started', onStarted);
|
|
1006
|
+
|
|
1007
|
+
if (channels !== null) {
|
|
1008
|
+
subscribe(channels);
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
if (!wasStarted) {
|
|
1012
|
+
socket.start();
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
return function () {
|
|
1016
|
+
socket.off('stop', onStop);
|
|
1017
|
+
socket.off('started', onStarted);
|
|
1018
|
+
|
|
1019
|
+
if (channels !== null) {
|
|
1020
|
+
unsubscribe(channels);
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
if (!wasStarted) {
|
|
1024
|
+
socket.stop();
|
|
1025
|
+
}
|
|
1026
|
+
};
|
|
1027
|
+
}, [channelsKey, customSocket]);
|
|
1028
|
+
useEffect(function () {
|
|
1029
|
+
if (socket === null) {
|
|
1030
|
+
return function () {};
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
var onMessage = function onMessage() {
|
|
1034
|
+
if (customOnMessage !== null) {
|
|
1035
|
+
customOnMessage.apply(void 0, arguments);
|
|
1036
|
+
}
|
|
1037
|
+
};
|
|
1038
|
+
|
|
1039
|
+
socket.on('message', onMessage);
|
|
1040
|
+
return function () {
|
|
1041
|
+
socket.off('message', onMessage);
|
|
1042
|
+
};
|
|
1043
|
+
}, [customOnMessage]);
|
|
1044
|
+
return {
|
|
1045
|
+
socket: socket,
|
|
1046
|
+
started: started,
|
|
1047
|
+
subscribe: subscribe,
|
|
1048
|
+
unsubscribe: unsubscribe
|
|
1049
|
+
};
|
|
1050
|
+
};
|
|
1051
|
+
|
|
1052
|
+
export { Socket, SocketContainer, SocketContext, Socket as default, useSocket, withSocket };
|