actioncable 6.1.7.8 → 7.0.8.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +78 -69
  3. data/README.md +1 -1
  4. data/app/assets/javascripts/action_cable.js +211 -295
  5. data/app/assets/javascripts/actioncable.esm.js +491 -0
  6. data/app/assets/javascripts/actioncable.js +489 -0
  7. data/lib/action_cable/channel/base.rb +1 -1
  8. data/lib/action_cable/channel/broadcasting.rb +1 -1
  9. data/lib/action_cable/channel/naming.rb +1 -1
  10. data/lib/action_cable/channel/streams.rb +5 -7
  11. data/lib/action_cable/channel/test_case.rb +16 -1
  12. data/lib/action_cable/connection/base.rb +5 -5
  13. data/lib/action_cable/connection/identification.rb +1 -1
  14. data/lib/action_cable/connection/subscriptions.rb +1 -1
  15. data/lib/action_cable/connection/tagged_logger_proxy.rb +3 -3
  16. data/lib/action_cable/connection/test_case.rb +1 -1
  17. data/lib/action_cable/engine.rb +10 -1
  18. data/lib/action_cable/gem_version.rb +5 -5
  19. data/lib/action_cable/helpers/action_cable_helper.rb +3 -2
  20. data/lib/action_cable/server/configuration.rb +1 -0
  21. data/lib/action_cable/server/worker/active_record_connection_management.rb +2 -2
  22. data/lib/action_cable/server/worker.rb +3 -4
  23. data/lib/action_cable/subscription_adapter/postgresql.rb +2 -2
  24. data/lib/action_cable/subscription_adapter/redis.rb +98 -22
  25. data/lib/action_cable/subscription_adapter/test.rb +1 -1
  26. data/lib/action_cable/test_helper.rb +2 -2
  27. data/lib/action_cable/version.rb +1 -1
  28. data/lib/rails/generators/channel/USAGE +1 -1
  29. data/lib/rails/generators/channel/channel_generator.rb +79 -20
  30. data/lib/rails/generators/channel/templates/javascript/index.js.tt +1 -5
  31. metadata +13 -11
  32. /data/lib/rails/generators/channel/templates/application_cable/{channel.rb.tt → channel.rb} +0 -0
  33. /data/lib/rails/generators/channel/templates/application_cable/{connection.rb.tt → connection.rb} +0 -0
