hop-2012/server/web/hop.js

106 lines
2.7 KiB
JavaScript
Raw Normal View History

2012-05-01 21:32:01 +00:00
var Hop = {
2012-05-01 12:48:58 +00:00
$tap: null,
$args: null,
$open_hooks: [],
2012-05-01 19:39:06 +00:00
$message_hooks: [],
$close_hooks: [],
run_open_hooks: function (event, stream) {
Hop._send(Hop._subscribe_msg(Hop.$tap.id, "", "", "", ""));
2012-05-01 21:32:01 +00:00
$.each(Hop.$open_hooks, function (i, f) { f(event, stream); });
},
2012-05-01 19:39:06 +00:00
run_message_hooks: function (event, stream) {
2012-05-01 21:32:01 +00:00
$.each(Hop.$message_hooks, function (i, f) { f(event, stream); });
2012-05-01 19:39:06 +00:00
},
run_close_hooks: function (event, stream) {
2012-05-01 21:32:01 +00:00
$.each(Hop.$close_hooks, function (i, f) { f(event, stream); });
},
2012-05-01 12:48:58 +00:00
_send: function (msg) {
2012-05-01 21:32:01 +00:00
Hop.$tap.send({data: JSON.stringify(msg)});
2012-05-01 12:48:58 +00:00
},
_post_msg: function (target, datum, token) {
return ["post", target, datum, token || ""];
},
post: function (target, datum, token) {
2012-05-01 21:32:01 +00:00
Hop._send(Hop._post_msg(target, datum, token));
2012-05-01 12:48:58 +00:00
},
_subscribe_msg: function (filter, sink, name, reply_sink, reply_name) {
return ["subscribe", filter, sink, name, reply_sink || "", reply_name || ""];
},
_subscribe: function (source, filter, name, reply_name) {
2012-05-01 21:32:01 +00:00
return Hop._post_msg(source,
Hop._subscribe_msg(filter, Hop.$tap.id, name,
reply_name ? Hop.$tap.id : "",
reply_name));
2012-05-01 12:48:58 +00:00
},
subscribe: function (source, filter, name, reply_name) {
2012-05-01 21:32:01 +00:00
Hop._send(Hop._subscribe(source, filter, name, reply_name));
2012-05-01 12:48:58 +00:00
},
_unsubscribe_msg: function (token) {
return ["unsubscribe", token];
},
_unsubscribe: function (source, token) {
2012-05-01 21:32:01 +00:00
return Hop._post_msg(source, Hop._unsubscribe_msg(token));
2012-05-01 12:48:58 +00:00
},
unsubscribe: function (source, token) {
2012-05-01 21:32:01 +00:00
Hop._send(Hop._unsubscribe(source, token));
2012-05-01 12:48:58 +00:00
},
_create_msg: function (classname, arg, reply_sink, reply_name) {
return ["create", classname, arg, reply_sink || "", reply_name || ""];
},
_create: function (classname, arg, reply_name, factory) {
2012-05-01 21:32:01 +00:00
return Hop._post_msg(factory || "factory",
Hop._create_msg(classname, arg,
reply_name ? Hop.$tap.id : "",
reply_name));
2012-05-01 12:48:58 +00:00
},
create: function (classname, arg, reply_name, factory) {
2012-05-01 21:32:01 +00:00
Hop._send(Hop._create(classname, arg, reply_name, factory));
2012-05-01 12:48:58 +00:00
},
2012-05-01 19:39:06 +00:00
_install_tap: function () {
2012-05-01 21:32:01 +00:00
Hop.$tap = $.stream("/_/tap", {
2012-05-01 12:48:58 +00:00
type: "http",
dataType: "json",
enableXDR: true,
2012-05-01 21:32:01 +00:00
open: Hop.run_open_hooks,
message: Hop.run_message_hooks,
error: Hop.run_close_hooks,
close: Hop.run_close_hooks
2012-05-01 12:48:58 +00:00
});
},
2012-05-01 19:39:06 +00:00
install_tap: function (args) {
2012-05-01 21:32:01 +00:00
Hop.$args = args;
Hop._install_tap();
setInterval(Hop.check_connectivity, 5000);
2012-05-01 19:39:06 +00:00
},
check_connectivity: function () {
2012-05-01 21:32:01 +00:00
switch (Hop.$tap.readyState) {
2012-05-01 19:39:06 +00:00
case 0: // connecting
case 1: // open
case 2: // closing
break;
case 3: // closed
2012-05-01 21:32:01 +00:00
Hop._install_tap();
2012-05-01 19:39:06 +00:00
}
2012-05-01 12:48:58 +00:00
}
}