Browse Source

Cleaned up one function in stations logic.

KrisVos130 8 years ago
parent
commit
d7a59c78b7
1 changed files with 34 additions and 32 deletions
  1. 34 32
      backend/logic/stations.js

+ 34 - 32
backend/logic/stations.js

@@ -85,38 +85,40 @@ module.exports = {
 	initializeStation: function(stationId, cb) {
 	initializeStation: function(stationId, cb) {
 		if (typeof cb !== 'function') cb = ()=>{};
 		if (typeof cb !== 'function') cb = ()=>{};
 		let _this = this;
 		let _this = this;
-		_this.getStation(stationId, (err, station) => {
+		async.waterfall([
-			if (!err) {
+			(next) => {
-				if (station) {
+				_this.getStation(stationId, next);
-					let notification = notifications.subscribe(`stations.nextSong?id=${station._id}`, _this.skipStation(station._id), true);
+			},
-					if (!station.paused ) {
+			(station, next) => {
-						/*if (!station.startedAt) {
+				if (!station) return next('Station not found.');
-						 station.startedAt = Date.now();
+				notifications.subscribe(`stations.nextSong?id=${station._id}`, _this.skipStation(station._id), true);
-						 station.timePaused = 0;
+				if (station.paused) {
-						 cache.hset('stations', stationId, station);
+					notifications.unschedule(`stations.nextSong?id${station._id}`);
-						 }*/
+					return next(true, station);
-						if (station.currentSong) {
+				}
-							let timeLeft = ((station.currentSong.duration * 1000) - (Date.now() - station.startedAt - station.timePaused));
+				next(null, station);
-							if (isNaN(timeLeft)) timeLeft = -1;
+			},
-							if (station.currentSong.duration * 1000 < timeLeft || timeLeft < 0) {
+			(station, next) => {
-								this.skipStation(station._id)((err, station) => {
+				if (!station.currentSong) {
-									cb(err, station);
+					return _this.skipStation(station._id)((err, station) => {
-								});
+						if (err) return next(err);
-							} else {
+						return next(true, station);
-								notifications.schedule(`stations.nextSong?id=${station._id}`, timeLeft);
+					});
-								cb(null, station);
+				}
-							}
+				let timeLeft = ((station.currentSong.duration * 1000) - (Date.now() - station.startedAt - station.timePaused));
-						} else {
+				if (isNaN(timeLeft)) timeLeft = -1;
-							_this.skipStation(station._id)((err, station) => {
+				if (station.currentSong.duration * 1000 < timeLeft || timeLeft < 0) {
-								cb(err, station);
+					this.skipStation(station._id)((err, station) => {
-							});
+						next(err, station);
-						}
+					});
-					} else {
+				} else {
-						notifications.unschedule(`stations.nextSong?id${station._id}`);
+					notifications.schedule(`stations.nextSong?id=${station._id}`, timeLeft);
-						cb(null, station);
+					next(null, station);
-					}
+				}
-				} else cb("Station not found");
+			}
-			} else cb(err);
+		], (err, station) => {
+			if (err && err !== true) return cb(err);
+			cb(null, station);
 		});
 		});
 	},
 	},