Use getters/setters

This commit is contained in:
An Phan 2016-03-18 12:45:12 +08:00
parent 3500ee7b33
commit e0d783f366
15 changed files with 119 additions and 104 deletions

View file

@ -118,7 +118,7 @@
computed: {
greeting() {
return _.sample(this.greetings).replace('%s', userStore.current().name);
return _.sample(this.greetings).replace('%s', userStore.current.name);
},
},

View file

@ -86,7 +86,7 @@
* @return {Array} The songs to add into a (new) playlist
*/
songsToAddTo() {
return this.selectedSongs.length ? this.selectedSongs : queueStore.all();
return this.selectedSongs.length ? this.selectedSongs : queueStore.all;
},
/**
@ -96,7 +96,7 @@
* - We have songs to shuffle.
*/
showShufflingAllOption() {
return songStore.all().length;
return songStore.all.length;
},
},
@ -112,7 +112,7 @@
* Shuffle all songs we have.
*/
shuffleAll() {
playback.queueAndPlay(songStore.all(), true);
playback.queueAndPlay(songStore.all, true);
},
/**

View file

@ -49,7 +49,7 @@
},
isAdmin() {
return userStore.current().is_admin;
return userStore.current.is_admin;
},
},

View file

@ -124,7 +124,7 @@
* @return {?Object}
*/
prev() {
return playback.prevSong();
return playback.previous;
},
/**
@ -133,7 +133,7 @@
* @return {?Object}
*/
next() {
return playback.nextSong();
return playback.next;
},
},

View file

