@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.
- package/dist/cjs.js +621 -824
- package/dist/es.js +621 -814
- 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
|
-
|
|
44
|
+
const debug$2 = createDebug__default["default"]('folklore:socket:pubnub');
|
|
65
45
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
-
|
|
83
|
+
onMessage(_ref) {
|
|
84
|
+
let {
|
|
85
|
+
message
|
|
86
|
+
} = _ref;
|
|
87
|
+
this.emit('message', message);
|
|
145
88
|
|
|
146
|
-
|
|
147
|
-
|
|
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
|
-
|
|
179
|
-
|
|
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
|
-
|
|
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
|
-
|
|
195
|
-
this.pubnub.removeListener(this.pubnubListener);
|
|
196
|
-
this.pubnubListener = null;
|
|
197
|
-
}
|
|
106
|
+
this.channels = channels;
|
|
198
107
|
|
|
199
|
-
|
|
200
|
-
|
|
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
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
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
|
-
|
|
222
|
-
|
|
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
|
-
|
|
236
|
-
|
|
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
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
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
|
-
|
|
267
|
-
|
|
156
|
+
this.ready = false;
|
|
157
|
+
debug$2('Destroyed.');
|
|
158
|
+
}
|
|
268
159
|
|
|
269
|
-
|
|
160
|
+
start() {
|
|
161
|
+
if (this.started) {
|
|
162
|
+
debug$2('Skipping start: Already started.');
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
270
165
|
|
|
271
|
-
|
|
166
|
+
if (this.starting) {
|
|
167
|
+
debug$2('Skipping start: Already starting.');
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
272
170
|
|
|
273
|
-
|
|
171
|
+
if (this.channels.length === 0) {
|
|
172
|
+
debug$2('Skipping start: No channels.');
|
|
173
|
+
this.shouldStart = true;
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
274
176
|
|
|
275
|
-
|
|
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
|
-
|
|
278
|
-
|
|
185
|
+
stop() {
|
|
186
|
+
if (!this.started && !this.starting) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
279
189
|
|
|
280
|
-
|
|
281
|
-
|
|
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
|
-
|
|
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
|
-
|
|
286
|
-
var _this;
|
|
217
|
+
}
|
|
287
218
|
|
|
288
|
-
|
|
219
|
+
const debug$1 = createDebug__default["default"]('folklore:socket:socketio');
|
|
289
220
|
|
|
290
|
-
|
|
291
|
-
|
|
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
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
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
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
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
|
-
|
|
260
|
+
onMessage(message, channel) {
|
|
261
|
+
debug$1('Message received on %s %o', channel, message);
|
|
262
|
+
this.emit('message', message, channel);
|
|
263
|
+
}
|
|
381
264
|
|
|
382
|
-
|
|
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
|
-
|
|
397
|
-
|
|
398
|
-
|
|
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
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
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
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
return;
|
|
411
|
-
}
|
|
300
|
+
if (started || starting) {
|
|
301
|
+
this.stop();
|
|
302
|
+
}
|
|
412
303
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
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
|
-
|
|
426
|
-
|
|
427
|
-
|
|
311
|
+
start() {
|
|
312
|
+
if (this.started) {
|
|
313
|
+
debug$1('Skipping start: Already started.');
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
428
316
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
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
|
-
|
|
440
|
-
|
|
441
|
-
|
|
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
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
328
|
+
if (this.channels.length === 0) {
|
|
329
|
+
debug$1('Skipping start: No channels.');
|
|
330
|
+
this.shouldStart = true;
|
|
331
|
+
return;
|
|
472
332
|
}
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
return
|
|
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
|
-
|
|
489
|
-
|
|
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
|
-
|
|
492
|
-
|
|
493
|
-
|
|
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
|
-
|
|
364
|
+
stopSocket(socket) {
|
|
365
|
+
socket.off('connect');
|
|
366
|
+
socket.off('message');
|
|
367
|
+
socket.close();
|
|
368
|
+
return socket;
|
|
369
|
+
}
|
|
499
370
|
|
|
500
|
-
|
|
371
|
+
destroy() {
|
|
372
|
+
this.stop();
|
|
373
|
+
this.sockets = {};
|
|
501
374
|
|
|
502
|
-
|
|
375
|
+
if (this.io !== null) {
|
|
376
|
+
this.io.close();
|
|
377
|
+
this.io = null;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
503
380
|
|
|
504
|
-
|
|
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
|
-
|
|
507
|
-
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
var SocketAdapters = {
|
|
396
|
+
PubNub: PubNubSocket,
|
|
397
|
+
SocketIO: SocketIOSocket
|
|
508
398
|
};
|
|
509
399
|
|
|
510
|
-
|
|
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
|
-
|
|
513
|
-
|
|
413
|
+
if (adapterKey === null) {
|
|
414
|
+
throw new Error("Adapter ".concat(adapter, " not found"));
|
|
415
|
+
}
|
|
514
416
|
|
|
515
|
-
|
|
417
|
+
return Socket.adapters[adapterKey];
|
|
418
|
+
}
|
|
516
419
|
|
|
517
|
-
|
|
518
|
-
|
|
420
|
+
static addAdapter(name, adapter) {
|
|
421
|
+
Socket.adapters = { ...Socket.adapters,
|
|
422
|
+
[name]: adapter
|
|
423
|
+
};
|
|
424
|
+
return Socket;
|
|
425
|
+
}
|
|
519
426
|
|
|
520
|
-
|
|
427
|
+
static setAdapters(adapters) {
|
|
428
|
+
Socket.adapters = adapters;
|
|
429
|
+
return Socket;
|
|
430
|
+
}
|
|
521
431
|
|
|
522
|
-
|
|
523
|
-
|
|
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
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
if (
|
|
547
|
-
|
|
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
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
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
|
-
|
|
561
|
-
|
|
562
|
-
|
|
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
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
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
|
-
|
|
611
|
-
|
|
612
|
-
|
|
480
|
+
onAdapterStarted() {
|
|
481
|
+
debug('Adapter started');
|
|
482
|
+
this.starting = false;
|
|
483
|
+
this.started = true;
|
|
484
|
+
this.emit('started');
|
|
485
|
+
}
|
|
613
486
|
|
|
614
|
-
|
|
615
|
-
|
|
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
|
-
|
|
623
|
-
|
|
624
|
-
|
|
504
|
+
const regExp = new RegExp("^".concat(this.options.namespace, ":"));
|
|
505
|
+
return name.replace(regExp, '');
|
|
506
|
+
}
|
|
625
507
|
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
}
|
|
508
|
+
getChannelWithNamespace(name) {
|
|
509
|
+
const parts = [];
|
|
629
510
|
|
|
630
|
-
|
|
631
|
-
this.
|
|
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
|
-
|
|
639
|
-
|
|
640
|
-
|
|
515
|
+
parts.push(name);
|
|
516
|
+
return parts.join(':');
|
|
517
|
+
}
|
|
641
518
|
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
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
|
-
|
|
664
|
-
|
|
665
|
-
|
|
526
|
+
debug("Set channels: ".concat(namespacedChannels.join(', ')));
|
|
527
|
+
this.updateChannels(namespacedChannels);
|
|
528
|
+
}
|
|
666
529
|
|
|
667
|
-
|
|
668
|
-
|
|
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
|
-
|
|
710
|
-
|
|
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
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
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
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
}
|
|
554
|
+
debug("Removing channel: ".concat(channel));
|
|
555
|
+
this.updateChannels(this.channels.filter(ch => ch !== namespacedChannel));
|
|
556
|
+
}
|
|
769
557
|
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
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
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
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
|
-
|
|
789
|
-
|
|
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
|
-
|
|
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
|
-
|
|
795
|
-
|
|
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
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
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
|
-
|
|
815
|
-
|
|
816
|
-
return
|
|
625
|
+
|
|
626
|
+
if (this.starting) {
|
|
627
|
+
debug('Skipping start: Already starting.');
|
|
628
|
+
return;
|
|
817
629
|
}
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
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
|
-
|
|
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
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
651
|
+
|
|
652
|
+
debug('Stopping...');
|
|
653
|
+
|
|
654
|
+
if (this.adapter !== null) {
|
|
655
|
+
this.adapter.stop();
|
|
842
656
|
}
|
|
843
|
-
}
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
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
|
-
|
|
852
|
-
|
|
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 =
|
|
679
|
+
Socket.adapters = { ...SocketAdapters
|
|
680
|
+
};
|
|
855
681
|
|
|
856
|
-
|
|
682
|
+
const SocketContext = /*#__PURE__*/React__default["default"].createContext({
|
|
857
683
|
socket: null,
|
|
858
|
-
subscribe:
|
|
859
|
-
unsubscribe:
|
|
684
|
+
subscribe: () => {},
|
|
685
|
+
unsubscribe: () => {}
|
|
860
686
|
});
|
|
861
687
|
|
|
862
|
-
|
|
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
|
-
|
|
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
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
}, [socket, host, adapter, namespace, uuid, publishKey, subscribeKey, secretKey]);
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
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
|
-
|
|
927
|
-
channelsCountRef.current = newChannels.reduce(
|
|
928
|
-
|
|
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
|
-
|
|
933
|
-
channelsCountRef.current = newChannels.reduce(
|
|
934
|
-
|
|
935
|
-
return newCount > 0 ?
|
|
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
|
-
|
|
940
|
-
|
|
941
|
-
|
|
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
|
|
763
|
+
return () => {
|
|
948
764
|
unsubscribe(initialChannels);
|
|
949
765
|
};
|
|
950
766
|
}, [initialChannels, subscribe, unsubscribe]);
|
|
951
|
-
React.useEffect(
|
|
767
|
+
React.useEffect(() => {
|
|
952
768
|
if (autoStart) {
|
|
953
769
|
finalSocket.start();
|
|
954
770
|
}
|
|
955
771
|
|
|
956
|
-
return
|
|
772
|
+
return () => {
|
|
957
773
|
finalSocket.destroy();
|
|
958
774
|
};
|
|
959
775
|
}, [autoStart, finalSocket]);
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
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
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
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
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
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
|
|
835
|
+
return () => {};
|
|
1035
836
|
}
|
|
1036
837
|
|
|
1037
|
-
|
|
838
|
+
const wasStarted = socket.isStarted();
|
|
1038
839
|
|
|
1039
|
-
|
|
1040
|
-
return setStarted(true);
|
|
1041
|
-
};
|
|
840
|
+
const onStarted = () => setStarted(true);
|
|
1042
841
|
|
|
1043
|
-
|
|
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
|
|
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(
|
|
868
|
+
React.useEffect(() => {
|
|
1072
869
|
if (socket === null) {
|
|
1073
|
-
return
|
|
870
|
+
return () => {};
|
|
1074
871
|
}
|
|
1075
872
|
|
|
1076
|
-
|
|
873
|
+
const onMessage = function () {
|
|
1077
874
|
if (customOnMessage !== null) {
|
|
1078
|
-
customOnMessage
|
|
875
|
+
customOnMessage(...arguments);
|
|
1079
876
|
}
|
|
1080
877
|
};
|
|
1081
878
|
|
|
1082
879
|
socket.on('message', onMessage);
|
|
1083
|
-
return
|
|
880
|
+
return () => {
|
|
1084
881
|
socket.off('message', onMessage);
|
|
1085
882
|
};
|
|
1086
883
|
}, [customOnMessage]);
|
|
1087
884
|
return {
|
|
1088
|
-
socket
|
|
1089
|
-
started
|
|
1090
|
-
subscribe
|
|
1091
|
-
unsubscribe
|
|
885
|
+
socket,
|
|
886
|
+
started,
|
|
887
|
+
subscribe,
|
|
888
|
+
unsubscribe
|
|
1092
889
|
};
|
|
1093
890
|
};
|
|
1094
891
|
|