/*global JSONP, 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; jsonp = JSONP(); init_url = station_finder + '/init/' + station_uid + '/' + token; listeners = []; turnOn = function () { var initial_value; jsonp.get(init_url, {}, function (init) { if (init.error !== undefined) { error(init); } 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); } }); }; error = function (args) { throw "Init error: \"" + args.error + "\""; }; tune = function (channel_path, callback) { listeners.push({ channel_path: channel_path, callback: function (event) { callback(event.event, event.args); } }); }; build_channel_path = function (channel_path) { return '/' + station_uid + '/' + token + '/' + channel_path; }; return { tune: tune, turnOn: turnOn }; }; }()); transistor.Radio = Radio; }(window.Transistor));