I think I'm on the right track here

This commit is contained in:
Calvin Montgomery 2013-07-02 23:19:17 -04:00
parent ee36e0f30f
commit fc5034d26a
3 changed files with 68 additions and 48 deletions

View File

@ -165,7 +165,7 @@ Channel.prototype.loadDump = function() {
if(e.temp !== undefined) { if(e.temp !== undefined) {
p.temp = e.temp; p.temp = e.temp;
} }
this.playlist.append(p); this.playlist.items.append(p);
} }
this.sendAll("playlist", this.playlist.items.toArray()); this.sendAll("playlist", this.playlist.items.toArray());
if(this.playlist.current) if(this.playlist.current)

View File

@ -61,9 +61,18 @@ Playlist.prototype.queueAction = function(data) {
this._qaInterval = setInterval(function() { this._qaInterval = setInterval(function() {
if(!pl.lock) { if(!pl.lock) {
var data = pl.alter_queue.shift(); var data = pl.alter_queue.shift();
pl[data.fn].apply(pl, data.args); if(data.waiting) {
if(!("expire" in data))
data.expire = Date.now() + 5000;
if(Date.now() < data.expire)
pl.alter_queue.unshift(data);
return;
}
//pl[data.fn].apply(pl, data.args);
data.fn();
if(pl.alter_queue.length == 0) { if(pl.alter_queue.length == 0) {
clearInterval(pl._qaInterval); clearInterval(pl._qaInterval);
pl._qaInterval = false;
} }
} }
}, 100); }, 100);
@ -73,7 +82,7 @@ Playlist.prototype.dump = function() {
var arr = this.items.toArray(); var arr = this.items.toArray();
var pos = 0; var pos = 0;
for(var i in arr) { for(var i in arr) {
if(arr[i].uid == this.current.uid) { if(this.current && arr[i].uid == this.current.uid) {
pos = i; pos = i;
break; break;
} }
@ -128,23 +137,23 @@ Playlist.prototype.makeItem = function(media) {
} }
Playlist.prototype.add = function(item, pos) { Playlist.prototype.add = function(item, pos) {
var success;
if(pos == "append") if(pos == "append")
return this.items.append(item); success = this.items.append(item);
else if(pos == "prepend") else if(pos == "prepend")
return this.items.prepend(item); success = this.items.prepend(item);
else else
return this.items.insertAfter(item, pos); success = this.items.insertAfter(item, pos);
if(success && this.items.length == 1) {
this.current = item;
this.startPlayback();
}
return success;
} }
Playlist.prototype.addMedia = function(data, callback) { Playlist.prototype.addMedia = function(data, callback) {
if(this.lock) {
this.queueAction({
fn: "addMedia",
args: arguments
});
return;
}
this.lock = true;
var pos = "append"; var pos = "append";
if(data.pos == "next") { if(data.pos == "next") {
if(!this.current) if(!this.current)
@ -153,55 +162,66 @@ Playlist.prototype.addMedia = function(data, callback) {
pos = this.current.uid; pos = this.current.uid;
} }
var it = this.makeItem(null);
var pl = this; var pl = this;
var action = {
fn: function() {
if(pl.add(it, pos))
callback(false, it);
},
waiting: true
};
this.queueAction(action);
InfoGetter.getMedia(data.id, data.type, function(err, media) { InfoGetter.getMedia(data.id, data.type, function(err, media) {
if(err) { if(err) {
action.expire = 0;
callback(err, null); callback(err, null);
pl.lock = false;
return; return;
} }
var it = pl.makeItem(media); it.media = media;
it.temp = data.temp; it.temp = data.temp;
it.queueby = data.queueby; it.queueby = data.queueby;
if(!pl.add(it, pos)) action.waiting = false;
callback(true, null); });
else }
callback(false, it);
pl.lock = false; Playlist.prototype.addMediaList = function(data, callback) {
if(data.pos == "next")
data.list = data.list.reverse();
var pl = this;
data.list.forEach(function(x) {
x.pos = data.pos;
pl.addMedia(x, callback);
}); });
} }
Playlist.prototype.remove = function(uid, callback) { Playlist.prototype.remove = function(uid, callback) {
if(this.lock) { var pl = this;
this.queueAction({ this.queueAction({
fn: "remove", fn: function() {
args: arguments var item = pl.items.find(uid);
}); if(pl.items.remove(uid)) {
return; if(item == pl.current)
} pl._next();
this.lock = true; if(callback)
var item = this.items.find(uid); callback();
if(this.items.remove(uid)) { }
if(item == this.current) },
this._next(); waiting: false
if(callback) });
callback();
}
this.lock = false;
} }
Playlist.prototype.move = function(from, after, callback) { Playlist.prototype.move = function(from, after, callback) {
if(this.lock) { var pl = this;
this.queueAction({ this.queueAction({
fn: "move", fn: function() {
args: arguments pl._move(from, after, callback);
}); },
return; waiting: false
} });
this.lock = true;
this._move(from, after, callback);
this.lock = false;
} }
Playlist.prototype._move = function(from, after, callback) { Playlist.prototype._move = function(from, after, callback) {

View File

@ -484,7 +484,7 @@ User.prototype.initCallbacks = function() {
return; return;
} }
var pl = this.channel.playlist.toArray(); var pl = this.channel.playlist.items.toArray();
var result = Database.saveUserPlaylist(pl, this.name, data.name); var result = Database.saveUserPlaylist(pl, this.name, data.name);
this.socket.emit("savePlaylist", { this.socket.emit("savePlaylist", {
success: result, success: result,