Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support aborting a track.play stream #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions lib/track.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ Track.prototype.play = function () {
var spotify = this._spotify;
var stream = new PassThrough();

// Abort support
var req = null;
var isAborted = false;
stream.abort = function () {
if (isAborted === false) {
isAborted = true;
if (req !== null) {
req.abort();
}
}
};

// if a song was playing before this, the "track_end" command needs to be sent
var track = spotify.currentTrack;
if (track && track._playSession) {
Expand All @@ -95,13 +107,15 @@ Track.prototype.play = function () {
spotify.trackUri(track, function (err, res) {
if (err) return stream.emit('error', err);
if (!res.uri) return stream.emit('error', new Error('response contained no "uri"'));
debug('GET %s', res.uri);
track._playSession = res;
var req = spotify.agent.get(res.uri)
.set({ 'User-Agent': spotify.userAgent })
.end()
.request();
req.on('response', response);
if (isAborted === false) {
debug('GET %s', res.uri);
track._playSession = res;
req = spotify.agent.get(res.uri)
.set({ 'User-Agent': spotify.userAgent })
.end()
.request();
req.on('response', response);
}
});

function response (res) {
Expand Down