Optimize some code

This commit is contained in:
An Phan 2016-05-03 11:08:24 +07:00
parent 5e45f4cb4d
commit 2aa45bce03
6 changed files with 22 additions and 91 deletions

View file

@ -50,11 +50,7 @@ export default {
this.remove(song); this.remove(song);
} }
http.post('interaction/like', { song: song.id }, () => { http.post('interaction/like', { song: song.id }, () => cb && cb());
if (cb) {
cb();
}
});
}, },
/** /**
@ -94,11 +90,7 @@ export default {
each(songs, song => song.liked = true); each(songs, song => song.liked = true);
this.add(songs); this.add(songs);
http.post('interaction/batch/like', { songs: map(songs, 'id') }, () => { http.post('interaction/batch/like', { songs: map(songs, 'id') }, () => cb && cb());
if (cb) {
cb();
}
});
}, },
/** /**
@ -108,15 +100,9 @@ export default {
* @param {?Function} cb * @param {?Function} cb
*/ */
unlike(songs, cb = null) { unlike(songs, cb = null) {
// Don't wait for the HTTP response to update the status, just set them to Unliked right away.
// This may cause a minor problem if the request fails somehow, but do we care?
each(songs, song => song.liked = false); each(songs, song => song.liked = false);
this.remove(songs); this.remove(songs);
http.post('interaction/batch/unlike', { songs: map(songs, 'id') }, () => { http.post('interaction/batch/unlike', { songs: map(songs, 'id') }, () => cb && cb());
if (cb) {
cb();
}
});
}, },
}; };

View file

@ -89,9 +89,7 @@ export default {
this.getSongs(playlist); this.getSongs(playlist);
this.add(playlist); this.add(playlist);
if (cb) { cb && cb();
cb();
}
}); });
}, },
@ -106,10 +104,7 @@ export default {
http.delete(`playlist/${playlist.id}`, {}, () => { http.delete(`playlist/${playlist.id}`, {}, () => {
this.remove(playlist); this.remove(playlist);
cb && cb();
if (cb) {
cb();
}
}); });
}, },
@ -128,11 +123,7 @@ export default {
return; return;
} }
http.put(`playlist/${playlist.id}/sync`, { songs: map(playlist.songs, 'id') }, () => { http.put(`playlist/${playlist.id}/sync`, { songs: map(playlist.songs, 'id') }, () => cb && cb());
if (cb) {
cb();
}
});
}, },
/** /**
@ -145,11 +136,7 @@ export default {
removeSongs(playlist, songs, cb = null) { removeSongs(playlist, songs, cb = null) {
playlist.songs = difference(playlist.songs, songs); playlist.songs = difference(playlist.songs, songs);
http.put(`playlist/${playlist.id}/sync`, { songs: map(playlist.songs, 'id') }, () => { http.put(`playlist/${playlist.id}/sync`, { songs: map(playlist.songs, 'id') }, () => cb && cb());
if (cb) {
cb();
}
});
}, },
/** /**
@ -161,10 +148,6 @@ export default {
update(playlist, cb = null) { update(playlist, cb = null) {
NProgress.start(); NProgress.start();
http.put(`playlist/${playlist.id}`, { name: playlist.name }, () => { http.put(`playlist/${playlist.id}`, { name: playlist.name }, () => cb && cb());
if (cb) {
cb();
}
});
}, },
}; };

View file

@ -157,10 +157,7 @@ export default {
clear(cb = null) { clear(cb = null) {
this.all = []; this.all = [];
this.current = null; this.current = null;
cb && cb();
if (cb) {
cb();
}
}, },
/** /**

View file

@ -50,9 +50,7 @@ export default {
window.useLastfm = this.state.useLastfm = data.useLastfm; window.useLastfm = this.state.useLastfm = data.useLastfm;
if (successCb) { successCb && successCb();
successCb();
}
}, errorCb); }, errorCb);
}, },

View file

@ -158,9 +158,7 @@ export default {
song.album.playCount += song.playCount - oldCount; song.album.playCount += song.playCount - oldCount;
song.album.artist.playCount += song.playCount - oldCount; song.album.artist.playCount += song.playCount - oldCount;
if (cb) { cb && cb();
cb();
}
}); });
}, },
@ -186,9 +184,7 @@ export default {
getInfo(song, cb = null) { getInfo(song, cb = null) {
// Check if the song's info has been retrieved before. // Check if the song's info has been retrieved before.
if (song.infoRetrieved) { if (song.infoRetrieved) {
if (cb) { cb && cb();
cb();
}
return; return;
} }
@ -229,9 +225,7 @@ export default {
song.infoRetrieved = true; song.infoRetrieved = true;
if (cb) { cb && cb();
cb();
}
}); });
}, },
@ -246,11 +240,7 @@ export default {
return; return;
} }
http.post(`${song.id}/scrobble/${song.playStartTime}`, () => { http.post(`${song.id}/scrobble/${song.playStartTime}`, () => cb && cb());
if (cb) {
cb();
}
});
}, },
/** /**
@ -272,14 +262,8 @@ export default {
}, response => { }, response => {
each(response.data, song => this.syncUpdatedSong(song)); each(response.data, song => this.syncUpdatedSong(song));
if (successCb) { successCb && successCb();
successCb(); }, () => errorCb && errorCb());
}
}, () => {
if (errorCb) {
errorCb();
}
});
}, },
/** /**

View file

@ -105,11 +105,7 @@ export default {
*/ */
login(email, password, successCb = null, errorCb = null) { login(email, password, successCb = null, errorCb = null) {
NProgress.start(); NProgress.start();
http.post('me', { email, password }, () => { http.post('me', { email, password }, () => successCb && successCb(), errorCb);
if (successCb) {
successCb();
}
}, errorCb);
}, },
/** /**
@ -118,11 +114,7 @@ export default {
* @param {Function} cb The callback. * @param {Function} cb The callback.
*/ */
logout(cb = null) { logout(cb = null) {
http.delete('me', {}, () => { http.delete('me', {}, () => cb && cb());
if (cb) {
cb();
}
});
}, },
/** /**
@ -141,10 +133,7 @@ export default {
email: this.current.email email: this.current.email
}, () => { }, () => {
this.setAvatar(); this.setAvatar();
cb && cb();
if (cb) {
cb();
}
} }
); );
}, },
@ -166,9 +155,7 @@ export default {
this.setAvatar(user); this.setAvatar(user);
this.all.unshift(user); this.all.unshift(user);
if (cb) { cb && cb();
cb();
}
}); });
}, },
@ -188,9 +175,7 @@ export default {
this.setAvatar(user); this.setAvatar(user);
user.password = ''; user.password = '';
if (cb) { cb && cb();
cb();
}
}); });
}, },
@ -228,9 +213,7 @@ export default {
/** /**
* Brian May enters the stage. * Brian May enters the stage.
*/ */
if (cb) { cb && cb();
cb();
}
}); });
}, },
}; };