Skip to content

Commit

Permalink
Tower code reuse across inject and capture scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
reid committed Dec 7, 2012
1 parent 82a95af commit d436305
Show file tree
Hide file tree
Showing 4 changed files with 758 additions and 833 deletions.
5 changes: 4 additions & 1 deletion lib/hub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,12 @@ Hub.prototype._createRouter = function () {
"(function () {\n" +
"var SockJS;\n";
streamConfig.postpend = "}());";

// Note: reverse loading order. files[0] == file
files.unshift(path.join(dir, "events.js"));
files.unshift(path.join(__dirname, "../../dep", "sock.js"));
files.unshift(path.join(dir, "tempest.js"));
files.unshift(path.join(__dirname, "../../dep", "yui-runtime.js"));
files.unshift(path.join(__dirname, "../../dep", "sock.js"));
}

if ("inject.js" === file) {
Expand Down
103 changes: 12 additions & 91 deletions lib/hub/view/public/capture.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,21 @@
/*jshint browser:true */
/*global SimpleEvents, SockJS, io */
/*global YUI */
(function () {
"use strict";

function getSockURL(resource, name) {
return [
document.location.protocol,
"//",
document.domain,
":" + document.location.port,
resource,
"/" + name
].join("");
}

var reconnectInterval = 1,
beatInterval;

window.Yeti = {
capture: function capture(resource) {
reconnectInterval = reconnectInterval * 2;
function capture(resource) {
YUI().use("tempest", function bootInjectedDriver(Y) {

if (resource === "/") {
resource = "";
}

var agentId = window.location.href.match(/\/agent\/(\d+)/)[1],
win = window, moving = false, unload,
sock = new SockJS(getSockURL(resource, "tower")),
tower = new SimpleEvents(sock);

if ("function" === typeof win.onbeforeunload) {
unload = win.onbeforeunload;
}

win.onbeforeunload = function () {
if (!moving) {
var img = new Image();
img.src = "/ping/unload/" + agentId;
}
if (unload) {
unload();
}
};
var driver = new Y.InjectedDriver({
captureOnly: true,
resource: resource
});

function beat() {
tower.emit("beat");
}
driver.connectWithHandshake();
});
}

tower.queueUntil("listening");
tower.on("listening", function () {
if (reconnectInterval > 2) {
// We detected the server is back online,
// to properly register we must reload.
window.location.reload(true);
}
tower.emit("register", {
agentId: agentId,
ua: window.navigator.userAgent
});
tower.queueUntil("ready");
document.getElementById("test").innerHTML = "Sending registration...";
});
tower.on("ready", function (id) {
beatInterval = window.setInterval(beat, 10000);
// Only matters when we fully reconnect
// without a reload, but for completeness:
reconnectInterval = 1;
// attachServer() tests use cookies instead of the URL
document.cookie = "yeti-agent=" + id +
";path=/;expires=Sat, 10 Mar 2029 08:00:00 GMT";
document.getElementById("test").innerHTML = "Waiting for tests...";
});
tower.on("navigate", function (test) {
moving = true;
document.location.href = test;
});
tower.on("regenerateId", function () {
// Get a new ID from the server.
// Will reset the yeti-agent on the next ready event.
document.location.href = resource + "/";
});
tower.on("close", function () {
window.clearInterval(beatInterval);
var seconds = reconnectInterval;
(function tick() {
document.getElementById("test").innerHTML = "Disconnected, " +
"reconnecting in " + seconds + "...";
seconds -= 1;
if (seconds !== -1) {
setTimeout(tick, 1000);
} else {
capture(resource);
}
}());
});
}
window.Yeti = {
capture: capture
};
}());
Loading

0 comments on commit d436305

Please sign in to comment.