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