@folklore/socket 0.4.3 → 0.4.11

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/lib/Socket.js DELETED
@@ -1,390 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports["default"] = void 0;
9
-
10
- var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
11
-
12
- var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
13
-
14
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
15
-
16
- var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
17
-
18
- var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
19
-
20
- var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
21
-
22
- var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
23
-
24
- var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
25
-
26
- var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
27
-
28
- var _debug = _interopRequireDefault(require("debug"));
29
-
30
- var _wolfy87Eventemitter = _interopRequireDefault(require("wolfy87-eventemitter"));
31
-
32
- var _invariant = _interopRequireDefault(require("invariant"));
33
-
34
- var _isFunction = _interopRequireDefault(require("lodash/isFunction"));
35
-
36
- var _index = _interopRequireDefault(require("./adapters/index"));
37
-
38
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
39
-
40
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
41
-
42
- var normalize = function normalize(str) {
43
- return str.replace(/[^a-z0-9]+/gi, '').toLowerCase();
44
- };
45
-
46
- var debug = (0, _debug["default"])('folklore:socket');
47
-
48
- var Socket =
49
- /*#__PURE__*/
50
- function (_EventEmitter) {
51
- (0, _inherits2["default"])(Socket, _EventEmitter);
52
- (0, _createClass2["default"])(Socket, null, [{
53
- key: "getAdapters",
54
- value: function getAdapters() {
55
- return Socket.adapters;
56
- }
57
- }, {
58
- key: "getAdapter",
59
- value: function getAdapter(adapter) {
60
- // prettier-ignore
61
- var adapterKey = Object.keys(Socket.adapters).find(function (key) {
62
- return normalize(key) === normalize(adapter);
63
- }) || null;
64
-
65
- if (adapterKey === null) {
66
- throw new Error("Adapter ".concat(adapter, " not found"));
67
- }
68
-
69
- return Socket.adapters[adapterKey];
70
- }
71
- }, {
72
- key: "addAdapter",
73
- value: function addAdapter(name, adapter) {
74
- Socket.adapters = _objectSpread({}, Socket.adapters, (0, _defineProperty2["default"])({}, name, adapter));
75
- return Socket;
76
- }
77
- }, {
78
- key: "setAdapters",
79
- value: function setAdapters(adapters) {
80
- Socket.adapters = adapters;
81
- return Socket;
82
- }
83
- }]);
84
-
85
- function Socket(opts) {
86
- var _this;
87
-
88
- (0, _classCallCheck2["default"])(this, Socket);
89
- _this = (0, _possibleConstructorReturn2["default"])(this, (0, _getPrototypeOf2["default"])(Socket).call(this));
90
- _this.options = _objectSpread({
91
- adapter: 'pubnub',
92
- namespace: null,
93
- uuid: null,
94
- publishKey: null,
95
- subscribeKey: null,
96
- secretKey: null,
97
- channels: []
98
- }, opts);
99
- _this.onAdapterReady = _this.onAdapterReady.bind((0, _assertThisInitialized2["default"])(_this));
100
- _this.onAdapterStart = _this.onAdapterStart.bind((0, _assertThisInitialized2["default"])(_this));
101
- _this.onAdapterStarted = _this.onAdapterStarted.bind((0, _assertThisInitialized2["default"])(_this));
102
- _this.onAdapterMessage = _this.onAdapterMessage.bind((0, _assertThisInitialized2["default"])(_this));
103
- _this.onAdapterStop = _this.onAdapterStop.bind((0, _assertThisInitialized2["default"])(_this));
104
- _this.shouldStart = false;
105
- _this.started = false;
106
- _this.starting = false;
107
- _this.ready = false;
108
- _this.adapter = null;
109
- _this.channels = [];
110
-
111
- _this.init();
112
-
113
- if (_this.options.channels.length) {
114
- _this.setChannels(_this.options.channels);
115
- }
116
-
117
- return _this;
118
- }
119
-
120
- (0, _createClass2["default"])(Socket, [{
121
- key: "onAdapterReady",
122
- value: function onAdapterReady() {
123
- debug('Adapter ready');
124
- this.ready = true;
125
- this.emit('ready');
126
-
127
- if (this.shouldStart) {
128
- this.shouldStart = false;
129
- this.start();
130
- }
131
- }
132
- }, {
133
- key: "onAdapterStart",
134
- value: function onAdapterStart() {
135
- debug('Adapter starting...');
136
- this.starting = true;
137
- this.started = false;
138
- this.emit('start');
139
- }
140
- }, {
141
- key: "onAdapterStarted",
142
- value: function onAdapterStarted() {
143
- debug('Adapter started');
144
- this.starting = false;
145
- this.started = true;
146
- this.emit('started');
147
- }
148
- }, {
149
- key: "onAdapterStop",
150
- value: function onAdapterStop() {
151
- debug('Adapter stopped');
152
- this.starting = false;
153
- this.started = false;
154
- this.emit('stop');
155
- }
156
- }, {
157
- key: "onAdapterMessage",
158
- value: function onAdapterMessage(message) {
159
- debug('Adapter message', message);
160
- this.emit('message', message);
161
- }
162
- }, {
163
- key: "getChannelWithoutNamespace",
164
- value: function getChannelWithoutNamespace(name) {
165
- if (this.options.namespace === null) {
166
- return name;
167
- }
168
-
169
- var regExp = new RegExp("^".concat(this.options.namespace, ":"));
170
- return name.replace(regExp, '');
171
- }
172
- }, {
173
- key: "getChannelWithNamespace",
174
- value: function getChannelWithNamespace(name) {
175
- var parts = [];
176
-
177
- if (this.options.namespace !== null) {
178
- parts.push(this.options.namespace);
179
- }
180
-
181
- parts.push(name);
182
- return parts.join(':');
183
- }
184
- }, {
185
- key: "setChannels",
186
- value: function setChannels(channels) {
187
- var _this2 = this;
188
-
189
- var namespacedChannels = channels.map(function (channel) {
190
- return _this2.getChannelWithNamespace(channel);
191
- }).sort();
192
-
193
- if (this.channels.join(',') === namespacedChannels.join(',')) {
194
- return;
195
- }
196
-
197
- debug("Set channels: ".concat(namespacedChannels.join(', ')));
198
- this.updateChannels(namespacedChannels);
199
- }
200
- }, {
201
- key: "addChannel",
202
- value: function addChannel(channel) {
203
- var namespacedChannel = this.getChannelWithNamespace(channel);
204
-
205
- if (this.channels.indexOf(namespacedChannel) !== -1) {
206
- return;
207
- }
208
-
209
- debug("Adding channel: ".concat(channel));
210
- this.updateChannels([].concat((0, _toConsumableArray2["default"])(this.channels), [namespacedChannel]));
211
- }
212
- }, {
213
- key: "addChannels",
214
- value: function addChannels(channels) {
215
- var _this3 = this;
216
-
217
- var namespacedChannels = channels.map(function (channel) {
218
- return _this3.getChannelWithNamespace(channel);
219
- }).sort();
220
- debug("Adding channels: ".concat(channels.join(',')));
221
- this.updateChannels([].concat((0, _toConsumableArray2["default"])(this.channels), (0, _toConsumableArray2["default"])(namespacedChannels.filter(function (it) {
222
- return _this3.channels.indexOf(it) === -1;
223
- }))));
224
- }
225
- }, {
226
- key: "removeChannel",
227
- value: function removeChannel(channel) {
228
- var namespacedChannel = this.getChannelWithNamespace(channel);
229
-
230
- if (this.channels.indexOf(namespacedChannel) === -1) {
231
- return;
232
- }
233
-
234
- debug("Removing channel: ".concat(channel));
235
- this.updateChannels(this.channels.filter(function (ch) {
236
- return ch !== namespacedChannel;
237
- }));
238
- }
239
- }, {
240
- key: "removeChannels",
241
- value: function removeChannels(channels) {
242
- var _this4 = this;
243
-
244
- var namespacedChannels = channels.map(function (channel) {
245
- return _this4.getChannelWithNamespace(channel);
246
- }).sort();
247
- debug("Removing channels: ".concat(channels.join(',')));
248
- this.updateChannels(this.channels.filter(function (it) {
249
- return namespacedChannels.indexOf(it) === -1;
250
- }));
251
- }
252
- }, {
253
- key: "updateChannels",
254
- value: function updateChannels(channels) {
255
- var sortedChannels = channels.sort();
256
- debug("Updating channels: ".concat(sortedChannels.join(', ')));
257
- this.channels = (0, _toConsumableArray2["default"])(sortedChannels);
258
-
259
- if (this.adapter !== null) {
260
- this.adapter.updateChannels(sortedChannels);
261
- }
262
- }
263
- }, {
264
- key: "hasChannel",
265
- value: function hasChannel(channel) {
266
- var namespacedChannel = this.getChannelWithNamespace(channel);
267
- return this.channels.reduce(function (found, it) {
268
- return found || it === namespacedChannel;
269
- }, false);
270
- }
271
- }, {
272
- key: "getChannels",
273
- value: function getChannels() {
274
- var _this5 = this;
275
-
276
- return this.channels.map(function (channel) {
277
- return _this5.getChannelWithoutNamespace(channel);
278
- });
279
- }
280
- }, {
281
- key: "init",
282
- value: function init() {
283
- var _this6 = this;
284
-
285
- var _this$options = this.options,
286
- adapterKey = _this$options.adapter,
287
- channels = _this$options.channels,
288
- adapterOptions = (0, _objectWithoutProperties2["default"])(_this$options, ["adapter", "channels"]);
289
- var SocketAdapter = Socket.getAdapter(adapterKey);
290
- this.adapter = new SocketAdapter(adapterOptions);
291
- var methods = ['start', 'stop', 'destroy', 'updateChannels', 'send'];
292
- methods.forEach(function (method) {
293
- (0, _invariant["default"])((0, _isFunction["default"])(_this6.adapter[method] || null), "Socket adapter should implement method ".concat(method));
294
- });
295
- this.adapter.on('ready', this.onAdapterReady);
296
- this.adapter.on('start', this.onAdapterStart);
297
- this.adapter.on('started', this.onAdapterStarted);
298
- this.adapter.on('message', this.onAdapterMessage);
299
- this.adapter.on('stop', this.onAdapterStop);
300
- }
301
- }, {
302
- key: "destroy",
303
- value: function destroy() {
304
- if (this.adapter !== null) {
305
- this.adapter.removeAllListeners();
306
- this.adapter.destroy();
307
- this.adapter = null;
308
- }
309
-
310
- this.started = false;
311
- this.starting = false;
312
- this.ready = false;
313
- debug('Destroyed.');
314
- }
315
- }, {
316
- key: "restart",
317
- value: function restart() {
318
- this.stop();
319
- this.start();
320
- }
321
- }, {
322
- key: "start",
323
- value: function start() {
324
- var _this7 = this;
325
-
326
- if (this.started) {
327
- debug('Skipping start: Already started.');
328
- return;
329
- }
330
-
331
- if (this.starting) {
332
- debug('Skipping start: Already starting.');
333
- return;
334
- }
335
-
336
- if (!this.ready) {
337
- debug('Skipping start: No ready.');
338
- this.shouldStart = true;
339
- return;
340
- }
341
-
342
- this.shouldStart = false;
343
- debug('Starting on channels:');
344
- this.channels.forEach(function (channel) {
345
- debug("- ".concat(_this7.getChannelWithoutNamespace(channel)));
346
- });
347
- this.adapter.start();
348
- }
349
- }, {
350
- key: "stop",
351
- value: function stop() {
352
- this.shouldStart = false;
353
-
354
- if (!this.started && !this.starting) {
355
- return;
356
- }
357
-
358
- debug('Stopping...');
359
-
360
- if (this.adapter !== null) {
361
- this.adapter.stop();
362
- }
363
- }
364
- }, {
365
- key: "send",
366
- value: function send(data, channel) {
367
- if (!this.started) {
368
- debug('Abort sending data: Not started');
369
- return Promise.reject();
370
- }
371
-
372
- var publishData = typeof data.channel !== 'undefined' && typeof data.message !== 'undefined' ? data : {
373
- channel: typeof channel !== 'undefined' ? this.getChannelWithNamespace(channel) : this.channels,
374
- message: data
375
- };
376
- debug('Sending', publishData);
377
- return this.adapter.send(publishData);
378
- }
379
- }, {
380
- key: "isStarted",
381
- value: function isStarted() {
382
- return this.started;
383
- }
384
- }]);
385
- return Socket;
386
- }(_wolfy87Eventemitter["default"]);
387
-
388
- Socket.adapters = _objectSpread({}, _index["default"]);
389
- var _default = Socket;
390
- exports["default"] = _default;
@@ -1,139 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
-
5
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
-
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- exports["default"] = void 0;
11
-
12
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13
-
14
- var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
15
-
16
- var _react = _interopRequireWildcard(require("react"));
17
-
18
- var _propTypes = _interopRequireDefault(require("prop-types"));
19
-
20
- var _Socket = _interopRequireDefault(require("./Socket"));
21
-
22
- var _SocketContext = _interopRequireDefault(require("./SocketContext"));
23
-
24
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
25
-
26
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
27
-
28
- var propTypes = {
29
- socket: _propTypes["default"].instanceOf(_Socket["default"]),
30
- adapter: _propTypes["default"].string,
31
- host: _propTypes["default"].string,
32
- namespace: _propTypes["default"].string,
33
- uuid: _propTypes["default"].string,
34
- publishKey: _propTypes["default"].string,
35
- subscribeKey: _propTypes["default"].string,
36
- secretKey: _propTypes["default"].string,
37
- channels: _propTypes["default"].arrayOf(_propTypes["default"].string),
38
- autoStart: _propTypes["default"].bool,
39
- children: _propTypes["default"].node
40
- };
41
- var defaultProps = {
42
- socket: null,
43
- adapter: 'pubnub',
44
- host: null,
45
- namespace: null,
46
- uuid: null,
47
- publishKey: null,
48
- subscribeKey: null,
49
- secretKey: null,
50
- channels: [],
51
- autoStart: false,
52
- children: null
53
- };
54
-
55
- var SocketContainer = function SocketContainer(_ref) {
56
- var children = _ref.children,
57
- socket = _ref.socket,
58
- autoStart = _ref.autoStart,
59
- adapter = _ref.adapter,
60
- host = _ref.host,
61
- namespace = _ref.namespace,
62
- uuid = _ref.uuid,
63
- publishKey = _ref.publishKey,
64
- subscribeKey = _ref.subscribeKey,
65
- secretKey = _ref.secretKey,
66
- initialChannels = _ref.channels;
67
- var finalSocket = (0, _react.useMemo)(function () {
68
- return socket || new _Socket["default"]({
69
- adapter: adapter,
70
- host: host,
71
- namespace: namespace,
72
- uuid: uuid,
73
- publishKey: publishKey,
74
- subscribeKey: subscribeKey,
75
- secretKey: secretKey
76
- });
77
- }, [socket, host, adapter, namespace, uuid, publishKey, subscribeKey, secretKey]);
78
-
79
- var _useState = (0, _react.useState)([]),
80
- _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
81
- channels = _useState2[0],
82
- setChannels = _useState2[1];
83
-
84
- var channelsCountRef = (0, _react.useRef)({});
85
- var updateChannels = (0, _react.useCallback)(function (newChannels) {
86
- finalSocket.setChannels(newChannels);
87
- setChannels(newChannels);
88
- }, [finalSocket, setChannels]);
89
- var addToChannelsCount = (0, _react.useCallback)(function (newChannels) {
90
- channelsCountRef.current = newChannels.reduce(function (map, channel) {
91
- return _objectSpread({}, map, (0, _defineProperty2["default"])({}, channel, (map[channel] || 0) + 1));
92
- }, channelsCountRef.current);
93
- updateChannels(Object.keys(channelsCountRef.current));
94
- }, [updateChannels]);
95
- var removeToChannelsCount = (0, _react.useCallback)(function (newChannels) {
96
- channelsCountRef.current = newChannels.reduce(function (map, channel) {
97
- var newCount = (map[channel] || 0) - 1;
98
- return newCount > 0 ? _objectSpread({}, map, (0, _defineProperty2["default"])({}, channel, newCount)) : map;
99
- }, channelsCountRef.current);
100
- updateChannels(Object.keys(channelsCountRef.current));
101
- }, [updateChannels]);
102
- var subscribe = (0, _react.useCallback)(function (channelsToAdd) {
103
- return addToChannelsCount(channelsToAdd);
104
- }, [addToChannelsCount]);
105
- var unsubscribe = (0, _react.useCallback)(function (channelsToRemove) {
106
- return removeToChannelsCount(channelsToRemove);
107
- }, [removeToChannelsCount]);
108
- (0, _react.useEffect)(function () {
109
- subscribe(initialChannels);
110
- return function () {
111
- unsubscribe(initialChannels);
112
- };
113
- }, [initialChannels, subscribe, unsubscribe]);
114
- (0, _react.useEffect)(function () {
115
- if (autoStart) {
116
- finalSocket.start();
117
- }
118
-
119
- return function () {
120
- finalSocket.destroy();
121
- };
122
- }, [autoStart, finalSocket]);
123
- var value = (0, _react.useMemo)(function () {
124
- return {
125
- socket: finalSocket,
126
- subscribe: subscribe,
127
- unsubscribe: unsubscribe,
128
- channels: channels
129
- };
130
- }, [finalSocket, subscribe]);
131
- return _react["default"].createElement(_SocketContext["default"].Provider, {
132
- value: value
133
- }, children);
134
- };
135
-
136
- SocketContainer.propTypes = propTypes;
137
- SocketContainer.defaultProps = defaultProps;
138
- var _default = SocketContainer;
139
- exports["default"] = _default;
@@ -1,19 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports["default"] = void 0;
9
-
10
- var _react = _interopRequireDefault(require("react"));
11
-
12
- var SocketContext = _react["default"].createContext({
13
- socket: null,
14
- subscribe: function subscribe() {},
15
- unsubscribe: function unsubscribe() {}
16
- });
17
-
18
- var _default = SocketContext;
19
- exports["default"] = _default;