2016-08-10 23:50:23 +02:00
|
|
|
/**
|
|
|
|
* This implements the webplayer reducers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// NPM imports
|
2016-08-07 00:58:36 +02:00
|
|
|
import Immutable from "immutable";
|
|
|
|
|
2016-08-10 23:50:23 +02:00
|
|
|
// Local imports
|
|
|
|
import { createReducer } from "../utils";
|
|
|
|
|
|
|
|
// Models
|
|
|
|
import { stateRecord } from "../models/webplayer";
|
|
|
|
|
|
|
|
// Actions
|
2016-08-07 00:58:36 +02:00
|
|
|
import {
|
|
|
|
PLAY_PAUSE,
|
2016-08-10 23:50:23 +02:00
|
|
|
STOP_PLAYBACK,
|
|
|
|
SET_PLAYLIST,
|
|
|
|
PUSH_SONG,
|
|
|
|
POP_SONG,
|
|
|
|
JUMP_TO_SONG,
|
|
|
|
PLAY_PREVIOUS,
|
|
|
|
PLAY_NEXT,
|
2016-08-07 00:58:36 +02:00
|
|
|
TOGGLE_RANDOM,
|
|
|
|
TOGGLE_REPEAT,
|
2016-08-08 12:22:35 +02:00
|
|
|
TOGGLE_MUTE,
|
2016-08-10 23:50:23 +02:00
|
|
|
SET_VOLUME,
|
2016-08-08 12:22:35 +02:00
|
|
|
INVALIDATE_STORE } from "../actions";
|
2016-08-10 23:50:23 +02:00
|
|
|
|
2016-08-07 00:58:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initial state
|
|
|
|
*/
|
|
|
|
|
|
|
|
var initialState = new stateRecord();
|
|
|
|
|
|
|
|
|
2016-08-10 23:50:23 +02:00
|
|
|
/**
|
|
|
|
* Helper functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop playback in reducer helper.
|
|
|
|
*
|
|
|
|
* @param state Current state to update.
|
|
|
|
*/
|
|
|
|
function stopPlayback(state) {
|
|
|
|
return (
|
|
|
|
state
|
|
|
|
.set("isPlaying", false)
|
|
|
|
.set("currentIndex", 0)
|
|
|
|
.set("playlist", new Immutable.List())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-07 00:58:36 +02:00
|
|
|
/**
|
|
|
|
* Reducers
|
|
|
|
*/
|
|
|
|
|
|
|
|
export default createReducer(initialState, {
|
|
|
|
[PLAY_PAUSE]: (state, payload) => {
|
2016-08-10 23:50:23 +02:00
|
|
|
// Force play or pause
|
2016-08-07 00:58:36 +02:00
|
|
|
return state.set("isPlaying", payload.isPlaying);
|
|
|
|
},
|
2016-08-10 23:50:23 +02:00
|
|
|
[STOP_PLAYBACK]: (state) => {
|
|
|
|
// Clear the playlist
|
|
|
|
return stopPlayback(state);
|
2016-08-07 00:58:36 +02:00
|
|
|
},
|
2016-08-10 23:50:23 +02:00
|
|
|
[SET_PLAYLIST]: (state, payload) => {
|
|
|
|
// Set current playlist, reset playlist index
|
2016-08-07 00:58:36 +02:00
|
|
|
return (
|
|
|
|
state
|
|
|
|
.set("playlist", new Immutable.List(payload.playlist))
|
|
|
|
.set("currentIndex", 0)
|
|
|
|
);
|
|
|
|
},
|
2016-08-10 23:50:23 +02:00
|
|
|
[PUSH_SONG]: (state, payload) => {
|
|
|
|
// Push song to playlist
|
|
|
|
const newPlaylist = state.get("playlist").insert(payload.index, payload.song);
|
|
|
|
return state.set("playlist", newPlaylist);
|
|
|
|
},
|
|
|
|
[POP_SONG]: (state, payload) => {
|
|
|
|
// Pop song from playlist
|
|
|
|
return state.deleteIn(["playlist", payload.index]);
|
|
|
|
},
|
|
|
|
[JUMP_TO_SONG]: (state, payload) => {
|
|
|
|
// Set current index
|
|
|
|
const newCurrentIndex = state.get("playlist").findKey(x => x == payload.song);
|
|
|
|
return state.set("currentIndex", newCurrentIndex);
|
|
|
|
},
|
|
|
|
[PLAY_PREVIOUS]: (state) => {
|
|
|
|
const newIndex = state.get("currentIndex") - 1;
|
|
|
|
if (newIndex < 0) {
|
|
|
|
// If there is an overlow on the left of the playlist, just stop
|
|
|
|
// playback
|
|
|
|
return stopPlayback(state);
|
|
|
|
} else {
|
|
|
|
return state.set("currentIndex", newIndex);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[PLAY_NEXT]: (state) => {
|
|
|
|
const newIndex = state.get("currentIndex") + 1;
|
|
|
|
if (newIndex > state.get("playlist").size) {
|
|
|
|
// If there is an overflow, just stop playback
|
|
|
|
return stopPlayback(state);
|
|
|
|
} else {
|
|
|
|
// Else, play next item
|
|
|
|
return state.set("currentIndex", newIndex);
|
|
|
|
}
|
|
|
|
},
|
2016-08-07 00:58:36 +02:00
|
|
|
[TOGGLE_RANDOM]: (state) => {
|
|
|
|
return state.set("isRandom", !state.get("isRandom"));
|
|
|
|
},
|
|
|
|
[TOGGLE_REPEAT]: (state) => {
|
|
|
|
return state.set("isRepeat", !state.get("isRepeat"));
|
|
|
|
},
|
|
|
|
[TOGGLE_MUTE]: (state) => {
|
|
|
|
return state.set("isMute", !state.get("isMute"));
|
|
|
|
},
|
2016-08-10 23:50:23 +02:00
|
|
|
[SET_VOLUME]: (state, payload) => {
|
|
|
|
return state.set("volume", payload.volume);
|
|
|
|
},
|
2016-08-08 12:22:35 +02:00
|
|
|
[INVALIDATE_STORE]: () => {
|
|
|
|
return new stateRecord();
|
2016-08-10 23:50:23 +02:00
|
|
|
},
|
2016-08-07 00:58:36 +02:00
|
|
|
});
|