Skip to content

Instantly share code, notes, and snippets.

@javan
Created November 1, 2018 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javan/387bec1ca052e36a9904177f87fed2de to your computer and use it in GitHub Desktop.
Save javan/387bec1ca052e36a9904177f87fed2de to your computer and use it in GitHub Desktop.
diff --git a/actioncable/.babelrc b/actioncable/.babelrc
index ed751f8745..4f0c469c60 100644
--- a/actioncable/.babelrc
+++ b/actioncable/.babelrc
@@ -1,6 +1,6 @@
{
"presets": [
- ["env", { "modules": false } ]
+ ["env", { "modules": false, "loose": true } ]
],
"plugins": [
"external-helpers"
diff --git a/actioncable/app/assets/javascripts/action_cable.js b/actioncable/app/assets/javascripts/action_cable.js
index 3da4cfff00..cd72f14679 100644
--- a/actioncable/app/assets/javascripts/action_cable.js
+++ b/actioncable/app/assets/javascripts/action_cable.js
@@ -22,30 +22,6 @@
throw new TypeError("Cannot call a class as a function");
}
};
- var createClass = function() {
- function defineProperties(target, props) {
- for (var i = 0; i < props.length; i++) {
- var descriptor = props[i];
- descriptor.enumerable = descriptor.enumerable || false;
- descriptor.configurable = true;
- if ("value" in descriptor) descriptor.writable = true;
- Object.defineProperty(target, descriptor.key, descriptor);
- }
- }
- return function(Constructor, protoProps, staticProps) {
- if (protoProps) defineProperties(Constructor.prototype, protoProps);
- if (staticProps) defineProperties(Constructor, staticProps);
- return Constructor;
- };
- }();
- var toConsumableArray = function(arr) {
- if (Array.isArray(arr)) {
- for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
- return arr2;
- } else {
- return Array.from(arr);
- }
- };
var message_types = INTERNAL.message_types, protocols = INTERNAL.protocols;
var supportedProtocols = protocols.slice(0, protocols.length - 1);
var indexOf = [].indexOf;
@@ -58,119 +34,94 @@
this.monitor = new ActionCable.ConnectionMonitor(this);
this.disconnected = true;
}
- createClass(Connection, [ {
- key: "send",
- value: function send(data) {
- if (this.isOpen()) {
- this.webSocket.send(JSON.stringify(data));
- return true;
- } else {
- return false;
- }
- }
- }, {
- key: "open",
- value: function open() {
- if (this.isActive()) {
- ActionCable.log("Attempted to open WebSocket, but existing socket is " + this.getState());
- return false;
- } else {
- ActionCable.log("Opening WebSocket, current state is " + this.getState() + ", subprotocols: " + protocols);
- if (this.webSocket) {
- this.uninstallEventHandlers();
- }
- this.webSocket = new ActionCable.WebSocket(this.consumer.url, protocols);
- this.installEventHandlers();
- this.monitor.start();
- return true;
- }
- }
- }, {
- key: "close",
- value: function close() {
- var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
- allowReconnect: true
- }, allowReconnect = _ref.allowReconnect;
- if (!allowReconnect) {
- this.monitor.stop();
- }
- if (this.isActive()) {
- return this.webSocket ? this.webSocket.close() : undefined;
- }
+ Connection.prototype.send = function send(data) {
+ if (this.isOpen()) {
+ this.webSocket.send(JSON.stringify(data));
+ return true;
+ } else {
+ return false;
}
- }, {
- key: "reopen",
- value: function reopen() {
- ActionCable.log("Reopening WebSocket, current state is " + this.getState());
- if (this.isActive()) {
- try {
- return this.close();
- } catch (error) {
- ActionCable.log("Failed to reopen WebSocket", error);
- } finally {
- ActionCable.log("Reopening WebSocket in " + this.constructor.reopenDelay + "ms");
- setTimeout(this.open, this.constructor.reopenDelay);
- }
- } else {
- return this.open();
+ };
+ Connection.prototype.open = function open() {
+ if (this.isActive()) {
+ ActionCable.log("Attempted to open WebSocket, but existing socket is " + this.getState());
+ return false;
+ } else {
+ ActionCable.log("Opening WebSocket, current state is " + this.getState() + ", subprotocols: " + protocols);
+ if (this.webSocket) {
+ this.uninstallEventHandlers();
}
+ this.webSocket = new ActionCable.WebSocket(this.consumer.url, protocols);
+ this.installEventHandlers();
+ this.monitor.start();
+ return true;
}
- }, {
- key: "getProtocol",
- value: function getProtocol() {
- return this.webSocket ? this.webSocket.protocol : undefined;
- }
- }, {
- key: "isOpen",
- value: function isOpen() {
- return this.isState("open");
- }
- }, {
- key: "isActive",
- value: function isActive() {
- return this.isState("open", "connecting");
+ };
+ Connection.prototype.close = function close() {
+ var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
+ allowReconnect: true
+ }, allowReconnect = _ref.allowReconnect;
+ if (!allowReconnect) {
+ this.monitor.stop();
}
- }, {
- key: "isProtocolSupported",
- value: function isProtocolSupported() {
- return indexOf.call(supportedProtocols, this.getProtocol()) >= 0;
+ if (this.isActive()) {
+ return this.webSocket ? this.webSocket.close() : undefined;
}
- }, {
- key: "isState",
- value: function isState() {
- for (var _len = arguments.length, states = Array(_len), _key = 0; _key < _len; _key++) {
- states[_key] = arguments[_key];
+ };
+ Connection.prototype.reopen = function reopen() {
+ ActionCable.log("Reopening WebSocket, current state is " + this.getState());
+ if (this.isActive()) {
+ try {
+ return this.close();
+ } catch (error) {
+ ActionCable.log("Failed to reopen WebSocket", error);
+ } finally {
+ ActionCable.log("Reopening WebSocket in " + this.constructor.reopenDelay + "ms");
+ setTimeout(this.open, this.constructor.reopenDelay);
}
- return indexOf.call(states, this.getState()) >= 0;
+ } else {
+ return this.open();
}
- }, {
- key: "getState",
- value: function getState() {
- if (this.webSocket) {
- for (var state in WebSocket) {
- if (WebSocket[state] === this.webSocket.readyState) {
- return state.toLowerCase();
- }
+ };
+ Connection.prototype.getProtocol = function getProtocol() {
+ return this.webSocket ? this.webSocket.protocol : undefined;
+ };
+ Connection.prototype.isOpen = function isOpen() {
+ return this.isState("open");
+ };
+ Connection.prototype.isActive = function isActive() {
+ return this.isState("open", "connecting");
+ };
+ Connection.prototype.isProtocolSupported = function isProtocolSupported() {
+ return indexOf.call(supportedProtocols, this.getProtocol()) >= 0;
+ };
+ Connection.prototype.isState = function isState() {
+ for (var _len = arguments.length, states = Array(_len), _key = 0; _key < _len; _key++) {
+ states[_key] = arguments[_key];
+ }
+ return indexOf.call(states, this.getState()) >= 0;
+ };
+ Connection.prototype.getState = function getState() {
+ if (this.webSocket) {
+ for (var state in WebSocket) {
+ if (WebSocket[state] === this.webSocket.readyState) {
+ return state.toLowerCase();
}
}
- return null;
}
- }, {
- key: "installEventHandlers",
- value: function installEventHandlers() {
- for (var eventName in this.events) {
- var handler = this.events[eventName].bind(this);
- this.webSocket["on" + eventName] = handler;
- }
+ return null;
+ };
+ Connection.prototype.installEventHandlers = function installEventHandlers() {
+ for (var eventName in this.events) {
+ var handler = this.events[eventName].bind(this);
+ this.webSocket["on" + eventName] = handler;
}
- }, {
- key: "uninstallEventHandlers",
- value: function uninstallEventHandlers() {
- for (var eventName in this.events) {
- this.webSocket["on" + eventName] = function() {};
- }
+ };
+ Connection.prototype.uninstallEventHandlers = function uninstallEventHandlers() {
+ for (var eventName in this.events) {
+ this.webSocket["on" + eventName] = function() {};
}
- } ]);
+ };
return Connection;
}();
Connection.reopenDelay = 500;
@@ -239,116 +190,87 @@
this.connection = connection;
this.reconnectAttempts = 0;
}
- createClass(ConnectionMonitor, [ {
- key: "start",
- value: function start() {
- if (!this.isRunning()) {
- this.startedAt = now();
- delete this.stoppedAt;
- this.startPolling();
- document.addEventListener("visibilitychange", this.visibilityDidChange);
- ActionCable.log("ConnectionMonitor started. pollInterval = " + this.getPollInterval() + " ms");
- }
+ ConnectionMonitor.prototype.start = function start() {
+ if (!this.isRunning()) {
+ this.startedAt = now();
+ delete this.stoppedAt;
+ this.startPolling();
+ document.addEventListener("visibilitychange", this.visibilityDidChange);
+ ActionCable.log("ConnectionMonitor started. pollInterval = " + this.getPollInterval() + " ms");
}
- }, {
- key: "stop",
- value: function stop() {
- if (this.isRunning()) {
- this.stoppedAt = now();
- this.stopPolling();
- document.removeEventListener("visibilitychange", this.visibilityDidChange);
- ActionCable.log("ConnectionMonitor stopped");
- }
- }
- }, {
- key: "isRunning",
- value: function isRunning() {
- return this.startedAt && !this.stoppedAt;
- }
- }, {
- key: "recordPing",
- value: function recordPing() {
- this.pingedAt = now();
- }
- }, {
- key: "recordConnect",
- value: function recordConnect() {
- this.reconnectAttempts = 0;
- this.recordPing();
- delete this.disconnectedAt;
- ActionCable.log("ConnectionMonitor recorded connect");
- }
- }, {
- key: "recordDisconnect",
- value: function recordDisconnect() {
- this.disconnectedAt = now();
- ActionCable.log("ConnectionMonitor recorded disconnect");
- }
- }, {
- key: "startPolling",
- value: function startPolling() {
+ };
+ ConnectionMonitor.prototype.stop = function stop() {
+ if (this.isRunning()) {
+ this.stoppedAt = now();
this.stopPolling();
- this.poll();
- }
- }, {
- key: "stopPolling",
- value: function stopPolling() {
- clearTimeout(this.pollTimeout);
+ document.removeEventListener("visibilitychange", this.visibilityDidChange);
+ ActionCable.log("ConnectionMonitor stopped");
}
- }, {
- key: "poll",
- value: function poll() {
- var _this = this;
- this.pollTimeout = setTimeout(function() {
- _this.reconnectIfStale();
- _this.poll();
- }, this.getPollInterval());
- }
- }, {
- key: "getPollInterval",
- value: function getPollInterval() {
- var _constructor$pollInte = this.constructor.pollInterval, min = _constructor$pollInte.min, max = _constructor$pollInte.max;
- var interval = 5 * Math.log(this.reconnectAttempts + 1);
- return Math.round(clamp(interval, min, max) * 1e3);
- }
- }, {
- key: "reconnectIfStale",
- value: function reconnectIfStale() {
- if (this.connectionIsStale()) {
- ActionCable.log("ConnectionMonitor detected stale connection. reconnectAttempts = " + this.reconnectAttempts + ", pollInterval = " + this.getPollInterval() + " ms, time disconnected = " + secondsSince(this.disconnectedAt) + " s, stale threshold = " + this.constructor.staleThreshold + " s");
- this.reconnectAttempts++;
- if (this.disconnectedRecently()) {
- ActionCable.log("ConnectionMonitor skipping reopening recent disconnect");
- } else {
- ActionCable.log("ConnectionMonitor reopening");
- this.connection.reopen();
- }
+ };
+ ConnectionMonitor.prototype.isRunning = function isRunning() {
+ return this.startedAt && !this.stoppedAt;
+ };
+ ConnectionMonitor.prototype.recordPing = function recordPing() {
+ this.pingedAt = now();
+ };
+ ConnectionMonitor.prototype.recordConnect = function recordConnect() {
+ this.reconnectAttempts = 0;
+ this.recordPing();
+ delete this.disconnectedAt;
+ ActionCable.log("ConnectionMonitor recorded connect");
+ };
+ ConnectionMonitor.prototype.recordDisconnect = function recordDisconnect() {
+ this.disconnectedAt = now();
+ ActionCable.log("ConnectionMonitor recorded disconnect");
+ };
+ ConnectionMonitor.prototype.startPolling = function startPolling() {
+ this.stopPolling();
+ this.poll();
+ };
+ ConnectionMonitor.prototype.stopPolling = function stopPolling() {
+ clearTimeout(this.pollTimeout);
+ };
+ ConnectionMonitor.prototype.poll = function poll() {
+ var _this = this;
+ this.pollTimeout = setTimeout(function() {
+ _this.reconnectIfStale();
+ _this.poll();
+ }, this.getPollInterval());
+ };
+ ConnectionMonitor.prototype.getPollInterval = function getPollInterval() {
+ var _constructor$pollInte = this.constructor.pollInterval, min = _constructor$pollInte.min, max = _constructor$pollInte.max;
+ var interval = 5 * Math.log(this.reconnectAttempts + 1);
+ return Math.round(clamp(interval, min, max) * 1e3);
+ };
+ ConnectionMonitor.prototype.reconnectIfStale = function reconnectIfStale() {
+ if (this.connectionIsStale()) {
+ ActionCable.log("ConnectionMonitor detected stale connection. reconnectAttempts = " + this.reconnectAttempts + ", pollInterval = " + this.getPollInterval() + " ms, time disconnected = " + secondsSince(this.disconnectedAt) + " s, stale threshold = " + this.constructor.staleThreshold + " s");
+ this.reconnectAttempts++;
+ if (this.disconnectedRecently()) {
+ ActionCable.log("ConnectionMonitor skipping reopening recent disconnect");
+ } else {
+ ActionCable.log("ConnectionMonitor reopening");
+ this.connection.reopen();
}
}
- }, {
- key: "connectionIsStale",
- value: function connectionIsStale() {
- return secondsSince(this.pingedAt ? this.pingedAt : this.startedAt) > this.constructor.staleThreshold;
- }
- }, {
- key: "disconnectedRecently",
- value: function disconnectedRecently() {
- return this.disconnectedAt && secondsSince(this.disconnectedAt) < this.constructor.staleThreshold;
- }
- }, {
- key: "visibilityDidChange",
- value: function visibilityDidChange() {
- var _this2 = this;
- if (document.visibilityState === "visible") {
- setTimeout(function() {
- if (_this2.connectionIsStale() || !_this2.connection.isOpen()) {
- ActionCable.log("ConnectionMonitor reopening stale connection on visibilitychange. visbilityState = " + document.visibilityState);
- _this2.connection.reopen();
- }
- }, 200);
- }
+ };
+ ConnectionMonitor.prototype.connectionIsStale = function connectionIsStale() {
+ return secondsSince(this.pingedAt ? this.pingedAt : this.startedAt) > this.constructor.staleThreshold;
+ };
+ ConnectionMonitor.prototype.disconnectedRecently = function disconnectedRecently() {
+ return this.disconnectedAt && secondsSince(this.disconnectedAt) < this.constructor.staleThreshold;
+ };
+ ConnectionMonitor.prototype.visibilityDidChange = function visibilityDidChange() {
+ var _this2 = this;
+ if (document.visibilityState === "visible") {
+ setTimeout(function() {
+ if (_this2.connectionIsStale() || !_this2.connection.isOpen()) {
+ ActionCable.log("ConnectionMonitor reopening stale connection on visibilitychange. visbilityState = " + document.visibilityState);
+ _this2.connection.reopen();
+ }
+ }, 200);
}
- } ]);
+ };
return ConnectionMonitor;
}();
ConnectionMonitor.pollInterval = {
@@ -363,31 +285,22 @@
this.subscriptions = new ActionCable.Subscriptions(this);
this.connection = new ActionCable.Connection(this);
}
- createClass(Consumer, [ {
- key: "send",
- value: function send(data) {
- return this.connection.send(data);
- }
- }, {
- key: "connect",
- value: function connect() {
+ Consumer.prototype.send = function send(data) {
+ return this.connection.send(data);
+ };
+ Consumer.prototype.connect = function connect() {
+ return this.connection.open();
+ };
+ Consumer.prototype.disconnect = function disconnect() {
+ return this.connection.close({
+ allowReconnect: false
+ });
+ };
+ Consumer.prototype.ensureActiveConnection = function ensureActiveConnection() {
+ if (!this.connection.isActive()) {
return this.connection.open();
}
- }, {
- key: "disconnect",
- value: function disconnect() {
- return this.connection.close({
- allowReconnect: false
- });
- }
- }, {
- key: "ensureActiveConnection",
- value: function ensureActiveConnection() {
- if (!this.connection.isActive()) {
- return this.connection.open();
- }
- }
- } ]);
+ };
return Consumer;
}();
var extend = function extend(object, properties) {
@@ -408,28 +321,21 @@
this.identifier = JSON.stringify(params);
extend(this, mixin);
}
- createClass(Subscription, [ {
- key: "perform",
- value: function perform(action) {
- var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
- data.action = action;
- return this.send(data);
- }
- }, {
- key: "send",
- value: function send(data) {
- return this.consumer.send({
- command: "message",
- identifier: this.identifier,
- data: JSON.stringify(data)
- });
- }
- }, {
- key: "unsubscribe",
- value: function unsubscribe() {
- return this.consumer.subscriptions.remove(this);
- }
- } ]);
+ Subscription.prototype.perform = function perform(action) {
+ var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
+ data.action = action;
+ return this.send(data);
+ };
+ Subscription.prototype.send = function send(data) {
+ return this.consumer.send({
+ command: "message",
+ identifier: this.identifier,
+ data: JSON.stringify(data)
+ });
+ };
+ Subscription.prototype.unsubscribe = function unsubscribe() {
+ return this.consumer.subscriptions.remove(this);
+ };
return Subscription;
}();
var Subscriptions = function() {
@@ -438,104 +344,83 @@
this.consumer = consumer;
this.subscriptions = [];
}
- createClass(Subscriptions, [ {
- key: "create",
- value: function create(channelName, mixin) {
- var channel = channelName;
- var params = (typeof channel === "undefined" ? "undefined" : _typeof(channel)) === "object" ? channel : {
- channel: channel
- };
- var subscription = new ActionCable.Subscription(this.consumer, params, mixin);
- return this.add(subscription);
- }
- }, {
- key: "add",
- value: function add(subscription) {
- this.subscriptions.push(subscription);
- this.consumer.ensureActiveConnection();
- this.notify(subscription, "initialized");
- this.sendCommand(subscription, "subscribe");
- return subscription;
- }
- }, {
- key: "remove",
- value: function remove(subscription) {
- this.forget(subscription);
- if (!this.findAll(subscription.identifier).length) {
- this.sendCommand(subscription, "unsubscribe");
- }
- return subscription;
- }
- }, {
- key: "reject",
- value: function reject(identifier) {
- var _this = this;
- return this.findAll(identifier).map(function(subscription) {
- _this.forget(subscription);
- _this.notify(subscription, "rejected");
- return subscription;
- });
+ Subscriptions.prototype.create = function create(channelName, mixin) {
+ var channel = channelName;
+ var params = (typeof channel === "undefined" ? "undefined" : _typeof(channel)) === "object" ? channel : {
+ channel: channel
+ };
+ var subscription = new ActionCable.Subscription(this.consumer, params, mixin);
+ return this.add(subscription);
+ };
+ Subscriptions.prototype.add = function add(subscription) {
+ this.subscriptions.push(subscription);
+ this.consumer.ensureActiveConnection();
+ this.notify(subscription, "initialized");
+ this.sendCommand(subscription, "subscribe");
+ return subscription;
+ };
+ Subscriptions.prototype.remove = function remove(subscription) {
+ this.forget(subscription);
+ if (!this.findAll(subscription.identifier).length) {
+ this.sendCommand(subscription, "unsubscribe");
}
- }, {
- key: "forget",
- value: function forget(subscription) {
- this.subscriptions = this.subscriptions.filter(function(s) {
- return s !== subscription;
- });
+ return subscription;
+ };
+ Subscriptions.prototype.reject = function reject(identifier) {
+ var _this = this;
+ return this.findAll(identifier).map(function(subscription) {
+ _this.forget(subscription);
+ _this.notify(subscription, "rejected");
return subscription;
+ });
+ };
+ Subscriptions.prototype.forget = function forget(subscription) {
+ this.subscriptions = this.subscriptions.filter(function(s) {
+ return s !== subscription;
+ });
+ return subscription;
+ };
+ Subscriptions.prototype.findAll = function findAll(identifier) {
+ return this.subscriptions.filter(function(s) {
+ return s.identifier === identifier;
+ });
+ };
+ Subscriptions.prototype.reload = function reload() {
+ var _this2 = this;
+ return this.subscriptions.map(function(subscription) {
+ return _this2.sendCommand(subscription, "subscribe");
+ });
+ };
+ Subscriptions.prototype.notifyAll = function notifyAll(callbackName) {
+ var _this3 = this;
+ for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
+ args[_key - 1] = arguments[_key];
}
- }, {
- key: "findAll",
- value: function findAll(identifier) {
- return this.subscriptions.filter(function(s) {
- return s.identifier === identifier;
- });
- }
- }, {
- key: "reload",
- value: function reload() {
- var _this2 = this;
- return this.subscriptions.map(function(subscription) {
- return _this2.sendCommand(subscription, "subscribe");
- });
- }
- }, {
- key: "notifyAll",
- value: function notifyAll(callbackName) {
- var _this3 = this;
- for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
- args[_key - 1] = arguments[_key];
- }
- return this.subscriptions.map(function(subscription) {
- return _this3.notify.apply(_this3, [ subscription, callbackName ].concat(args));
- });
- }
- }, {
- key: "notify",
- value: function notify(subscription, callbackName) {
- for (var _len2 = arguments.length, args = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
- args[_key2 - 2] = arguments[_key2];
- }
- var subscriptions = void 0;
- if (typeof subscription === "string") {
- subscriptions = this.findAll(subscription);
- } else {
- subscriptions = [ subscription ];
- }
- return subscriptions.map(function(subscription) {
- return typeof subscription[callbackName] === "function" ? subscription[callbackName].apply(subscription, args) : undefined;
- });
+ return this.subscriptions.map(function(subscription) {
+ return _this3.notify.apply(_this3, [ subscription, callbackName ].concat(args));
+ });
+ };
+ Subscriptions.prototype.notify = function notify(subscription, callbackName) {
+ for (var _len2 = arguments.length, args = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
+ args[_key2 - 2] = arguments[_key2];
}
- }, {
- key: "sendCommand",
- value: function sendCommand(subscription, command) {
- var identifier = subscription.identifier;
- return this.consumer.send({
- command: command,
- identifier: identifier
- });
+ var subscriptions = void 0;
+ if (typeof subscription === "string") {
+ subscriptions = this.findAll(subscription);
+ } else {
+ subscriptions = [ subscription ];
}
- } ]);
+ return subscriptions.map(function(subscription) {
+ return typeof subscription[callbackName] === "function" ? subscription[callbackName].apply(subscription, args) : undefined;
+ });
+ };
+ Subscriptions.prototype.sendCommand = function sendCommand(subscription, command) {
+ var identifier = subscription.identifier;
+ return this.consumer.send({
+ command: command,
+ identifier: identifier
+ });
+ };
return Subscriptions;
}();
var ActionCable = {
@@ -576,7 +461,7 @@
messages[_key] = arguments[_key];
}
messages.push(Date.now());
- (_logger = this.logger).log.apply(_logger, [ "[ActionCable]" ].concat(toConsumableArray(messages)));
+ (_logger = this.logger).log.apply(_logger, [ "[ActionCable]" ].concat(messages));
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment