actioncable 6.1.5 → 7.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,154 +1,115 @@
1
1
  (function(global, factory) {
2
- typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define([ "exports" ], factory) : factory(global.ActionCable = {});
3
- })(this, function(exports) {
2
+ typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define([ "exports" ], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self,
3
+ factory(global.ActionCable = {}));
4
+ })(this, (function(exports) {
4
5
  "use strict";
5
6
  var adapters = {
6
7
  logger: self.console,
7
8
  WebSocket: self.WebSocket
8
9
  };
9
10
  var logger = {
10
- log: function log() {
11
+ log(...messages) {
11
12
  if (this.enabled) {
12
- var _adapters$logger;
13
- for (var _len = arguments.length, messages = Array(_len), _key = 0; _key < _len; _key++) {
14
- messages[_key] = arguments[_key];
15
- }
16
13
  messages.push(Date.now());
17
- (_adapters$logger = adapters.logger).log.apply(_adapters$logger, [ "[ActionCable]" ].concat(messages));
18
- }
19
- }
20
- };
21
- var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function(obj) {
22
- return typeof obj;
23
- } : function(obj) {
24
- return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
25
- };
26
- var classCallCheck = function(instance, Constructor) {
27
- if (!(instance instanceof Constructor)) {
28
- throw new TypeError("Cannot call a class as a function");
29
- }
30
- };
31
- var createClass = function() {
32
- function defineProperties(target, props) {
33
- for (var i = 0; i < props.length; i++) {
34
- var descriptor = props[i];
35
- descriptor.enumerable = descriptor.enumerable || false;
36
- descriptor.configurable = true;
37
- if ("value" in descriptor) descriptor.writable = true;
38
- Object.defineProperty(target, descriptor.key, descriptor);
14
+ adapters.logger.log("[ActionCable]", ...messages);
39
15
  }
40
16
  }
41
- return function(Constructor, protoProps, staticProps) {
42
- if (protoProps) defineProperties(Constructor.prototype, protoProps);
43
- if (staticProps) defineProperties(Constructor, staticProps);
44
- return Constructor;
45
- };
46
- }();
47
- var now = function now() {
48
- return new Date().getTime();
49
- };
50
- var secondsSince = function secondsSince(time) {
51
- return (now() - time) / 1e3;
52
17
  };
53
- var clamp = function clamp(number, min, max) {
54
- return Math.max(min, Math.min(max, number));
55
- };
56
- var ConnectionMonitor = function() {
57
- function ConnectionMonitor(connection) {
58
- classCallCheck(this, ConnectionMonitor);
18
+ const now = () => (new Date).getTime();
19
+ const secondsSince = time => (now() - time) / 1e3;
20
+ class ConnectionMonitor {
21
+ constructor(connection) {
59
22
  this.visibilityDidChange = this.visibilityDidChange.bind(this);
60
23
  this.connection = connection;
61
24
  this.reconnectAttempts = 0;
62
25
  }
63
- ConnectionMonitor.prototype.start = function start() {
26
+ start() {
64
27
  if (!this.isRunning()) {
65
28
  this.startedAt = now();
66
29
  delete this.stoppedAt;
67
30
  this.startPolling();
68
31
  addEventListener("visibilitychange", this.visibilityDidChange);
69
- logger.log("ConnectionMonitor started. pollInterval = " + this.getPollInterval() + " ms");
32
+ logger.log(`ConnectionMonitor started. stale threshold = ${this.constructor.staleThreshold} s`);
70
33
  }
71
- };
72
- ConnectionMonitor.prototype.stop = function stop() {
34
+ }
35
+ stop() {
73
36
  if (this.isRunning()) {
74
37
  this.stoppedAt = now();
75
38
  this.stopPolling();
76
39
  removeEventListener("visibilitychange", this.visibilityDidChange);
77
40
  logger.log("ConnectionMonitor stopped");
78
41
  }
79
- };
80
- ConnectionMonitor.prototype.isRunning = function isRunning() {
42
+ }
43
+ isRunning() {
81
44
  return this.startedAt && !this.stoppedAt;
82
- };
83
- ConnectionMonitor.prototype.recordPing = function recordPing() {
45
+ }
46
+ recordPing() {
84
47
  this.pingedAt = now();
85
- };
86
- ConnectionMonitor.prototype.recordConnect = function recordConnect() {
48
+ }
49
+ recordConnect() {
87
50
  this.reconnectAttempts = 0;
88
51
  this.recordPing();
89
52
  delete this.disconnectedAt;
90
53
  logger.log("ConnectionMonitor recorded connect");
91
- };
92
- ConnectionMonitor.prototype.recordDisconnect = function recordDisconnect() {
54
+ }
55
+ recordDisconnect() {
93
56
  this.disconnectedAt = now();
94
57
  logger.log("ConnectionMonitor recorded disconnect");
95
- };
96
- ConnectionMonitor.prototype.startPolling = function startPolling() {
58
+ }
59
+ startPolling() {
97
60
  this.stopPolling();
98
61
  this.poll();
99
- };
100
- ConnectionMonitor.prototype.stopPolling = function stopPolling() {
62
+ }
63
+ stopPolling() {
101
64
  clearTimeout(this.pollTimeout);
102
- };
103
- ConnectionMonitor.prototype.poll = function poll() {
104
- var _this = this;
105
- this.pollTimeout = setTimeout(function() {
106
- _this.reconnectIfStale();
107
- _this.poll();
108
- }, this.getPollInterval());
109
- };
110
- ConnectionMonitor.prototype.getPollInterval = function getPollInterval() {
111
- var _constructor$pollInte = this.constructor.pollInterval, min = _constructor$pollInte.min, max = _constructor$pollInte.max, multiplier = _constructor$pollInte.multiplier;
112
- var interval = multiplier * Math.log(this.reconnectAttempts + 1);
113
- return Math.round(clamp(interval, min, max) * 1e3);
114
- };
115
- ConnectionMonitor.prototype.reconnectIfStale = function reconnectIfStale() {
65
+ }
66
+ poll() {
67
+ this.pollTimeout = setTimeout((() => {
68
+ this.reconnectIfStale();
69
+ this.poll();
70
+ }), this.getPollInterval());
71
+ }
72
+ getPollInterval() {
73
+ const {staleThreshold: staleThreshold, reconnectionBackoffRate: reconnectionBackoffRate} = this.constructor;
74
+ const backoff = Math.pow(1 + reconnectionBackoffRate, Math.min(this.reconnectAttempts, 10));
75
+ const jitterMax = this.reconnectAttempts === 0 ? 1 : reconnectionBackoffRate;
76
+ const jitter = jitterMax * Math.random();
77
+ return staleThreshold * 1e3 * backoff * (1 + jitter);
78
+ }
79
+ reconnectIfStale() {
116
80
  if (this.connectionIsStale()) {
117
- logger.log("ConnectionMonitor detected stale connection. reconnectAttempts = " + this.reconnectAttempts + ", pollInterval = " + this.getPollInterval() + " ms, time disconnected = " + secondsSince(this.disconnectedAt) + " s, stale threshold = " + this.constructor.staleThreshold + " s");
81
+ logger.log(`ConnectionMonitor detected stale connection. reconnectAttempts = ${this.reconnectAttempts}, time stale = ${secondsSince(this.refreshedAt)} s, stale threshold = ${this.constructor.staleThreshold} s`);
118
82
  this.reconnectAttempts++;
119
83
  if (this.disconnectedRecently()) {
120
- logger.log("ConnectionMonitor skipping reopening recent disconnect");
84
+ logger.log(`ConnectionMonitor skipping reopening recent disconnect. time disconnected = ${secondsSince(this.disconnectedAt)} s`);
121
85
  } else {
122
86
  logger.log("ConnectionMonitor reopening");
123
87
  this.connection.reopen();
124
88
  }
125
89
  }
126
- };
127
- ConnectionMonitor.prototype.connectionIsStale = function connectionIsStale() {
128
- return secondsSince(this.pingedAt ? this.pingedAt : this.startedAt) > this.constructor.staleThreshold;
129
- };
130
- ConnectionMonitor.prototype.disconnectedRecently = function disconnectedRecently() {
90
+ }
91
+ get refreshedAt() {
92
+ return this.pingedAt ? this.pingedAt : this.startedAt;
93
+ }
94
+ connectionIsStale() {
95
+ return secondsSince(this.refreshedAt) > this.constructor.staleThreshold;
96
+ }
97
+ disconnectedRecently() {
131
98
  return this.disconnectedAt && secondsSince(this.disconnectedAt) < this.constructor.staleThreshold;
132
- };
133
- ConnectionMonitor.prototype.visibilityDidChange = function visibilityDidChange() {
134
- var _this2 = this;
99
+ }
100
+ visibilityDidChange() {
135
101
  if (document.visibilityState === "visible") {
136
- setTimeout(function() {
137
- if (_this2.connectionIsStale() || !_this2.connection.isOpen()) {
138
- logger.log("ConnectionMonitor reopening stale connection on visibilitychange. visibilityState = " + document.visibilityState);
139
- _this2.connection.reopen();
102
+ setTimeout((() => {
103
+ if (this.connectionIsStale() || !this.connection.isOpen()) {
104
+ logger.log(`ConnectionMonitor reopening stale connection on visibilitychange. visibilityState = ${document.visibilityState}`);
105
+ this.connection.reopen();
140
106
  }
141
- }, 200);
107
+ }), 200);
142
108
  }
143
- };
144
- return ConnectionMonitor;
145
- }();
146
- ConnectionMonitor.pollInterval = {
147
- min: 3,
148
- max: 30,
149
- multiplier: 5
150
- };
109
+ }
110
+ }
151
111
  ConnectionMonitor.staleThreshold = 6;
112
+ ConnectionMonitor.reconnectionBackoffRate = .15;
152
113
  var INTERNAL = {
153
114
  message_types: {
154
115
  welcome: "welcome",
@@ -165,32 +126,31 @@
165
126
  default_mount_path: "/cable",
166
127
  protocols: [ "actioncable-v1-json", "actioncable-unsupported" ]
167
128
  };
168
- var message_types = INTERNAL.message_types, protocols = INTERNAL.protocols;
169
- var supportedProtocols = protocols.slice(0, protocols.length - 1);
170
- var indexOf = [].indexOf;
171
- var Connection = function() {
172
- function Connection(consumer) {
173
- classCallCheck(this, Connection);
129
+ const {message_types: message_types, protocols: protocols} = INTERNAL;
130
+ const supportedProtocols = protocols.slice(0, protocols.length - 1);
131
+ const indexOf = [].indexOf;
132
+ class Connection {
133
+ constructor(consumer) {
174
134
  this.open = this.open.bind(this);
175
135
  this.consumer = consumer;
176
136
  this.subscriptions = this.consumer.subscriptions;
177
137
  this.monitor = new ConnectionMonitor(this);
178
138
  this.disconnected = true;
179
139
  }
180
- Connection.prototype.send = function send(data) {
140
+ send(data) {
181
141
  if (this.isOpen()) {
182
142
  this.webSocket.send(JSON.stringify(data));
183
143
  return true;
184
144
  } else {
185
145
  return false;
186
146
  }
187
- };
188
- Connection.prototype.open = function open() {
147
+ }
148
+ open() {
189
149
  if (this.isActive()) {
190
- logger.log("Attempted to open WebSocket, but existing socket is " + this.getState());
150
+ logger.log(`Attempted to open WebSocket, but existing socket is ${this.getState()}`);
191
151
  return false;
192
152
  } else {
193
- logger.log("Opening WebSocket, current state is " + this.getState() + ", subprotocols: " + protocols);
153
+ logger.log(`Opening WebSocket, current state is ${this.getState()}, subprotocols: ${protocols}`);
194
154
  if (this.webSocket) {
195
155
  this.uninstallEventHandlers();
196
156
  }
@@ -199,90 +159,85 @@
199
159
  this.monitor.start();
200
160
  return true;
201
161
  }
202
- };
203
- Connection.prototype.close = function close() {
204
- var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
205
- allowReconnect: true
206
- }, allowReconnect = _ref.allowReconnect;
162
+ }
163
+ close({allowReconnect: allowReconnect} = {
164
+ allowReconnect: true
165
+ }) {
207
166
  if (!allowReconnect) {
208
167
  this.monitor.stop();
209
168
  }
210
169
  if (this.isActive()) {
211
170
  return this.webSocket.close();
212
171
  }
213
- };
214
- Connection.prototype.reopen = function reopen() {
215
- logger.log("Reopening WebSocket, current state is " + this.getState());
172
+ }
173
+ reopen() {
174
+ logger.log(`Reopening WebSocket, current state is ${this.getState()}`);
216
175
  if (this.isActive()) {
217
176
  try {
218
177
  return this.close();
219
178
  } catch (error) {
220
179
  logger.log("Failed to reopen WebSocket", error);
221
180
  } finally {
222
- logger.log("Reopening WebSocket in " + this.constructor.reopenDelay + "ms");
181
+ logger.log(`Reopening WebSocket in ${this.constructor.reopenDelay}ms`);
223
182
  setTimeout(this.open, this.constructor.reopenDelay);
224
183
  }
225
184
  } else {
226
185
  return this.open();
227
186
  }
228
- };
229
- Connection.prototype.getProtocol = function getProtocol() {
187
+ }
188
+ getProtocol() {
230
189
  if (this.webSocket) {
231
190
  return this.webSocket.protocol;
232
191
  }
233
- };
234
- Connection.prototype.isOpen = function isOpen() {
192
+ }
193
+ isOpen() {
235
194
  return this.isState("open");
236
- };
237
- Connection.prototype.isActive = function isActive() {
195
+ }
196
+ isActive() {
238
197
  return this.isState("open", "connecting");
239
- };
240
- Connection.prototype.isProtocolSupported = function isProtocolSupported() {
198
+ }
199
+ isProtocolSupported() {
241
200
  return indexOf.call(supportedProtocols, this.getProtocol()) >= 0;
242
- };
243
- Connection.prototype.isState = function isState() {
244
- for (var _len = arguments.length, states = Array(_len), _key = 0; _key < _len; _key++) {
245
- states[_key] = arguments[_key];
246
- }
201
+ }
202
+ isState(...states) {
247
203
  return indexOf.call(states, this.getState()) >= 0;
248
- };
249
- Connection.prototype.getState = function getState() {
204
+ }
205
+ getState() {
250
206
  if (this.webSocket) {
251
- for (var state in adapters.WebSocket) {
207
+ for (let state in adapters.WebSocket) {
252
208
  if (adapters.WebSocket[state] === this.webSocket.readyState) {
253
209
  return state.toLowerCase();
254
210
  }
255
211
  }
256
212
  }
257
213
  return null;
258
- };
259
- Connection.prototype.installEventHandlers = function installEventHandlers() {
260
- for (var eventName in this.events) {
261
- var handler = this.events[eventName].bind(this);
262
- this.webSocket["on" + eventName] = handler;
214
+ }
215
+ installEventHandlers() {
216
+ for (let eventName in this.events) {
217
+ const handler = this.events[eventName].bind(this);
218
+ this.webSocket[`on${eventName}`] = handler;
263
219
  }
264
- };
265
- Connection.prototype.uninstallEventHandlers = function uninstallEventHandlers() {
266
- for (var eventName in this.events) {
267
- this.webSocket["on" + eventName] = function() {};
220
+ }
221
+ uninstallEventHandlers() {
222
+ for (let eventName in this.events) {
223
+ this.webSocket[`on${eventName}`] = function() {};
268
224
  }
269
- };
270
- return Connection;
271
- }();
225
+ }
226
+ }
272
227
  Connection.reopenDelay = 500;
273
228
  Connection.prototype.events = {
274
- message: function message(event) {
229
+ message(event) {
275
230
  if (!this.isProtocolSupported()) {
276
231
  return;
277
232
  }
278
- var _JSON$parse = JSON.parse(event.data), identifier = _JSON$parse.identifier, message = _JSON$parse.message, reason = _JSON$parse.reason, reconnect = _JSON$parse.reconnect, type = _JSON$parse.type;
233
+ const {identifier: identifier, message: message, reason: reason, reconnect: reconnect, type: type} = JSON.parse(event.data);
279
234
  switch (type) {
280
235
  case message_types.welcome:
281
236
  this.monitor.recordConnect();
282
237
  return this.subscriptions.reload();
283
238
 
284
239
  case message_types.disconnect:
285
- logger.log("Disconnecting. Reason: " + reason);
240
+ logger.log(`Disconnecting. Reason: ${reason}`);
286
241
  return this.close({
287
242
  allowReconnect: reconnect
288
243
  });
@@ -291,7 +246,6 @@
291
246
  return this.monitor.recordPing();
292
247
 
293
248
  case message_types.confirmation:
294
- this.subscriptions.confirmSubscription(identifier);
295
249
  return this.subscriptions.notify(identifier, "connected");
296
250
 
297
251
  case message_types.rejection:
@@ -301,8 +255,8 @@
301
255
  return this.subscriptions.notify(identifier, "received", message);
302
256
  }
303
257
  },
304
- open: function open() {
305
- logger.log("WebSocket onopen event, using '" + this.getProtocol() + "' subprotocol");
258
+ open() {
259
+ logger.log(`WebSocket onopen event, using '${this.getProtocol()}' subprotocol`);
306
260
  this.disconnected = false;
307
261
  if (!this.isProtocolSupported()) {
308
262
  logger.log("Protocol is unsupported. Stopping monitor and disconnecting.");
@@ -311,7 +265,7 @@
311
265
  });
312
266
  }
313
267
  },
314
- close: function close(event) {
268
+ close(event) {
315
269
  logger.log("WebSocket onclose event");
316
270
  if (this.disconnected) {
317
271
  return;
@@ -322,222 +276,136 @@
322
276
  willAttemptReconnect: this.monitor.isRunning()
323
277
  });
324
278
  },
325
- error: function error() {
279
+ error() {
326
280
  logger.log("WebSocket onerror event");
327
281
  }
328
282
  };
329
- var extend = function extend(object, properties) {
283
+ const extend = function(object, properties) {
330
284
  if (properties != null) {
331
- for (var key in properties) {
332
- var value = properties[key];
285
+ for (let key in properties) {
286
+ const value = properties[key];
333
287
  object[key] = value;
334
288
  }
335
289
  }
336
290
  return object;
337
291
  };
338
- var Subscription = function() {
339
- function Subscription(consumer) {
340
- var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
341
- var mixin = arguments[2];
342
- classCallCheck(this, Subscription);
292
+ class Subscription {
293
+ constructor(consumer, params = {}, mixin) {
343
294
  this.consumer = consumer;
344
295
  this.identifier = JSON.stringify(params);
345
296
  extend(this, mixin);
346
297
  }
347
- Subscription.prototype.perform = function perform(action) {
348
- var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
298
+ perform(action, data = {}) {
349
299
  data.action = action;
350
300
  return this.send(data);
351
- };
352
- Subscription.prototype.send = function send(data) {
301
+ }
302
+ send(data) {
353
303
  return this.consumer.send({
354
304
  command: "message",
355
305
  identifier: this.identifier,
356
306
  data: JSON.stringify(data)
357
307
  });
358
- };
359
- Subscription.prototype.unsubscribe = function unsubscribe() {
308
+ }
309
+ unsubscribe() {
360
310
  return this.consumer.subscriptions.remove(this);
361
- };
362
- return Subscription;
363
- }();
364
- var SubscriptionGuarantor = function() {
365
- function SubscriptionGuarantor(subscriptions) {
366
- classCallCheck(this, SubscriptionGuarantor);
367
- this.subscriptions = subscriptions;
368
- this.pendingSubscriptions = [];
369
- }
370
- SubscriptionGuarantor.prototype.guarantee = function guarantee(subscription) {
371
- if (this.pendingSubscriptions.indexOf(subscription) == -1) {
372
- logger.log("SubscriptionGuarantor guaranteeing " + subscription.identifier);
373
- this.pendingSubscriptions.push(subscription);
374
- } else {
375
- logger.log("SubscriptionGuarantor already guaranteeing " + subscription.identifier);
376
- }
377
- this.startGuaranteeing();
378
- };
379
- SubscriptionGuarantor.prototype.forget = function forget(subscription) {
380
- logger.log("SubscriptionGuarantor forgetting " + subscription.identifier);
381
- this.pendingSubscriptions = this.pendingSubscriptions.filter(function(s) {
382
- return s !== subscription;
383
- });
384
- };
385
- SubscriptionGuarantor.prototype.startGuaranteeing = function startGuaranteeing() {
386
- this.stopGuaranteeing();
387
- this.retrySubscribing();
388
- };
389
- SubscriptionGuarantor.prototype.stopGuaranteeing = function stopGuaranteeing() {
390
- clearTimeout(this.retryTimeout);
391
- };
392
- SubscriptionGuarantor.prototype.retrySubscribing = function retrySubscribing() {
393
- var _this = this;
394
- this.retryTimeout = setTimeout(function() {
395
- if (_this.subscriptions && typeof _this.subscriptions.subscribe === "function") {
396
- _this.pendingSubscriptions.map(function(subscription) {
397
- logger.log("SubscriptionGuarantor resubscribing " + subscription.identifier);
398
- _this.subscriptions.subscribe(subscription);
399
- });
400
- }
401
- }, 500);
402
- };
403
- return SubscriptionGuarantor;
404
- }();
405
- var Subscriptions = function() {
406
- function Subscriptions(consumer) {
407
- classCallCheck(this, Subscriptions);
311
+ }
312
+ }
313
+ class Subscriptions {
314
+ constructor(consumer) {
408
315
  this.consumer = consumer;
409
- this.guarantor = new SubscriptionGuarantor(this);
410
316
  this.subscriptions = [];
411
317
  }
412
- Subscriptions.prototype.create = function create(channelName, mixin) {
413
- var channel = channelName;
414
- var params = (typeof channel === "undefined" ? "undefined" : _typeof(channel)) === "object" ? channel : {
318
+ create(channelName, mixin) {
319
+ const channel = channelName;
320
+ const params = typeof channel === "object" ? channel : {
415
321
  channel: channel
416
322
  };
417
- var subscription = new Subscription(this.consumer, params, mixin);
323
+ const subscription = new Subscription(this.consumer, params, mixin);
418
324
  return this.add(subscription);
419
- };
420
- Subscriptions.prototype.add = function add(subscription) {
325
+ }
326
+ add(subscription) {
421
327
  this.subscriptions.push(subscription);
422
328
  this.consumer.ensureActiveConnection();
423
329
  this.notify(subscription, "initialized");
424
- this.subscribe(subscription);
330
+ this.sendCommand(subscription, "subscribe");
425
331
  return subscription;
426
- };
427
- Subscriptions.prototype.remove = function remove(subscription) {
332
+ }
333
+ remove(subscription) {
428
334
  this.forget(subscription);
429
335
  if (!this.findAll(subscription.identifier).length) {
430
336
  this.sendCommand(subscription, "unsubscribe");
431
337
  }
432
338
  return subscription;
433
- };
434
- Subscriptions.prototype.reject = function reject(identifier) {
435
- var _this = this;
436
- return this.findAll(identifier).map(function(subscription) {
437
- _this.forget(subscription);
438
- _this.notify(subscription, "rejected");
339
+ }
340
+ reject(identifier) {
341
+ return this.findAll(identifier).map((subscription => {
342
+ this.forget(subscription);
343
+ this.notify(subscription, "rejected");
439
344
  return subscription;
440
- });
441
- };
442
- Subscriptions.prototype.forget = function forget(subscription) {
443
- this.guarantor.forget(subscription);
444
- this.subscriptions = this.subscriptions.filter(function(s) {
445
- return s !== subscription;
446
- });
345
+ }));
346
+ }
347
+ forget(subscription) {
348
+ this.subscriptions = this.subscriptions.filter((s => s !== subscription));
447
349
  return subscription;
448
- };
449
- Subscriptions.prototype.findAll = function findAll(identifier) {
450
- return this.subscriptions.filter(function(s) {
451
- return s.identifier === identifier;
452
- });
453
- };
454
- Subscriptions.prototype.reload = function reload() {
455
- var _this2 = this;
456
- return this.subscriptions.map(function(subscription) {
457
- return _this2.subscribe(subscription);
458
- });
459
- };
460
- Subscriptions.prototype.notifyAll = function notifyAll(callbackName) {
461
- var _this3 = this;
462
- for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
463
- args[_key - 1] = arguments[_key];
464
- }
465
- return this.subscriptions.map(function(subscription) {
466
- return _this3.notify.apply(_this3, [ subscription, callbackName ].concat(args));
467
- });
468
- };
469
- Subscriptions.prototype.notify = function notify(subscription, callbackName) {
470
- for (var _len2 = arguments.length, args = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
471
- args[_key2 - 2] = arguments[_key2];
472
- }
473
- var subscriptions = void 0;
350
+ }
351
+ findAll(identifier) {
352
+ return this.subscriptions.filter((s => s.identifier === identifier));
353
+ }
354
+ reload() {
355
+ return this.subscriptions.map((subscription => this.sendCommand(subscription, "subscribe")));
356
+ }
357
+ notifyAll(callbackName, ...args) {
358
+ return this.subscriptions.map((subscription => this.notify(subscription, callbackName, ...args)));
359
+ }
360
+ notify(subscription, callbackName, ...args) {
361
+ let subscriptions;
474
362
  if (typeof subscription === "string") {
475
363
  subscriptions = this.findAll(subscription);
476
364
  } else {
477
365
  subscriptions = [ subscription ];
478
366
  }
479
- return subscriptions.map(function(subscription) {
480
- return typeof subscription[callbackName] === "function" ? subscription[callbackName].apply(subscription, args) : undefined;
481
- });
482
- };
483
- Subscriptions.prototype.subscribe = function subscribe(subscription) {
484
- if (this.sendCommand(subscription, "subscribe")) {
485
- this.guarantor.guarantee(subscription);
486
- }
487
- };
488
- Subscriptions.prototype.confirmSubscription = function confirmSubscription(identifier) {
489
- var _this4 = this;
490
- logger.log("Subscription confirmed " + identifier);
491
- this.findAll(identifier).map(function(subscription) {
492
- return _this4.guarantor.forget(subscription);
493
- });
494
- };
495
- Subscriptions.prototype.sendCommand = function sendCommand(subscription, command) {
496
- var identifier = subscription.identifier;
367
+ return subscriptions.map((subscription => typeof subscription[callbackName] === "function" ? subscription[callbackName](...args) : undefined));
368
+ }
369
+ sendCommand(subscription, command) {
370
+ const {identifier: identifier} = subscription;
497
371
  return this.consumer.send({
498
372
  command: command,
499
373
  identifier: identifier
500
374
  });
501
- };
502
- return Subscriptions;
503
- }();
504
- var Consumer = function() {
505
- function Consumer(url) {
506
- classCallCheck(this, Consumer);
375
+ }
376
+ }
377
+ class Consumer {
378
+ constructor(url) {
507
379
  this._url = url;
508
380
  this.subscriptions = new Subscriptions(this);
509
381
  this.connection = new Connection(this);
510
382
  }
511
- Consumer.prototype.send = function send(data) {
383
+ get url() {
384
+ return createWebSocketURL(this._url);
385
+ }
386
+ send(data) {
512
387
  return this.connection.send(data);
513
- };
514
- Consumer.prototype.connect = function connect() {
388
+ }
389
+ connect() {
515
390
  return this.connection.open();
516
- };
517
- Consumer.prototype.disconnect = function disconnect() {
391
+ }
392
+ disconnect() {
518
393
  return this.connection.close({
519
394
  allowReconnect: false
520
395
  });
521
- };
522
- Consumer.prototype.ensureActiveConnection = function ensureActiveConnection() {
396
+ }
397
+ ensureActiveConnection() {
523
398
  if (!this.connection.isActive()) {
524
399
  return this.connection.open();
525
400
  }
526
- };
527
- createClass(Consumer, [ {
528
- key: "url",
529
- get: function get$$1() {
530
- return createWebSocketURL(this._url);
531
- }
532
- } ]);
533
- return Consumer;
534
- }();
401
+ }
402
+ }
535
403
  function createWebSocketURL(url) {
536
404
  if (typeof url === "function") {
537
405
  url = url();
538
406
  }
539
407
  if (url && !/^wss?:/i.test(url)) {
540
- var a = document.createElement("a");
408
+ const a = document.createElement("a");
541
409
  a.href = url;
542
410
  a.href = a.href;
543
411
  a.protocol = a.protocol.replace("http", "ws");
@@ -546,29 +414,28 @@
546
414
  return url;
547
415
  }
548
416
  }
549
- function createConsumer() {
550
- var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getConfig("url") || INTERNAL.default_mount_path;
417
+ function createConsumer(url = getConfig("url") || INTERNAL.default_mount_path) {
551
418
  return new Consumer(url);
552
419
  }
553
420
  function getConfig(name) {
554
- var element = document.head.querySelector("meta[name='action-cable-" + name + "']");
421
+ const element = document.head.querySelector(`meta[name='action-cable-${name}']`);
555
422
  if (element) {
556
423
  return element.getAttribute("content");
557
424
  }
558
425
  }
426
+ console.log("DEPRECATION: action_cable.js has been renamed to actioncable.js – please update your reference before Rails 8");
559
427
  exports.Connection = Connection;
560
428
  exports.ConnectionMonitor = ConnectionMonitor;
561
429
  exports.Consumer = Consumer;
562
430
  exports.INTERNAL = INTERNAL;
563
431
  exports.Subscription = Subscription;
564
432
  exports.Subscriptions = Subscriptions;
565
- exports.SubscriptionGuarantor = SubscriptionGuarantor;
566
433
  exports.adapters = adapters;
567
- exports.createWebSocketURL = createWebSocketURL;
568
- exports.logger = logger;
569
434
  exports.createConsumer = createConsumer;
435
+ exports.createWebSocketURL = createWebSocketURL;
570
436
  exports.getConfig = getConfig;
437
+ exports.logger = logger;
571
438
  Object.defineProperty(exports, "__esModule", {
572
439
  value: true
573
440
  });
574
- });
441
+ }));