Permalink
Browse files

GET /volume returns the current volume

  • Loading branch information...
1 parent 7911008 commit ad056e400477e8e190333e37d90b63dc803d087a @oz committed Oct 1, 2012
Showing with 57 additions and 27 deletions.
  1. +23 −19 data/worker.js
  2. +3 −2 lib/http_server.js
  3. +26 −3 lib/main.js
  4. +5 −3 lib/rdio_web_app.js
View
@@ -4,55 +4,59 @@
* FIXME(s)
* --------
*
- * Note that this uses unsafeWindow to check for some globals hopefully defined
- * by Rdio's Javascript. The worker being loaded only on rdio.com domains helps.
- *
- * The "transparent" window proxy does not allow access to Rdio's JS, making
- * it a little useless in this case... Suggestions?
+ * Note that this uses unsafeWindow to check for some globals normally
+ * defined by Rdio's Javascript. The safer "transparent" window proxy
+ * does not allow access to Rdio's JS, making it a little useless in
+ * this case... Suggestions?
*
* Also, encapsulate stuff.
*
*/
(function (window, unsafe) {
- var $ = unsafe.jQuery;
- var player = null; // Rdio's player object.
-
- // not even jQuery...
- if ( !$ ) { return self.port.emit("noplayer"); }
+ var $ = unsafe.jQuery,
+ port = self.port,
+ player = null // Rdio's player object.
+ ;
// Wait for DOM ready if jQuery's present on the page.
+ if ( !$ ) { return self.port.emit("noplayer"); }
$(function() {
if ( unsafe.R && unsafe.R.player ) {
player = unsafe.R.player;
- self.port.emit('ready');
+ port.emit('ready');
} else {
- self.port.emit('noplayer');
+ port.emit('noplayer');
player = null;
}
});
- self.port.on('playPause', function () {
+ port.on('playPause', function () {
if ( null !== player ) {
player.playPause();
} else {
console.log('Cannot playPause, player is not available.');
}
});
- self.port.on('volume:get', function () {
+ port.on('volume:get', function () {
+ var volume;
+
if ( null !== player ) {
- self.port.emit('volume:got', player.volume());
+ // Reduce float precision (hey, it's Javascript... :p)
+ volume = parseInt(player.volume() * 100, 10) / 100;
+ port.emit('volume:got', volume);
} else {
- console.log("Cannot update volume, player is not available.");
+ console.log("Cannot get volume, player is not available.");
}
});
- self.port.on('volume:update', function (value) {
+ port.on('volume:update', function (value) {
if ( null !== player ) {
+ // XXX User Rdio's pubsub object to get the new value?
player.volume(value);
- self.port.emit('volume:set', player.volume());
+ port.emit('volume:set', value);
} else {
console.log("Cannot update volume, player is not available.");
}
});
-})(window, unsafeWindow);
+})(window, unsafeWindow);
View
@@ -11,7 +11,8 @@ var srv = new nsHttpServer();
var server = {
start: function (port) { srv.start(port); return server; },
- stop: function (cb) { srv.stop(cv); return server; }
+ stop: function (cb) { srv.stop(cv); return server; },
+ error: function (code, cb) { srv.registerErrorHandler(code, cb); }
};
// Add some HTTP verbs as direct methods on server object.
@@ -35,4 +36,4 @@ httpVerbs.forEach(function (method) {
};
});
-exports.server = server;
+exports.server = server;
View
@@ -46,16 +46,39 @@ server.post('/play_pause', function (req, res) {
return ;
});
+server.post('/next', function (req, req) {
+ if ( !rdio.isReady() ) {
+ return res.write( error('Player not ready') );
+ }
+
+ if ( rdio.next() ) {
+ res.write( success('next') );
+ } else {
+ res.write( error('Player not ready') );
+ }
+
+});
+
server.get('/volume', function (req, res) {
if ( !rdio.isReady() ) {
return res.write( error('Player not ready') );
}
+
+ // Process asynchronously from there on.
+ res.processAsync();
rdio.volume(function (volume) {
- return res.write( success(volume) );
+ res.write( success(volume) );
+ res.finish();
});
+});
- return ;
+server.error(500, function (req, res) {
+
+ console.log("500 error handler");
+
+ res.setStatusLine(req.httpVersion, 500, "KO");
+ return res.write( error("Internal error") );
});
// Start HTTP server
-server.start(serverPort);
+server.start(serverPort);
View
@@ -33,14 +33,16 @@ var RdioWebApp = EventEmitter.compose({
},
volume: function (value, cb) {
+ // Get volume
if ( 'function' == typeof value ) {
+ this.worker.port.once('volume:got', value);
this.worker.port.emit('volume:get');
- this.worker.port.once('volume:got', cb);
+ // Set volume
} else {
- this.worker.port.emit('volume:update', value);
if ( cb ) {
this.worker.port.once('volume:set', cb);
}
+ this.worker.port.emit('volume:update', value);
}
return this;
},
@@ -99,4 +101,4 @@ var RdioWebApp = EventEmitter.compose({
}
});
-exports.rdioWebApp = new RdioWebApp(rdioTab);
+exports.rdioWebApp = new RdioWebApp(rdioTab);

0 comments on commit ad056e4

Please sign in to comment.