@ -47,7 +47,7 @@ export default {
* Listen to 'ended' event on the audio player and play the next song in the queue.
*/
document.querySelector('.plyr').addEventListener('ended', e => {
songStore.scrobble(queueStore.current());
songStore.scrobble(queueStore.current);
if (preferenceStore.get('repeatMode') === 'REPEAT_ONE') {
this.restart();
@ -67,7 +67,7 @@ export default {
}
// The current song has only 10 seconds left to play.
let nextSong = queueStore.getNextSong();
let nextSong = queueStore.next;
if (!nextSong || nextSong.preloaded) {
return;
}
@ -111,14 +111,14 @@ export default {
return;
}
if (queueStore.current()) {
queueStore.current().playbackState = 'stopped';
if (queueStore.current) {
queueStore.current.playbackState = 'stopped';
}
song.playbackState = 'playing';
// Set the song as the current song
queueStore.current(song);
queueStore.current = song;
// Add it into the "recent" list
songStore.addRecent(song);
@ -138,7 +138,7 @@ export default {
* Restart playing a song.
*/
restart() {
let song = queueStore.current();
let song = queueStore.current;
// Record the UNIX timestamp the song start playing, for scrobbling purpose
song.playStartTime = Math.floor(Date.now() / 1000);
@ -173,38 +173,38 @@ export default {
},
/**
* Get the next song in the queue.
* The next song in the queue.
* If we're in REPEAT_ALL mode and there's no next song, just get the first song.
*
* @return {Object} The song
*/
nextSong() {
let next = queueStore.getNextSong();
get next() {
let next = queueStore.next;
if (next) {
return next;
}
if (preferenceStore.get('repeatMode') === 'REPEAT_ALL') {
return queueStore.first();
return queueStore.first;
}
},
/**
* Get the prev song in the queue.
* The previous song in the queue.
* If we're in REPEAT_ALL mode and there's no prev song, get the last song.
*
* @return {Object} The song
*/
prevSong() {
let prev = queueStore.getPrevSong();
get previous() {
let prev = queueStore.previous;
if (prev) {
return prev;
}
if (preferenceStore.get('repeatMode') === 'REPEAT_ALL') {
return queueStore.last();
return queueStore.last;
}
},
@ -229,13 +229,13 @@ export default {
playPrev() {
// If the song's duration is greater than 5 seconds and we've passed 5 seconds into it,
// restart playing instead.
if (this.player.media.currentTime > 5 && queueStore.current().length > 5) {
if (this.player.media.currentTime > 5 && queueStore.current.length > 5) {
this.player.restart();
return;
}
let prev = this.prevSong();
let prev = this.previous;
if (!prev && preferenceStore.get('repeatMode') === 'NO_REPEAT') {
this.stop();
@ -251,7 +251,7 @@ export default {
* If the next song is not found and the current mode is NO_REPEAT, we stop completely.
*/
playNext() {
let next = this.nextSong();
let next = this.next;
if (!next && preferenceStore.get('repeatMode') === 'NO_REPEAT') {
// Nothing lasts forever, even cold November rain.
@ -306,8 +306,8 @@ export default {
this.player.pause();
this.player.seek(0);
if (queueStore.current()) {
queueStore.current().playbackState = 'stopped';
if (queueStore.current) {
queueStore.current.playbackState = 'stopped';
}
},
@ -316,7 +316,7 @@ export default {
*/
pause() {
this.player.pause();
queueStore.current().playbackState = 'paused';
queueStore.current.playbackState = 'paused';
},
/**
@ -324,8 +324,8 @@ export default {
*/
resume() {
this.player.play();
queueStore.current().playbackState = 'playing';
this.app.$broadcast('song:played', queueStore.current());
queueStore.current.playbackState = 'playing';
this.app.$broadcast('song:played', queueStore.current);
},
/**
@ -336,7 +336,7 @@ export default {
*/
queueAndPlay(songs = null, shuffle = false) {
if (!songs) {
songs = songStore.all();
songs = songStore.all;
}
if (!songs.length) {
@ -353,7 +353,7 @@ export default {
// Wrap this inside a nextTick() to wait for the DOM to complete updating
// and then play the first song in the queue.
this.app.$nextTick(() => this.play(queueStore.first()));
this.app.$nextTick(() => this.play(queueStore.first));
},
/**
@ -361,13 +361,13 @@ export default {
* If the current queue is empty, try creating it by shuffling all songs.
*/
playFirstInQueue() {
if (!queueStore.all().length) {
if (!queueStore.all.length) {
this.queueAndPlay();
return;
}
this.play(queueStore.first());
this.play(queueStore.first);
},
/**

View file

@ -48,12 +48,12 @@ export default {
*
* @return {Array.<Object>}
*/
all() {
get all() {
return this.state.albums;
},
byId(id) {
return _.find(this.all(), 'id', id);
return _.find(this.all, 'id', id);
},
/**

View file

@ -37,12 +37,12 @@ export default {
return artist;
},
all() {
get all() {
return this.state.artists;
},
byId(id) {
return _.find(this.all(), 'id', id);
return _.find(this.all, 'id', id);
},
/**

View file

@ -25,7 +25,7 @@ export default {
*/
init(user = null) {
if (!user) {
user = userStore.current();
user = userStore.current;
}
this.storeKey = `preferences_${user.id}`;

View file

@ -32,29 +32,29 @@ export default {
},
/**
* Get all queued songs.
* All queued songs.
*
* @return {Array.<Object>}
*/
all() {
get all() {
return this.state.songs;
},
/**
* Get the first song in the queue.
* The first song in the queue.
*
* @return {?Object}
*/
first() {
get first() {
return _.first(this.state.songs);
},
/**
* Get the last song in the queue.
* The last song in the queue.
*
* @return {?Object}
*/
last() {
get last() {
return _.last(this.state.songs);
},
@ -157,50 +157,57 @@ export default {
},
/**
* Get the next song in queue.
* The next song in queue.
*
* @return {?Object}
*/
getNextSong() {
if (!this.current()) {
get next() {
if (!this.current) {
return _.first(this.state.songs);
}
let i = _.pluck(this.state.songs, 'id').indexOf(this.current().id) + 1;
let i = _.pluck(this.state.songs, 'id').indexOf(this.current.id) + 1;
return i >= this.state.songs.length ? null : this.state.songs[i];
},
/**
* Get the previous song in queue.
* The previous song in queue.
*
* @return {?Object}
*/
getPrevSong() {
if (!this.current()) {
get previous() {
if (!this.current) {
return _.last(this.state.songs);
}
let i = _.pluck(this.state.songs, 'id').indexOf(this.current().id) - 1;
let i = _.pluck(this.state.songs, 'id').indexOf(this.current.id) - 1;
return i < 0 ? null : this.state.songs[i];
},
/**
* Get or set the current song.
*
* @param {?Object} song
* The current song.
*
* @return {Object}
*/
current(song = null) {
if (song) {
this.state.current = song;
}
get current() {
return this.state.current;
},
/**
* Set a song as the current queued song.
*
* @param {Object} song
*
* @return {Object} The queued song.
*/
set current(song) {
this.state.current = song;
return this.current;
},
/**
* Shuffle the queue.
*

View file

@ -108,7 +108,7 @@ export default {
*
* @return {Array.<Object>}
*/
all() {
get all() {
return this.state.songs;
},
@ -233,7 +233,7 @@ export default {
* @param {?Function} cb
*/
scrobble(song, cb = null) {
if (!window.useLastfm || !userStore.current().preferences.lastfm_session_key) {
if (!window.useLastfm || !userStore.current.preferences.lastfm_session_key) {
return;
}
@ -253,7 +253,7 @@ export default {
* @param {?Function} errorCb
*/
update(songs, data, successCb = null, errorCb = null) {
if (!userStore.current().is_admin) {
if (!userStore.current.is_admin) {
return;
}

View file

@ -32,11 +32,11 @@ export default {
},
/**
* Get all users.
* All users.
*
* @return {Array.<Object>}
*/
all() {
get all() {
return this.state.users;
},
@ -52,20 +52,27 @@ export default {
},
/**
* Get or set the current user.
*
* @param {?Object} user
* The current user.
*
* @return {Object}
*/
current(user = null) {
if (user) {
this.state.current = user;
}
get current() {
return this.state.current;
},
/**
* Set the current user.
*
* @param {Object} user
*
* @return {Object}
*/
set current(user) {
this.state.current = user;
return this.current;
},
/**
* Set a user's avatar using Gravatar's service.
*
@ -73,7 +80,7 @@ export default {
*/
setAvatar(user = null) {
if (!user) {
user = this.current();
user = this.current;
}
Vue.set(user, 'avatar', `https://www.gravatar.com/avatar/${md5(user.email)}?s=256`);
@ -121,8 +128,8 @@ export default {
http.put('me', {
password,
name: this.current().name,
email: this.current().email
name: this.current.name,
email: this.current.email
}, () => {
this.setAvatar();

View file

@ -26,7 +26,7 @@ describe('stores/album', () => {
describe('#all', () => {
it('correctly returns all songs', () => {
albumStore.all().length.should.equal(7);
albumStore.all.length.should.equal(7);
});
});

View file

@ -13,19 +13,19 @@ describe('stores/queue', () => {
describe('#all', () => {
it('correctly returns all queued songs', () => {
queueStore.all().should.equal(songs);
queueStore.all.should.equal(songs);
});
});
describe('#first', () => {
it('correctly returns the first queued song', () => {
queueStore.first().title.should.equal('No bravery');
queueStore.first.title.should.equal('No bravery');
});
});
describe('#last', () => {
it('correctly returns the last queued song', () => {
queueStore.last().title.should.equal('Tears and rain');
queueStore.last.title.should.equal('Tears and rain');
});
});
@ -36,18 +36,18 @@ describe('stores/queue', () => {
it('correctly appends a song to end of the queue', () => {
queueStore.queue(song);
queueStore.last().title.should.equal('I Swear');
queueStore.last.title.should.equal('I Swear');
});
it('correctly prepends a song to top of the queue', () => {
queueStore.queue(song, false, true);
queueStore.first().title.should.equal('I Swear');
queueStore.first.title.should.equal('I Swear');
});
it('correctly replaces the whole queue', () => {
queueStore.queue(song, true);
queueStore.all().length.should.equal(1);
queueStore.first().title.should.equal('I Swear');
queueStore.all.length.should.equal(1);
queueStore.first.title.should.equal('I Swear');
});
});
@ -56,12 +56,12 @@ describe('stores/queue', () => {
it('correctly removes a song from queue', () => {
queueStore.unqueue(queueStore.state.songs[0]);
queueStore.first().title.should.equal('So long, Jimmy'); // Oh the irony.
queueStore.first.title.should.equal('So long, Jimmy'); // Oh the irony.
});
it('correctly removes mutiple songs from queue', () => {
queueStore.unqueue([queueStore.state.songs[0], queueStore.state.songs[1]]);
queueStore.first().title.should.equal('Wisemen');
queueStore.first.title.should.equal('Wisemen');
});
});
@ -74,33 +74,34 @@ describe('stores/queue', () => {
describe('#current', () => {
it('returns the correct current song', () => {
queueStore.current().title.should.equal('So long, Jimmy');
queueStore.current.title.should.equal('So long, Jimmy');
});
it('successfully sets the current song', () => {
queueStore.current(queueStore.state.songs[0]).title.should.equal('No bravery');
queueStore.current = queueStore.state.songs[0];
queueStore.current.title.should.equal('No bravery');
});
});
describe('#getNextSong', () => {
it('correctly gets the next song in queue', () => {
queueStore.getNextSong().title.should.equal('Wisemen');
queueStore.next.title.should.equal('Wisemen');
});
it('correctly returns null if at end of queue', () => {
queueStore.current(queueStore.state.songs[queueStore.state.songs.length - 1]);
(queueStore.getNextSong() === null).should.be.true;
queueStore.current = queueStore.state.songs[queueStore.state.songs.length - 1];
(queueStore.next === null).should.be.true;
});
});
describe('#getPrevSong', () => {
it('correctly gets the previous song in queue', () => {
queueStore.getPrevSong().title.should.equal('No bravery');
queueStore.previous.title.should.equal('No bravery');
});
it('correctly returns null if at end of queue', () => {
queueStore.current(queueStore.state.songs[0]);
(queueStore.getPrevSong() === null).should.be.true;
queueStore.current = queueStore.state.songs[0];
(queueStore.previous === null).should.be.true;
});
});
});

View file

@ -28,7 +28,7 @@ describe('stores/song', () => {
describe('#all', () => {
it('correctly returns all songs', () => {
songStore.all().length.should.equal(14);
songStore.all.length.should.equal(14);
});
});
@ -110,7 +110,7 @@ describe('stores/song', () => {
songStore.syncUpdatedSong(song);
// A new album should be created...
_.last(albumStore.all()).name.should.equal('Brand New Album from All-4-One');
_.last(albumStore.all).name.should.equal('Brand New Album from All-4-One');
// ...and assigned with the song.
songStore.byId(song.id).album.name.should.equal('Brand New Album from All-4-One');
@ -132,11 +132,11 @@ describe('stores/song', () => {
songStore.syncUpdatedSong(song);
// A new artist should be created...
let lastArtist = _.last(artistStore.all());
let lastArtist = _.last(artistStore.all);
lastArtist.name.should.equal('John Cena');
// A new album should be created
let lastAlbum = _.last(albumStore.all());
let lastAlbum = _.last(albumStore.all);
lastAlbum.name.should.equal("It's... John Cena!!!");
// The album must belong to John Cena of course!

View file

@ -15,7 +15,7 @@ describe('stores/user', () => {
describe('#all', () => {
it('correctly returns all users', () => {
userStore.all().should.equal(data.users);
userStore.all.should.equal(data.users);
});
});
@ -27,19 +27,19 @@ describe('stores/user', () => {
describe('#current', () => {
it('correctly gets the current user', () => {
userStore.current().id.should.equal(1);
userStore.current.id.should.equal(1);
});
it('correctly sets the current user', () => {
userStore.current(data.users[1]);
userStore.state.current.id.should.equal(2);
userStore.current = data.users[1];
userStore.current.id.should.equal(2);
});
});
describe('#setAvatar', () => {
it('correctly sets the current users avatar', () => {
userStore.setAvatar();
userStore.current().avatar.should.equal('https://www.gravatar.com/avatar/b9611f1bba1aacbe6f5de5856695a202?s=256');
userStore.current.avatar.should.equal('https://www.gravatar.com/avatar/b9611f1bba1aacbe6f5de5856695a202?s=256');
});
it('correctly sets a users avatar', () => {