Refactor, mostly using destructuring

This commit is contained in:
An Phan 2017-01-13 00:50:00 +08:00
parent dc67de8335
commit 1a3e1e7f1c
No known key found for this signature in database
GPG key ID: 05536BB4BCDC02A2
10 changed files with 67 additions and 95 deletions

View file

@ -23,9 +23,7 @@ export const albumStore = {
this.all = reduce(artists, (albums, artist) => {
// While we're doing so, for each album, we get its length
// and keep a back reference to the artist too.
each(artist.albums, album => {
this.setupAlbum(album, artist)
})
each(artist.albums, album => this.setupAlbum(album, artist))
return albums.concat(artist.albums)
}, [])

View file

@ -25,9 +25,7 @@ export const artistStore = {
this.all = artists
// Traverse through artists array to get the cover and number of songs for each.
each(this.all, artist => {
this.setupArtist(artist)
})
each(this.all, artist => this.setupArtist(artist))
albumStore.init(this.all)
},
@ -95,7 +93,7 @@ export const artistStore = {
*/
add (artists) {
artists = [].concat(artists)
each(artists, a => this.setupArtist(a))
each(artists, artist => this.setupArtist(artist))
this.all = union(this.all, artists)
},
@ -110,9 +108,7 @@ export const artistStore = {
this.all = difference(this.all, artists)
// Remember to clear the cache
each(artists, artist => {
delete this.cache[artist.id]
})
each(artists, artist => delete this.cache[artist.id])
},
/**

View file

@ -44,9 +44,9 @@ export const favoriteStore = {
NProgress.start()
return new Promise((resolve, reject) => {
http.post('interaction/like', { song: song.id }, response => {
http.post('interaction/like', { song: song.id }, ({ data }) => {
// We don't really need to notify just for one song.
resolve(response.data)
resolve(data)
}, error => reject(error))
})
},
@ -92,9 +92,9 @@ export const favoriteStore = {
NProgress.start()
return new Promise((resolve, reject) => {
http.post('interaction/batch/like', { songs: map(songs, 'id') }, response => {
http.post('interaction/batch/like', { songs: map(songs, 'id') }, ({ data }) => {
alerts.success(`Added ${pluralize(songs.length, 'song')} into Favorites.`)
resolve(response.data)
resolve(data)
}, error => reject(error))
})
},
@ -113,9 +113,9 @@ export const favoriteStore = {
NProgress.start()
return new Promise((resolve, reject) => {
http.post('interaction/batch/unlike', { songs: map(songs, 'id') }, response => {
http.post('interaction/batch/unlike', { songs: map(songs, 'id') }, ({ data }) => {
alerts.success(`Removed ${pluralize(songs.length, 'song')} from Favorites.`)
resolve(response.data)
resolve(data)
}, error => reject(error))
})
}

View file

@ -101,8 +101,7 @@ export const playlistStore = {
NProgress.start()
return new Promise((resolve, reject) => {
http.post('playlist', { name, songs }, response => {
const playlist = response.data
http.post('playlist', { name, songs }, ({ data: playlist }) => {
playlist.songs = songs
this.objectifySongs(playlist)
this.add(playlist)
@ -121,10 +120,10 @@ export const playlistStore = {
NProgress.start()
return new Promise((resolve, reject) => {
http.delete(`playlist/${playlist.id}`, {}, response => {
http.delete(`playlist/${playlist.id}`, {}, ({ data }) => {
this.remove(playlist)
alerts.success(`Deleted playlist "${playlist.name}".`)
resolve(response.data)
resolve(data)
}, error => reject(error))
})
},
@ -182,7 +181,12 @@ export const playlistStore = {
NProgress.start()
return new Promise((resolve, reject) => {
http.put(`playlist/${playlist.id}`, { name: playlist.name }, () => resolve(playlist), error => reject(error))
http.put(
`playlist/${playlist.id}`,
{ name: playlist.name },
() => resolve(playlist),
error => reject(error)
)
})
}
}

View file

@ -43,7 +43,7 @@ export const preferenceStore = {
each(Object.keys(this.state), key => {
Object.defineProperty(this, key, {
get: () => this.state[key],
set: (value) => {
set: value => {
this.state[key] = value
this.save()
},

View file

@ -168,9 +168,9 @@ export const queueStore = {
return first(this.all)
}
const idx = map(this.all, 'id').indexOf(this.current.id) + 1
const index = map(this.all, 'id').indexOf(this.current.id) + 1
return idx >= this.all.length ? null : this.all[idx]
return index >= this.all.length ? null : this.all[index]
},
/**
@ -183,9 +183,9 @@ export const queueStore = {
return last(this.all)
}
const idx = map(this.all, 'id').indexOf(this.current.id) - 1
const index = map(this.all, 'id').indexOf(this.current.id) - 1
return idx < 0 ? null : this.all[idx]
return index < 0 ? null : this.all[index]
},
/**

View file

@ -19,9 +19,9 @@ export const settingStore = {
update () {
return new Promise((resolve, reject) => {
http.post('settings', this.all, response => {
http.post('settings', this.all, ({ data }) => {
alerts.success('Settings saved.')
resolve(response.data)
resolve(data)
}, error => reject(error))
})
}

View file

@ -1,37 +1,39 @@
import { assign } from 'lodash'
import { assign, clone } from 'lodash'
import isMobile from 'ismobilejs'
import { http } from '../services'
import { userStore, preferenceStore, artistStore, songStore, playlistStore, queueStore, settingStore } from '.'
const emptyState = {
songs: [],
albums: [],
artists: [],
favorites: [],
queued: [],
interactions: [],
users: [],
settings: [],
currentUser: null,
playlists: [],
useLastfm: false,
useYouTube: false,
useiTunes: true,
allowDownload: false,
currentVersion: '',
latestVersion: '',
cdnUrl: '',
originalMediaPath: ''
}
export const sharedStore = {
state: {
songs: [],
albums: [],
artists: [],
favorites: [],
queued: [],
interactions: [],
users: [],
settings: [],
currentUser: null,
playlists: [],
useLastfm: false,
useYouTube: false,
useiTunes: true,
allowDownload: false,
currentVersion: '',
latestVersion: '',
cdnUrl: '',
originalMediaPath: ''
},
state: clone(emptyState),
init () {
this.reset()
return new Promise((resolve, reject) => {
http.get('data', response => {
assign(this.state, response.data)
http.get('data', ({ data }) => {
assign(this.state, data)
// Don't allow downloading on mobile devices
this.state.allowDownload = this.state.allowDownload && !isMobile.any
@ -60,22 +62,6 @@ export const sharedStore = {
},
reset () {
this.state.songs = []
this.state.albums = []
this.state.artists = []
this.state.favorites = []
this.state.queued = []
this.state.interactions = []
this.state.users = []
this.state.settings = []
this.state.currentUser = null
this.state.playlists = []
this.state.useLastfm = false
this.state.useYouTube = false
this.state.useiTunes = true
this.state.allowDownload = false
this.state.currentVersion = ''
this.state.latestVersion = ''
this.state.cdnUrl = ''
this.state = clone(emptyState)
}
}

View file

@ -180,13 +180,13 @@ export const songStore = {
return new Promise((resolve, reject) => {
const oldCount = song.playCount
http.post('interaction/play', { song: song.id }, response => {
http.post('interaction/play', { song: song.id }, ({ data }) => {
// Use the data from the server to make sure we don't miss a play from another device.
song.playCount = response.data.play_count
song.playCount = data.play_count
song.album.playCount += song.playCount - oldCount
song.artist.playCount += song.playCount - oldCount
resolve(response.data)
resolve(data)
}, error => reject(error))
})
},
@ -215,8 +215,8 @@ export const songStore = {
*/
scrobble (song) {
return new Promise((resolve, reject) => {
http.post(`${song.id}/scrobble/${song.playStartTime}`, {}, response => {
resolve(response.data)
http.post(`${song.id}/scrobble/${song.playStartTime}`, {}, ({ data }) => {
resolve(data)
}, error => reject(error))
})
},
@ -232,8 +232,7 @@ export const songStore = {
http.put('songs', {
data,
songs: map(songs, 'id')
}, response => {
const songs = response.data
}, ({ songs }) => {
each(songs, song => this.syncUpdatedSong(song))
alerts.success(`Updated ${pluralize(songs.length, 'song')}.`)
resolve(songs)
@ -396,13 +395,5 @@ export const songStore = {
*/
getRecentlyAdded (n = 10) {
return take(orderBy(this.all, 'created_at', 'desc'), n)
},
/**
* Called when the application is torn down.
* Reset stuff.
*/
teardown () {
this.state.recentlyPlayed = []
}
}

View file

@ -105,8 +105,8 @@ export const userStore = {
NProgress.start()
return new Promise((resolve, reject) => {
http.post('me', { email, password }, response => {
resolve(response.data)
http.post('me', { email, password }, ({ data }) => {
resolve(data)
}, error => reject(error))
})
},
@ -116,8 +116,8 @@ export const userStore = {
*/
logout () {
return new Promise((resolve, reject) => {
http.delete('me', {}, response => {
resolve(response.data)
http.delete('me', {}, ({ data }) => {
resolve(data)
}, error => reject(error))
})
},
@ -155,8 +155,7 @@ export const userStore = {
NProgress.start()
return new Promise((resolve, reject) => {
http.post('user', { name, email, password }, response => {
const user = response.data
http.post('user', { name, email, password }, ({ data: user }) => {
this.setAvatar(user)
this.all.unshift(user)
alerts.success(`New user &quot;${name}&quot; created.`)
@ -178,10 +177,8 @@ export const userStore = {
return new Promise((resolve, reject) => {
http.put(`user/${user.id}`, { name, email, password }, () => {
this.setAvatar(user)
user.name = name
user.email = email
user.password = ''
this.setAvatar(user);
[user.name, user.email, user.password] = [name, email, '']
alerts.success('User profile updated.')
resolve(user)
}, error => reject(error))
@ -197,7 +194,7 @@ export const userStore = {
NProgress.start()
return new Promise((resolve, reject) => {
http.delete(`user/${user.id}`, {}, response => {
http.delete(`user/${user.id}`, {}, ({ data }) => {
this.all = without(this.all, user)
alerts.success(`User &quot;${user.name}&quot; deleted.`)
@ -223,7 +220,7 @@ export const userStore = {
/**
* Brian May enters the stage.
*/
resolve(response.data)
resolve(data)
}, error => reject(error))
})
}