Skip to content

Commit

Permalink
spotify queue
Browse files Browse the repository at this point in the history
  • Loading branch information
lufinkey committed Feb 14, 2018
1 parent eac543d commit 38167f2
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 6 deletions.
8 changes: 3 additions & 5 deletions index.js
@@ -1,9 +1,7 @@

import { NativeModules } from 'react-native';
import NativeModuleEvents from 'react-native-events';
import Queue from './src/Queue';
import Spotify from './src/Spotify';

const Spotify = NativeModules.Spotify;

NativeModuleEvents.registerNativeModule(Spotify);
Spotify.queue = Queue;

export default Spotify;
2 changes: 1 addition & 1 deletion ios/RCTSpotify.m
Expand Up @@ -81,7 +81,7 @@ -(id)init

+(BOOL)requiresMainQueueSetup
{
return YES;
return NO;
}

RCT_EXPORT_METHOD(__registerAsJSEventEmitter:(int)moduleId)
Expand Down
150 changes: 150 additions & 0 deletions src/Queue.js
@@ -0,0 +1,150 @@

import EventEmitter from 'events';
import Spotify from './Spotify';
import SpotifyURI from 'spotify-uri';


const Queue = new EventEmitter();

let currentURI = null;
let uris = [];
let connected = null;



// handle connection

Spotify.on('login', () => {
if(connected === null)
{
connected = true;
}
});

Spotify.on('logout', () => {
connected = null;
Queue.clear();
});

Spotify.on('disconnect', () => {
connected = false;
});

Spotify.on('reconnect', () => {
connected = true;
});



// handle track queueing

let tryQueuePlayback = null;
tryQueuePlayback = () => {
if(currentURI == null)
{
return;
}
Spotify.playURI(currentURI, 0, 0, (error) => {
if(error)
{
// ensure we're logged in and we have uris
if(connected !== null && uris.length > 0)
{
if(!connected)
{
// we must have failed because we weren't connected, so wait until we are
Spotify.once('reconnect', tryQueuePlayback);
}
else
{
// unknown error
Queue.emit('queueError', error);
}
}
return;
}
});
};

Spotify.on('trackDelivered', (event) => {
if(uris.length > 0)
{
// play the next song in the queue
Queue.nextTrack();
}
else if(currentURI != null)
{
// the queue has finished
currentURI = null;
Queue.emit('queueFinish');
}
});



// functions

Queue.nextTrack = function()
{
var nextURI = uris[0];
uris.splice(0, 1);
if(uris.length == 0)
{
currentURI = null;
return;
}
currentURI = nextURI;
tryQueuePlayback();
}

Queue.skipToTrack = function(index)
{
uris.splice(0, index);
Queue.nextTrack();
}

Queue.addTrack = function(uri)
{
const uriParts = SpotifyURI.parse(uri);
if(type !== 'track')
{
throw new Error("invalid URI: must be a track");
}
var wasEmpty = (uris.length == 0);
uris.push(uri);
}

Queue.removeTrack = function(index)
{
if(!Number.isInteger(index))
{
throw new Error("invalid index");
}
uris.splice(index, 1);
}

Queue.moveTrack = function(sourceIndex, destIndex)
{
if(!Number.isInteger(sourceIndex) || !Number.isInteger(destIndex)
|| sourceIndex < 0 || sourceIndex >= uris.length
|| destIndex < 0 || destIndex >= uris.length)
{
throw new Error("invalid index");
}
var uri = uris[sourceIndex];
uris.splice(sourceIndex, 1);
uris.splice(destIndex, 0, uri);
}

Queue.getURIs = function()
{
return uris.slice(0);
}

Queue.clear = function()
{
currentURI = null;
uris = [];
}

export default Queue;
8 changes: 8 additions & 0 deletions src/Spotify.js
@@ -0,0 +1,8 @@

import { NativeModules } from 'react-native';
import NativeModuleEvents from 'react-native-events';

const Spotify = NativeModules.Spotify;
NativeModuleEvents.registerNativeModule(Spotify);

export default Spotify;

0 comments on commit 38167f2

Please sign in to comment.