diff --git a/action_cable_react_jwt.js b/action_cable_react_jwt.js index fe37797..401439f 100644 --- a/action_cable_react_jwt.js +++ b/action_cable_react_jwt.js @@ -1,11 +1,11 @@ -(function() { - (function() { - (function() { +(function () { + (function () { + (function () { var slice = [].slice; global.document = { - addEventListener: function() {}, - removeEventListener: function() {} + addEventListener: function () { }, + removeEventListener: function () { } } this.ActionCable = { @@ -19,23 +19,23 @@ "default_mount_path": "/cable", "protocols": ["actioncable-v1-json", "actioncable-unsupported"] }, - createConsumer: function(url, jwt) { + createConsumer: function (url, jwt) { var ref; if (url == null) { url = (ref = this.getConfig("url")) != null ? ref : this.INTERNAL.default_mount_path; } return new ActionCable.Consumer(this.createWebSocketURL(url), jwt); }, - getConfig: function(name) { - if(!document.head) return null; + getConfig: function (name) { + if (!document.head) return null; var element; element = document.head.querySelector("meta[name='action-cable-" + name + "']"); return element != null ? element.getAttribute("content") : void 0; }, - createWebSocketURL: function(url) { + createWebSocketURL: function (url) { var a; if (url && !/^wss?:/i.test(url) - && Object.getOwnPropertyNames(document).includes("createElement")) { + && Object.getOwnPropertyNames(document).includes("createElement")) { a = document.createElement("a"); a.href = url; a.href = a.href; @@ -45,13 +45,13 @@ return url.replace(/^http/, 'ws'); } }, - startDebugging: function() { + startDebugging: function () { return this.debugging = true; }, - stopDebugging: function() { + stopDebugging: function () { return this.debugging = null; }, - log: function() { + log: function () { var messages; messages = 1 <= arguments.length ? slice.call(arguments, 0) : []; if (this.debugging) { @@ -66,11 +66,11 @@ var ActionCable = this.ActionCable; - (function() { - (function() { - var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; + (function () { + (function () { + var bind = function (fn, me) { return function () { return fn.apply(me, arguments); }; }; - ActionCable.ConnectionMonitor = (function() { + ActionCable.ConnectionMonitor = (function () { var clamp, now, secondsSince; ConnectionMonitor.pollInterval = { @@ -86,7 +86,7 @@ this.reconnectAttempts = 0; } - ConnectionMonitor.prototype.start = function() { + ConnectionMonitor.prototype.start = function () { if (!this.isRunning()) { this.startedAt = now(); delete this.stoppedAt; @@ -96,7 +96,7 @@ } }; - ConnectionMonitor.prototype.stop = function() { + ConnectionMonitor.prototype.stop = function () { if (this.isRunning()) { this.stoppedAt = now(); this.stopPolling(); @@ -105,52 +105,52 @@ } }; - ConnectionMonitor.prototype.isRunning = function() { + ConnectionMonitor.prototype.isRunning = function () { return (this.startedAt != null) && (this.stoppedAt == null); }; - ConnectionMonitor.prototype.recordPing = function() { + ConnectionMonitor.prototype.recordPing = function () { return this.pingedAt = now(); }; - ConnectionMonitor.prototype.recordConnect = function() { + ConnectionMonitor.prototype.recordConnect = function () { this.reconnectAttempts = 0; this.recordPing(); delete this.disconnectedAt; return ActionCable.log("ConnectionMonitor recorded connect"); }; - ConnectionMonitor.prototype.recordDisconnect = function() { + ConnectionMonitor.prototype.recordDisconnect = function () { this.disconnectedAt = now(); return ActionCable.log("ConnectionMonitor recorded disconnect"); }; - ConnectionMonitor.prototype.startPolling = function() { + ConnectionMonitor.prototype.startPolling = function () { this.stopPolling(); return this.poll(); }; - ConnectionMonitor.prototype.stopPolling = function() { + ConnectionMonitor.prototype.stopPolling = function () { return clearTimeout(this.pollTimeout); }; - ConnectionMonitor.prototype.poll = function() { - return this.pollTimeout = setTimeout((function(_this) { - return function() { + ConnectionMonitor.prototype.poll = function () { + return this.pollTimeout = setTimeout((function (_this) { + return function () { _this.reconnectIfStale(); return _this.poll(); }; })(this), this.getPollInterval()); }; - ConnectionMonitor.prototype.getPollInterval = function() { + ConnectionMonitor.prototype.getPollInterval = function () { var interval, max, min, ref; ref = this.constructor.pollInterval, min = ref.min, max = ref.max; interval = 5 * Math.log(this.reconnectAttempts + 1); return Math.round(clamp(interval, min, max) * 1000); }; - ConnectionMonitor.prototype.reconnectIfStale = function() { + ConnectionMonitor.prototype.reconnectIfStale = function () { 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++; @@ -163,19 +163,19 @@ } }; - ConnectionMonitor.prototype.connectionIsStale = function() { + ConnectionMonitor.prototype.connectionIsStale = function () { var ref; return secondsSince((ref = this.pingedAt) != null ? ref : this.startedAt) > this.constructor.staleThreshold; }; - ConnectionMonitor.prototype.disconnectedRecently = function() { + ConnectionMonitor.prototype.disconnectedRecently = function () { return this.disconnectedAt && secondsSince(this.disconnectedAt) < this.constructor.staleThreshold; }; - ConnectionMonitor.prototype.visibilityDidChange = function() { + ConnectionMonitor.prototype.visibilityDidChange = function () { if (document.visibilityState === "visible") { - return setTimeout((function(_this) { - return function() { + return setTimeout((function (_this) { + return function () { if (_this.connectionIsStale() || !_this.connection.isOpen()) { ActionCable.log("ConnectionMonitor reopening stale connection on visibilitychange. visbilityState = " + document.visibilityState); return _this.connection.reopen(); @@ -185,15 +185,15 @@ } }; - now = function() { + now = function () { return new Date().getTime(); }; - secondsSince = function(time) { + secondsSince = function (time) { return (now() - time) / 1000; }; - clamp = function(number, min, max) { + clamp = function (number, min, max) { return Math.max(min, Math.min(max, number)); }; @@ -202,17 +202,17 @@ })(); }).call(this); - (function() { + (function () { var i, message_types, protocols, ref, supportedProtocols, unsupportedProtocol, slice = [].slice, - bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, - indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; + bind = function (fn, me) { return function () { return fn.apply(me, arguments); }; }, + indexOf = [].indexOf || function (item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; ref = ActionCable.INTERNAL, message_types = ref.message_types, protocols = ref.protocols; supportedProtocols = 2 <= protocols.length ? slice.call(protocols, 0, i = protocols.length - 1) : (i = 0, []), unsupportedProtocol = protocols[i++]; - ActionCable.Connection = (function() { + ActionCable.Connection = (function () { Connection.reopenDelay = 500; function Connection(consumer) { @@ -223,7 +223,7 @@ this.disconnected = true; } - Connection.prototype.send = function(data) { + Connection.prototype.send = function (data) { if (this.isOpen()) { this.webSocket.send(JSON.stringify(data)); return true; @@ -232,7 +232,7 @@ } }; - Connection.prototype.open = function() { + Connection.prototype.open = function () { if (this.isActive()) { ActionCable.log("Attempted to open WebSocket, but existing socket is " + (this.getState())); throw new Error("Existing connection must be closed before opening"); @@ -249,11 +249,11 @@ } }; - Connection.prototype.close = function(arg) { + Connection.prototype.close = function (arg) { var allowReconnect, ref1; allowReconnect = (arg != null ? arg : { - allowReconnect: true - }).allowReconnect; + allowReconnect: true + }).allowReconnect; if (!allowReconnect) { this.monitor.stop(); } @@ -262,7 +262,7 @@ } }; - Connection.prototype.reopen = function() { + Connection.prototype.reopen = function () { var error, error1; ActionCable.log("Reopening WebSocket, current state is " + (this.getState())); if (this.isActive()) { @@ -280,31 +280,31 @@ } }; - Connection.prototype.getProtocol = function() { + Connection.prototype.getProtocol = function () { var ref1; return (ref1 = this.webSocket) != null ? ref1.protocol : void 0; }; - Connection.prototype.isOpen = function() { + Connection.prototype.isOpen = function () { return this.isState("open"); }; - Connection.prototype.isActive = function() { + Connection.prototype.isActive = function () { return this.isState("open", "connecting"); }; - Connection.prototype.isProtocolSupported = function() { + Connection.prototype.isProtocolSupported = function () { var ref1; return ref1 = this.getProtocol(), indexOf.call(supportedProtocols, ref1) >= 0; }; - Connection.prototype.isState = function() { + Connection.prototype.isState = function () { var ref1, states; states = 1 <= arguments.length ? slice.call(arguments, 0) : []; return ref1 = this.getState(), indexOf.call(states, ref1) >= 0; }; - Connection.prototype.getState = function() { + Connection.prototype.getState = function () { var ref1, state, value; for (state in WebSocket) { value = WebSocket[state]; @@ -315,7 +315,7 @@ return null; }; - Connection.prototype.installEventHandlers = function() { + Connection.prototype.installEventHandlers = function () { var eventName, handler; for (eventName in this.events) { handler = this.events[eventName].bind(this); @@ -323,15 +323,15 @@ } }; - Connection.prototype.uninstallEventHandlers = function() { + Connection.prototype.uninstallEventHandlers = function () { var eventName; for (eventName in this.events) { - this.webSocket["on" + eventName] = function() {}; + this.webSocket["on" + eventName] = function () { }; } }; Connection.prototype.events = { - message: function(event) { + message: function (event) { var identifier, message, ref1, type; if (!this.isProtocolSupported()) { return; @@ -351,7 +351,7 @@ return this.subscriptions.notify(identifier, "received", message); } }, - open: function() { + open: function () { ActionCable.log("WebSocket onopen event, using '" + (this.getProtocol()) + "' subprotocol"); this.disconnected = false; if (!this.isProtocolSupported()) { @@ -361,7 +361,7 @@ }); } }, - close: function(event) { + close: function (event) { ActionCable.log("WebSocket onclose event"); if (this.disconnected) { return; @@ -372,7 +372,7 @@ willAttemptReconnect: this.monitor.isRunning() }); }, - error: function() { + error: function () { return ActionCable.log("WebSocket onerror event"); } }; @@ -382,26 +382,26 @@ })(); }).call(this); - (function() { + (function () { var slice = [].slice; - ActionCable.Subscriptions = (function() { + ActionCable.Subscriptions = (function () { function Subscriptions(consumer) { this.consumer = consumer; this.subscriptions = []; } - Subscriptions.prototype.create = function(channelName, mixin) { + Subscriptions.prototype.create = function (channelName, mixin) { var channel, params, subscription; channel = channelName; params = typeof channel === "object" ? channel : { - channel: channel - }; + channel: channel + }; subscription = new ActionCable.Subscription(this.consumer, params, mixin); return this.add(subscription); }; - Subscriptions.prototype.add = function(subscription) { + Subscriptions.prototype.add = function (subscription) { this.subscriptions.push(subscription); this.consumer.ensureActiveConnection(); this.notify(subscription, "initialized"); @@ -409,7 +409,7 @@ return subscription; }; - Subscriptions.prototype.remove = function(subscription) { + Subscriptions.prototype.remove = function (subscription) { this.forget(subscription); if (!this.findAll(subscription.identifier).length) { this.sendCommand(subscription, "unsubscribe"); @@ -417,7 +417,7 @@ return subscription; }; - Subscriptions.prototype.reject = function(identifier) { + Subscriptions.prototype.reject = function (identifier) { var i, len, ref, results, subscription; ref = this.findAll(identifier); results = []; @@ -430,9 +430,9 @@ return results; }; - Subscriptions.prototype.forget = function(subscription) { + Subscriptions.prototype.forget = function (subscription) { var s; - this.subscriptions = (function() { + this.subscriptions = (function () { var i, len, ref, results; ref = this.subscriptions; results = []; @@ -447,7 +447,7 @@ return subscription; }; - Subscriptions.prototype.findAll = function(identifier) { + Subscriptions.prototype.findAll = function (identifier) { var i, len, ref, results, s; ref = this.subscriptions; results = []; @@ -460,7 +460,7 @@ return results; }; - Subscriptions.prototype.reload = function() { + Subscriptions.prototype.reload = function () { var i, len, ref, results, subscription; ref = this.subscriptions; results = []; @@ -471,7 +471,7 @@ return results; }; - Subscriptions.prototype.notifyAll = function() { + Subscriptions.prototype.notifyAll = function () { var args, callbackName, i, len, ref, results, subscription; callbackName = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : []; ref = this.subscriptions; @@ -483,7 +483,7 @@ return results; }; - Subscriptions.prototype.notify = function() { + Subscriptions.prototype.notify = function () { var args, callbackName, i, len, results, subscription, subscriptions; subscription = arguments[0], callbackName = arguments[1], args = 3 <= arguments.length ? slice.call(arguments, 2) : []; if (typeof subscription === "string") { @@ -494,12 +494,16 @@ results = []; for (i = 0, len = subscriptions.length; i < len; i++) { subscription = subscriptions[i]; - results.push(typeof subscription[callbackName] === "function" ? subscription[callbackName].apply(subscription, args) : void 0); + if (subscription && typeof subscription[callbackName] === "function") { + results.push(subscription[callbackName].apply(subscription, args)); + } else { + results.push(void 0); + } } return results; }; - Subscriptions.prototype.sendCommand = function(subscription, command) { + Subscriptions.prototype.sendCommand = function (subscription, command) { var identifier; identifier = subscription.identifier; return this.consumer.send({ @@ -513,8 +517,8 @@ })(); }).call(this); - (function() { - ActionCable.Subscription = (function() { + (function () { + ActionCable.Subscription = (function () { var extend; function Subscription(consumer, params, mixin) { @@ -526,7 +530,7 @@ extend(this, mixin); } - Subscription.prototype.perform = function(action, data) { + Subscription.prototype.perform = function (action, data) { if (data == null) { data = {}; } @@ -534,7 +538,7 @@ return this.send(data); }; - Subscription.prototype.send = function(data) { + Subscription.prototype.send = function (data) { return this.consumer.send({ command: "message", identifier: this.identifier, @@ -542,11 +546,11 @@ }); }; - Subscription.prototype.unsubscribe = function() { + Subscription.prototype.unsubscribe = function () { return this.consumer.subscriptions.remove(this); }; - extend = function(object, properties) { + extend = function (object, properties) { var key, value; if (properties != null) { for (key in properties) { @@ -562,8 +566,8 @@ })(); }).call(this); - (function() { - ActionCable.Consumer = (function() { + (function () { + ActionCable.Consumer = (function () { function Consumer(url, jwt) { this.url = url; this.jwt = jwt; @@ -571,21 +575,21 @@ this.connection = new ActionCable.Connection(this); } - Consumer.prototype.send = function(data) { + Consumer.prototype.send = function (data) { return this.connection.send(data); }; - Consumer.prototype.connect = function() { + Consumer.prototype.connect = function () { return this.connection.open(); }; - Consumer.prototype.disconnect = function() { + Consumer.prototype.disconnect = function () { return this.connection.close({ allowReconnect: false }); }; - Consumer.prototype.ensureActiveConnection = function() { + Consumer.prototype.ensureActiveConnection = function () { if (!this.connection.isActive()) { return this.connection.open(); }