lib/assets/javascripts/transistor.js in transistor-0.1.5 vs lib/assets/javascripts/transistor.js in transistor-0.1.6
- old
+ new
@@ -82,73 +82,75 @@
get:jsonp,
init:setDefaults
};
};
-/*global JSONP, Faye */
+/*global Faye */
if (window.Transistor === undefined) {
window.Transistor = {};
}
(function (transistor) {
var Radio = (function () {
- return function (station_finder, station_uid, token, cache) {
- var jsonp, init_url, bayeux, listeners, turnOn, error, tune, build_channel_path;
+ return function (aerial_url, station_uid, token, cache) {
+ var radio = this,
+ faye,
+ H,
+ Extension;
- jsonp = JSONP();
+ H = (function () {
+ var callbacks = {},
+ buildChannel;
- init_url = station_finder + '/init/' + station_uid + '/' + token;
- listeners = [];
+ buildChannel = function (channel_path) {
+ return ['', station_uid, token, channel_path].join('/');
+ };
- turnOn = function () {
- var initial_value;
+ return {
+ registerCallback: function (channel_path, callback) {
+ var channel = buildChannel(channel_path),
+ listener = function (message) {
+ callback(message.event, message.args);
+ };
- jsonp.get(init_url, {}, function (init) {
- if (init.error !== undefined) {
- error(init);
+ callbacks[channel_path] = listener;
+
+ faye.subscribe(channel, listener);
+ },
+ triggerCallback: function (channel_path, message) {
+ callbacks[channel_path](message);
}
+ };
+ }());
- bayeux = new Faye.Client(init.aerial_url, { retry: 3 });
-
- for (var i = 0; i < listeners.length; i += 1) {
- initial_value = init.channels[listeners[i].channel_path];
-
- if (initial_value !== undefined) {
- listeners[i].callback({
- event: 'init',
- args: initial_value
- });
- }
-
- bayeux.subscribe(build_channel_path(listeners[i].channel_path), listeners[i].callback);
+ Extension = (function () {
+ var process = function (message, callback) {
+ if (message.ext && message.ext.init) {
+ var channel_path = message.subscription.split('/').splice(3).join('/');
+ H.triggerCallback(channel_path, {
+ event: 'init',
+ args: message.ext.init.collection
+ });
}
- });
- };
+ callback(message);
+ };
- error = function (args) {
- throw "Init error: \"" + args.error + "\"";
- };
+ return function () {
+ this.incoming = process;
+ };
+ }());
- tune = function (channel_path, callback) {
- listeners.push({
- channel_path: channel_path,
- callback: function (event) {
- callback(event.event, event.args);
- }
- });
- };
+ faye = new Faye.Client(aerial_url);
+ faye.addExtension(new Extension());
- build_channel_path = function (channel_path) {
- return '/' + station_uid + '/' + token + '/' + channel_path;
+ radio.tune = function (channel_path, callback) {
+ H.registerCallback(channel_path, callback);
};
- return {
- tune: tune,
- turnOn: turnOn
- };
+ return radio;
};
}());
transistor.Radio = Radio;
@@ -331,15 +333,13 @@
} else {
jsonp.get(command_url, {data: JSON.stringify(data)}, cb);
}
};
- return {
- set: set,
- insert: insert,
- update: update,
- remove: remove
- };
+ this.set = set;
+ this.insert = insert;
+ this.update = update;
+ this.remove = remove;
};
}());
transistor.Control = Control;