actioncable
Advanced tools
Comparing version 5.0.0-beta1-3 to 5.0.0-beta3
@@ -0,3 +1,9 @@ | ||
!function () { | ||
var ActionCable | ||
// fix scope | ||
(function() { | ||
var slice = [].slice; | ||
ActionCable = { | ||
@@ -35,2 +41,16 @@ INTERNAL: { | ||
} | ||
}, | ||
startDebugging: function() { | ||
return this.debugging = true; | ||
}, | ||
stopDebugging: function() { | ||
return this.debugging = null; | ||
}, | ||
log: function() { | ||
var messages; | ||
messages = 1 <= arguments.length ? slice.call(arguments, 0) : []; | ||
if (this.debugging) { | ||
messages.push(Date.now()); | ||
return console.log.apply(console, ["[ActionCable]"].concat(slice.call(messages))); | ||
} | ||
} | ||
@@ -67,5 +87,10 @@ }; | ||
Connection.prototype.open = function() { | ||
if (this.webSocket && !this.isState("closed")) { | ||
if (this.isAlive()) { | ||
ActionCable.log("Attemped to open WebSocket, but existing socket is " + (this.getState())); | ||
throw new Error("Existing connection must be closed before opening"); | ||
} else { | ||
ActionCable.log("Opening WebSocket, current state is " + (this.getState())); | ||
if (this.webSocket != null) { | ||
this.uninstallEventHandlers(); | ||
} | ||
this.webSocket = new WebSocket(this.consumer.url); | ||
@@ -83,10 +108,16 @@ this.installEventHandlers(); | ||
Connection.prototype.reopen = function() { | ||
if (this.isState("closed")) { | ||
return this.open(); | ||
} else { | ||
var error, error1; | ||
ActionCable.log("Reopening WebSocket, current state is " + (this.getState())); | ||
if (this.isAlive()) { | ||
try { | ||
return this.close(); | ||
} catch (error1) { | ||
error = error1; | ||
return 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(); | ||
} | ||
@@ -99,2 +130,6 @@ }; | ||
Connection.prototype.isAlive = function() { | ||
return (this.webSocket != null) && !this.isState("closing", "closed"); | ||
}; | ||
Connection.prototype.isState = function() { | ||
@@ -125,2 +160,9 @@ var ref, states; | ||
Connection.prototype.uninstallEventHandlers = function() { | ||
var eventName; | ||
for (eventName in this.events) { | ||
this.webSocket["on" + eventName] = function() {}; | ||
} | ||
}; | ||
Connection.prototype.events = { | ||
@@ -140,2 +182,3 @@ message: function(event) { | ||
open: function() { | ||
ActionCable.log("WebSocket onopen event"); | ||
this.disconnected = false; | ||
@@ -145,5 +188,7 @@ return this.consumer.subscriptions.reload(); | ||
close: function() { | ||
ActionCable.log("WebSocket onclose event"); | ||
return this.disconnect(); | ||
}, | ||
error: function() { | ||
ActionCable.log("WebSocket onerror event"); | ||
return this.disconnect(); | ||
@@ -161,8 +206,2 @@ } | ||
Connection.prototype.toJSON = function() { | ||
return { | ||
state: this.getState() | ||
}; | ||
}; | ||
return Connection; | ||
@@ -198,3 +237,4 @@ | ||
this.pingedAt = now(); | ||
return delete this.disconnectedAt; | ||
delete this.disconnectedAt; | ||
return ActionCable.log("ConnectionMonitor connected"); | ||
}; | ||
@@ -219,3 +259,4 @@ | ||
this.poll(); | ||
return document.addEventListener("visibilitychange", this.visibilityDidChange); | ||
document.addEventListener("visibilitychange", this.visibilityDidChange); | ||
return ActionCable.log("ConnectionMonitor started, pollInterval is " + (this.getInterval()) + "ms"); | ||
}; | ||
@@ -225,3 +266,4 @@ | ||
this.stoppedAt = now(); | ||
return document.removeEventListener("visibilitychange", this.visibilityDidChange); | ||
document.removeEventListener("visibilitychange", this.visibilityDidChange); | ||
return ActionCable.log("ConnectionMonitor stopped"); | ||
}; | ||
@@ -249,4 +291,8 @@ | ||
if (this.connectionIsStale()) { | ||
ActionCable.log("ConnectionMonitor detected stale connection, reconnectAttempts = " + this.reconnectAttempts); | ||
this.reconnectAttempts++; | ||
if (!this.disconnectedRecently()) { | ||
if (this.disconnectedRecently()) { | ||
return ActionCable.log("ConnectionMonitor skipping reopen because recently disconnected at " + this.disconnectedAt); | ||
} else { | ||
ActionCable.log("ConnectionMonitor reopening"); | ||
return this.consumer.connection.reopen(); | ||
@@ -271,2 +317,3 @@ } | ||
if (_this.connectionIsStale() || !_this.consumer.connection.isOpen()) { | ||
ActionCable.log("ConnectionMonitor reopening stale connection after visibilitychange to " + document.visibilityState); | ||
return _this.consumer.connection.reopen(); | ||
@@ -279,16 +326,2 @@ } | ||
ConnectionMonitor.prototype.toJSON = function() { | ||
var connectionIsStale, interval; | ||
interval = this.getInterval(); | ||
connectionIsStale = this.connectionIsStale(); | ||
return { | ||
startedAt: this.startedAt, | ||
stoppedAt: this.stoppedAt, | ||
pingedAt: this.pingedAt, | ||
reconnectAttempts: this.reconnectAttempts, | ||
connectionIsStale: connectionIsStale, | ||
interval: interval | ||
}; | ||
}; | ||
now = function() { | ||
@@ -318,3 +351,2 @@ return new Date().getTime(); | ||
this.subscriptions = []; | ||
this.history = []; | ||
} | ||
@@ -409,3 +441,3 @@ | ||
Subscriptions.prototype.notify = function() { | ||
var args, callbackName, i, identifier, len, results, subscription, subscriptions; | ||
var args, callbackName, i, len, results, subscription, subscriptions; | ||
subscription = arguments[0], callbackName = arguments[1], args = 3 <= arguments.length ? slice.call(arguments, 2) : []; | ||
@@ -420,17 +452,3 @@ if (typeof subscription === "string") { | ||
subscription = subscriptions[i]; | ||
if (typeof subscription[callbackName] === "function") { | ||
subscription[callbackName].apply(subscription, args); | ||
} | ||
if (callbackName === "initialized" || callbackName === "connected" || callbackName === "disconnected" || callbackName === "rejected") { | ||
identifier = subscription.identifier; | ||
results.push(this.record({ | ||
notification: { | ||
identifier: identifier, | ||
callbackName: callbackName, | ||
args: args | ||
} | ||
})); | ||
} else { | ||
results.push(void 0); | ||
} | ||
results.push(typeof subscription[callbackName] === "function" ? subscription[callbackName].apply(subscription, args) : void 0); | ||
} | ||
@@ -453,25 +471,2 @@ return results; | ||
Subscriptions.prototype.record = function(data) { | ||
data.time = new Date(); | ||
this.history = this.history.slice(-19); | ||
return this.history.push(data); | ||
}; | ||
Subscriptions.prototype.toJSON = function() { | ||
var subscription; | ||
return { | ||
history: this.history, | ||
identifiers: (function() { | ||
var i, len, ref, results; | ||
ref = this.subscriptions; | ||
results = []; | ||
for (i = 0, len = ref.length; i < len; i++) { | ||
subscription = ref[i]; | ||
results.push(subscription.identifier); | ||
} | ||
return results; | ||
}).call(this) | ||
}; | ||
}; | ||
return Subscriptions; | ||
@@ -546,15 +541,2 @@ | ||
Consumer.prototype.inspect = function() { | ||
return JSON.stringify(this, null, 2); | ||
}; | ||
Consumer.prototype.toJSON = function() { | ||
return { | ||
url: this.url, | ||
subscriptions: this.subscriptions, | ||
connection: this.connection, | ||
connectionMonitor: this.connectionMonitor | ||
}; | ||
}; | ||
return Consumer; | ||
@@ -566,3 +548,2 @@ | ||
// umd export | ||
@@ -582,1 +563,3 @@ // https://github.com/umdjs/umd/blob/master/templates/returnExports.js | ||
} | ||
}() |
{ | ||
"name": "actioncable", | ||
"version": "5.0.0-beta1-3", | ||
"version": "5.0.0-beta3", | ||
"description": "pre-built for Actioncable client", | ||
@@ -5,0 +5,0 @@ "main": "dist/actioncable.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
18010
475