@@ -0,0 +1,491 @@
1
+ var adapters = {
2
+ logger: self.console,
3
+ WebSocket: self.WebSocket
4
+ };
5
+
6
+ var logger = {
7
+ log(...messages) {
8
+ if (this.enabled) {
9
+ messages.push(Date.now());
10
+ adapters.logger.log("[ActionCable]", ...messages);
11
+ }
12
+ }
13
+ };
14
+
15
+ const now = () => (new Date).getTime();
16
+
17
+ const secondsSince = time => (now() - time) / 1e3;
18
+
19
+ class ConnectionMonitor {
20
+ constructor(connection) {
21
+ this.visibilityDidChange = this.visibilityDidChange.bind(this);
22
+ this.connection = connection;
23
+ this.reconnectAttempts = 0;
24
+ }
25
+ start() {
26
+ if (!this.isRunning()) {
27
+ this.startedAt = now();
28
+ delete this.stoppedAt;
29
+ this.startPolling();
30
+ addEventListener("visibilitychange", this.visibilityDidChange);
31
+ logger.log(`ConnectionMonitor started. stale threshold = ${this.constructor.staleThreshold} s`);
32
+ }
33
+ }
34
+ stop() {
35
+ if (this.isRunning()) {
36
+ this.stoppedAt = now();
37
+ this.stopPolling();
38
+ removeEventListener("visibilitychange", this.visibilityDidChange);
39
+ logger.log("ConnectionMonitor stopped");
40
+ }
41
+ }
42
+ isRunning() {
43
+ return this.startedAt && !this.stoppedAt;
44
+ }
45
+ recordPing() {
46
+ this.pingedAt = now();
47
+ }
48
+ recordConnect() {
49
+ this.reconnectAttempts = 0;
50
+ this.recordPing();
51
+ delete this.disconnectedAt;
52
+ logger.log("ConnectionMonitor recorded connect");
53
+ }
54
+ recordDisconnect() {
55
+ this.disconnectedAt = now();
56
+ logger.log("ConnectionMonitor recorded disconnect");
57
+ }
58
+ startPolling() {
59
+ this.stopPolling();
60
+ this.poll();
61
+ }
62
+ stopPolling() {
63
+ clearTimeout(this.pollTimeout);
64
+ }
65
+ poll() {
66
+ this.pollTimeout = setTimeout((() => {
67
+ this.reconnectIfStale();
68
+ this.poll();
69
+ }), this.getPollInterval());
70
+ }
71
+ getPollInterval() {
72
+ const {staleThreshold: staleThreshold, reconnectionBackoffRate: reconnectionBackoffRate} = this.constructor;
73
+ const backoff = Math.pow(1 + reconnectionBackoffRate, Math.min(this.reconnectAttempts, 10));
74
+ const jitterMax = this.reconnectAttempts === 0 ? 1 : reconnectionBackoffRate;
75
+ const jitter = jitterMax * Math.random();
76
+ return staleThreshold * 1e3 * backoff * (1 + jitter);
77
+ }
78
+ reconnectIfStale() {
79
+ if (this.connectionIsStale()) {
80
+ logger.log(`ConnectionMonitor detected stale connection. reconnectAttempts = ${this.reconnectAttempts}, time stale = ${secondsSince(this.refreshedAt)} s, stale threshold = ${this.constructor.staleThreshold} s`);
81
+ this.reconnectAttempts++;
82
+ if (this.disconnectedRecently()) {
83
+ logger.log(`ConnectionMonitor skipping reopening recent disconnect. time disconnected = ${secondsSince(this.disconnectedAt)} s`);
84
+ } else {
85
+ logger.log("ConnectionMonitor reopening");
86
+ this.connection.reopen();
87
+ }
88
+ }
89
+ }
90
+ get refreshedAt() {
91
+ return this.pingedAt ? this.pingedAt : this.startedAt;
92
+ }
93
+ connectionIsStale() {
94
+ return secondsSince(this.refreshedAt) > this.constructor.staleThreshold;
95
+ }
96
+ disconnectedRecently() {
97
+ return this.disconnectedAt && secondsSince(this.disconnectedAt) < this.constructor.staleThreshold;
98
+ }
99
+ visibilityDidChange() {
100
+ if (document.visibilityState === "visible") {
101
+ setTimeout((() => {
102
+ if (this.connectionIsStale() || !this.connection.isOpen()) {
103
+ logger.log(`ConnectionMonitor reopening stale connection on visibilitychange. visibilityState = ${document.visibilityState}`);
104
+ this.connection.reopen();
105
+ }
106
+ }), 200);
107
+ }
108
+ }
109
+ }
110
+
111
+ ConnectionMonitor.staleThreshold = 6;
112
+
113
+ ConnectionMonitor.reconnectionBackoffRate = .15;
114
+
115
+ var INTERNAL = {
116
+ message_types: {
117
+ welcome: "welcome",
118
+ disconnect: "disconnect",
119
+ ping: "ping",
120
+ confirmation: "confirm_subscription",
121
+ rejection: "reject_subscription"
122
+ },
123
+ disconnect_reasons: {
124
+ unauthorized: "unauthorized",
125
+ invalid_request: "invalid_request",
126
+ server_restart: "server_restart"
127
+ },
128
+ default_mount_path: "/cable",
129
+ protocols: [ "actioncable-v1-json", "actioncable-unsupported" ]
130
+ };
131
+
132
+ const {message_types: message_types, protocols: protocols} = INTERNAL;
133
+
134
+ const supportedProtocols = protocols.slice(0, protocols.length - 1);
135
+
136
+ const indexOf = [].indexOf;
137
+
138
+ class Connection {
139
+ constructor(consumer) {
140
+ this.open = this.open.bind(this);
141
+ this.consumer = consumer;
142
+ this.subscriptions = this.consumer.subscriptions;
143
+ this.monitor = new ConnectionMonitor(this);
144
+ this.disconnected = true;
145
+ }
146
+ send(data) {
147
+ if (this.isOpen()) {
148
+ this.webSocket.send(JSON.stringify(data));
149
+ return true;
150
+ } else {
151
+ return false;
152
+ }
153
+ }
154
+ open() {
155
+ if (this.isActive()) {
156
+ logger.log(`Attempted to open WebSocket, but existing socket is ${this.getState()}`);
157
+ return false;
158
+ } else {
159
+ logger.log(`Opening WebSocket, current state is ${this.getState()}, subprotocols: ${protocols}`);
160
+ if (this.webSocket) {
161
+ this.uninstallEventHandlers();
162
+ }
163
+ this.webSocket = new adapters.WebSocket(this.consumer.url, protocols);
164
+ this.installEventHandlers();
165
+ this.monitor.start();
166
+ return true;
167
+ }
168
+ }
169
+ close({allowReconnect: allowReconnect} = {
170
+ allowReconnect: true
171
+ }) {
172
+ if (!allowReconnect) {
173
+ this.monitor.stop();
174
+ }
175
+ if (this.isOpen()) {
176
+ return this.webSocket.close();
177
+ }
178
+ }
179
+ reopen() {
180
+ logger.log(`Reopening WebSocket, current state is ${this.getState()}`);
181
+ if (this.isActive()) {
182
+ try {
183
+ return this.close();
184
+ } catch (error) {
185
+ logger.log("Failed to reopen WebSocket", error);
186
+ } finally {
187
+ logger.log(`Reopening WebSocket in ${this.constructor.reopenDelay}ms`);
188
+ setTimeout(this.open, this.constructor.reopenDelay);
189
+ }
190
+ } else {
191
+ return this.open();
192
+ }
193
+ }
194
+ getProtocol() {
195
+ if (this.webSocket) {
196
+ return this.webSocket.protocol;
197
+ }
198
+ }
199
+ isOpen() {
200
+ return this.isState("open");
201
+ }
202
+ isActive() {
203
+ return this.isState("open", "connecting");
204
+ }
205
+ isProtocolSupported() {
206
+ return indexOf.call(supportedProtocols, this.getProtocol()) >= 0;
207
+ }
208
+ isState(...states) {
209
+ return indexOf.call(states, this.getState()) >= 0;
210
+ }
211
+ getState() {
212
+ if (this.webSocket) {
213
+ for (let state in adapters.WebSocket) {
214
+ if (adapters.WebSocket[state] === this.webSocket.readyState) {
215
+ return state.toLowerCase();
216
+ }
217
+ }
218
+ }
219
+ return null;
220
+ }
221
+ installEventHandlers() {
222
+ for (let eventName in this.events) {
223
+ const handler = this.events[eventName].bind(this);
224
+ this.webSocket[`on${eventName}`] = handler;
225
+ }
226
+ }
227
+ uninstallEventHandlers() {
228
+ for (let eventName in this.events) {
229
+ this.webSocket[`on${eventName}`] = function() {};
230
+ }
231
+ }
232
+ }
233
+
234
+ Connection.reopenDelay = 500;
235
+
236
+ Connection.prototype.events = {
237
+ message(event) {
238
+ if (!this.isProtocolSupported()) {
239
+ return;
240
+ }
241
+ const {identifier: identifier, message: message, reason: reason, reconnect: reconnect, type: type} = JSON.parse(event.data);
242
+ switch (type) {
243
+ case message_types.welcome:
244
+ this.monitor.recordConnect();
245
+ return this.subscriptions.reload();
246
+
247
+ case message_types.disconnect:
248
+ logger.log(`Disconnecting. Reason: ${reason}`);
249
+ return this.close({
250
+ allowReconnect: reconnect
251
+ });
252
+
253
+ case message_types.ping:
254
+ return this.monitor.recordPing();
255
+
256
+ case message_types.confirmation:
257
+ this.subscriptions.confirmSubscription(identifier);
258
+ return this.subscriptions.notify(identifier, "connected");
259
+
260
+ case message_types.rejection:
261
+ return this.subscriptions.reject(identifier);
262
+
263
+ default:
264
+ return this.subscriptions.notify(identifier, "received", message);
265
+ }
266
+ },
267
+ open() {
268
+ logger.log(`WebSocket onopen event, using '${this.getProtocol()}' subprotocol`);
269
+ this.disconnected = false;
270
+ if (!this.isProtocolSupported()) {
271
+ logger.log("Protocol is unsupported. Stopping monitor and disconnecting.");
272
+ return this.close({
273
+ allowReconnect: false
274
+ });
275
+ }
276
+ },
277
+ close(event) {
278
+ logger.log("WebSocket onclose event");
279
+ if (this.disconnected) {
280
+ return;
281
+ }
282
+ this.disconnected = true;
283
+ this.monitor.recordDisconnect();
284
+ return this.subscriptions.notifyAll("disconnected", {
285
+ willAttemptReconnect: this.monitor.isRunning()
286
+ });
287
+ },
288
+ error() {
289
+ logger.log("WebSocket onerror event");
290
+ }
291
+ };
292
+
293
+ const extend = function(object, properties) {
294
+ if (properties != null) {
295
+ for (let key in properties) {
296
+ const value = properties[key];
297
+ object[key] = value;
298
+ }
299
+ }
300
+ return object;
301
+ };
302
+
303
+ class Subscription {
304
+ constructor(consumer, params = {}, mixin) {
305
+ this.consumer = consumer;
306
+ this.identifier = JSON.stringify(params);
307
+ extend(this, mixin);
308
+ }
309
+ perform(action, data = {}) {
310
+ data.action = action;
311
+ return this.send(data);
312
+ }
313
+ send(data) {
314
+ return this.consumer.send({
315
+ command: "message",
316
+ identifier: this.identifier,
317
+ data: JSON.stringify(data)
318
+ });
319
+ }
320
+ unsubscribe() {
321
+ return this.consumer.subscriptions.remove(this);
322
+ }
323
+ }
324
+
325
+ class SubscriptionGuarantor {
326
+ constructor(subscriptions) {
327
+ this.subscriptions = subscriptions;
328
+ this.pendingSubscriptions = [];
329
+ }
330
+ guarantee(subscription) {
331
+ if (this.pendingSubscriptions.indexOf(subscription) == -1) {
332
+ logger.log(`SubscriptionGuarantor guaranteeing ${subscription.identifier}`);
333
+ this.pendingSubscriptions.push(subscription);
334
+ } else {
335
+ logger.log(`SubscriptionGuarantor already guaranteeing ${subscription.identifier}`);
336
+ }
337
+ this.startGuaranteeing();
338
+ }
339
+ forget(subscription) {
340
+ logger.log(`SubscriptionGuarantor forgetting ${subscription.identifier}`);
341
+ this.pendingSubscriptions = this.pendingSubscriptions.filter((s => s !== subscription));
342
+ }
343
+ startGuaranteeing() {
344
+ this.stopGuaranteeing();
345
+ this.retrySubscribing();
346
+ }
347
+ stopGuaranteeing() {
348
+ clearTimeout(this.retryTimeout);
349
+ }
350
+ retrySubscribing() {
351
+ this.retryTimeout = setTimeout((() => {
352
+ if (this.subscriptions && typeof this.subscriptions.subscribe === "function") {
353
+ this.pendingSubscriptions.map((subscription => {
354
+ logger.log(`SubscriptionGuarantor resubscribing ${subscription.identifier}`);
355
+ this.subscriptions.subscribe(subscription);
356
+ }));
357
+ }
358
+ }), 500);
359
+ }
360
+ }
361
+
362
+ class Subscriptions {
363
+ constructor(consumer) {
364
+ this.consumer = consumer;
365
+ this.guarantor = new SubscriptionGuarantor(this);
366
+ this.subscriptions = [];
367
+ }
368
+ create(channelName, mixin) {
369
+ const channel = channelName;
370
+ const params = typeof channel === "object" ? channel : {
371
+ channel: channel
372
+ };
373
+ const subscription = new Subscription(this.consumer, params, mixin);
374
+ return this.add(subscription);
375
+ }
376
+ add(subscription) {
377
+ this.subscriptions.push(subscription);
378
+ this.consumer.ensureActiveConnection();
379
+ this.notify(subscription, "initialized");
380
+ this.subscribe(subscription);
381
+ return subscription;
382
+ }
383
+ remove(subscription) {
384
+ this.forget(subscription);
385
+ if (!this.findAll(subscription.identifier).length) {
386
+ this.sendCommand(subscription, "unsubscribe");
387
+ }
388
+ return subscription;
389
+ }
390
+ reject(identifier) {
391
+ return this.findAll(identifier).map((subscription => {
392
+ this.forget(subscription);
393
+ this.notify(subscription, "rejected");
394
+ return subscription;
395
+ }));
396
+ }
397
+ forget(subscription) {
398
+ this.guarantor.forget(subscription);
399
+ this.subscriptions = this.subscriptions.filter((s => s !== subscription));
400
+ return subscription;
401
+ }
402
+ findAll(identifier) {
403
+ return this.subscriptions.filter((s => s.identifier === identifier));
404
+ }
405
+ reload() {
406
+ return this.subscriptions.map((subscription => this.subscribe(subscription)));
407
+ }
408
+ notifyAll(callbackName, ...args) {
409
+ return this.subscriptions.map((subscription => this.notify(subscription, callbackName, ...args)));
410
+ }
411
+ notify(subscription, callbackName, ...args) {
412
+ let subscriptions;
413
+ if (typeof subscription === "string") {
414
+ subscriptions = this.findAll(subscription);
415
+ } else {
416
+ subscriptions = [ subscription ];
417
+ }
418
+ return subscriptions.map((subscription => typeof subscription[callbackName] === "function" ? subscription[callbackName](...args) : undefined));
419
+ }
420
+ subscribe(subscription) {
421
+ if (this.sendCommand(subscription, "subscribe")) {
422
+ this.guarantor.guarantee(subscription);
423
+ }
424
+ }
425
+ confirmSubscription(identifier) {
426
+ logger.log(`Subscription confirmed ${identifier}`);
427
+ this.findAll(identifier).map((subscription => this.guarantor.forget(subscription)));
428
+ }
429
+ sendCommand(subscription, command) {
430
+ const {identifier: identifier} = subscription;
431
+ return this.consumer.send({
432
+ command: command,
433
+ identifier: identifier
434
+ });
435
+ }
436
+ }
437
+
438
+ class Consumer {
439
+ constructor(url) {
440
+ this._url = url;
441
+ this.subscriptions = new Subscriptions(this);
442
+ this.connection = new Connection(this);
443
+ }
444
+ get url() {
445
+ return createWebSocketURL(this._url);
446
+ }
447
+ send(data) {
448
+ return this.connection.send(data);
449
+ }
450
+ connect() {
451
+ return this.connection.open();
452
+ }
453
+ disconnect() {
454
+ return this.connection.close({
455
+ allowReconnect: false
456
+ });
457
+ }
458
+ ensureActiveConnection() {
459
+ if (!this.connection.isActive()) {
460
+ return this.connection.open();
461
+ }
462
+ }
463
+ }
464
+
465
+ function createWebSocketURL(url) {
466
+ if (typeof url === "function") {
467
+ url = url();
468
+ }
469
+ if (url && !/^wss?:/i.test(url)) {
470
+ const a = document.createElement("a");
471
+ a.href = url;
472
+ a.href = a.href;
473
+ a.protocol = a.protocol.replace("http", "ws");
474
+ return a.href;
475
+ } else {
476
+ return url;
477
+ }
478
+ }
479
+
480
+ function createConsumer(url = getConfig("url") || INTERNAL.default_mount_path) {
481
+ return new Consumer(url);
482
+ }
483
+
484
+ function getConfig(name) {
485
+ const element = document.head.querySelector(`meta[name='action-cable-${name}']`);
486
+ if (element) {
487
+ return element.getAttribute("content");
488
+ }
489
+ }
490
+
491
+ export { Connection, ConnectionMonitor, Consumer, INTERNAL, Subscription, SubscriptionGuarantor, Subscriptions, adapters, createConsumer, createWebSocketURL, getConfig, logger };