Skip to content

Commit

Permalink
move track end handle to a separate function of the Player prototype …
Browse files Browse the repository at this point in the history
…(DRY)
  • Loading branch information
eiriklv committed Jun 24, 2014
1 parent 2d30b07 commit 2649409
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions modules/spotify/player/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,10 @@ Player.prototype.play = function (song) {
// attach the mp3 stream to the context (to be able to skip)
this.current = track.play();

// handle error on mp3 stream
this.current.on('error', function (err) {
debug(err);

// if the song is not available, just skip it and move on
spotify.disconnect();
this.current = null;

// update queueitem to playing: false and queue: false
this.queue.update(song, function (err, product) {
this.next();
}.bind(this));
this.handlePlayEnd.call(this, song, spotify);
}.bind(this));

// decode and play the mp3 stream
Expand All @@ -56,31 +49,29 @@ Player.prototype.play = function (song) {
.pipe(new Speaker())
.on('finish', function () {
debug('finished playing song');
spotify.disconnect();
this.current = null;

// update queueitem to playing: false and queue: false
this.queue.update(song, function (err, product) {
this.next();
}.bind(this));
this.handlePlayEnd.call(this, song, spotify);
}.bind(this))
.on('error', function (err) {
debug(err);

// if the song is not available, just skip it and move on
spotify.disconnect();
this.current = null;

// update queueitem to playing: false and queue: false
this.queue.update(song, function (err, product) {
this.next();
}.bind(this));
this.handlePlayEnd.call(this, song, spotify);
}.bind(this));

}.bind(this));
}.bind(this));
};

// handle end of play (either success or error)
Player.prototype.handlePlayEnd = function (song, spotify) {
// if the song is not available, just skip it and move on
spotify.disconnect();
this.current = null;

// update queueitem to playing: false and queue: false
this.queue.update(song, function (err, product) {
this.next();
}.bind(this));
};

// skip the current track (if one is playing)
Player.prototype.skip = function () {
if (this.current) return this.current.end();
Expand All @@ -90,7 +81,6 @@ Player.prototype.skip = function () {
// publish meta data
Player.prototype.publish = function (track, song) {
// update db with playing: true

debug('Playing: %s - %s', track.artist[0].name, track.name);
debug(util.inspect(track, { colors: true }));
};
Expand Down

0 comments on commit 2649409

Please sign in to comment.