Use new asyncqueue system for moving and deleting

This commit is contained in:
calzoneman 2013-09-30 10:05:09 -05:00
parent 0eda0b8ed2
commit 3666b43f7a
2 changed files with 70 additions and 140 deletions

View file

@ -1587,15 +1587,20 @@ Channel.prototype.trySetTemp = function(user, data) {
Channel.prototype.dequeue = function(uid) { Channel.prototype.dequeue = function(uid) {
var chan = this; var self = this;
function afterDelete() { self.plqueue.queue(function (q) {
chan.sendAll("delete", { if (self.dead)
uid: uid return;
});
chan.broadcastPlaylistMeta(); if (self.playlist.remove(uid)) {
} self.sendAll("delete", {
if(!this.playlist.remove(uid, afterDelete)) uid: uid
return; });
self.broadcastPlaylistMeta();
}
q.release();
});
} }
Channel.prototype.tryDequeue = function(user, data) { Channel.prototype.tryDequeue = function(user, data) {
@ -1666,6 +1671,7 @@ Channel.prototype.tryJumpTo = function(user, data) {
Channel.prototype.clearqueue = function() { Channel.prototype.clearqueue = function() {
this.playlist.clear(); this.playlist.clear();
this.plqueue.reset();
this.sendAll("playlist", this.playlist.items.toArray()); this.sendAll("playlist", this.playlist.items.toArray());
this.broadcastPlaylistMeta(); this.broadcastPlaylistMeta();
} }
@ -1683,6 +1689,7 @@ Channel.prototype.shufflequeue = function() {
var n = []; var n = [];
var pl = this.playlist.items.toArray(false); var pl = this.playlist.items.toArray(false);
this.playlist.clear(); this.playlist.clear();
this.plqueue.reset();
while(pl.length > 0) { while(pl.length > 0) {
var i = parseInt(Math.random() * pl.length); var i = parseInt(Math.random() * pl.length);
var item = this.playlist.makeItem(pl[i].media); var item = this.playlist.makeItem(pl[i].media);
@ -1732,32 +1739,36 @@ Channel.prototype.tryUpdate = function(user, data) {
} }
Channel.prototype.move = function(data, user) { Channel.prototype.move = function(data, user) {
var chan = this; var self = this;
function afterMove() { self.plqueue.queue(function (q) {
if (chan.dead) if (self.dead)
return; return;
var moveby = user && user.name ? user.name : null; if (self.playlist.move(data.from, data.after)) {
if(typeof data.moveby !== "undefined") var moveby = user && user.name ? user.name : null;
moveby = data.moveby; if (typeof data.moveby !== "undefined")
moveby = data.moveby;
var fromit = chan.playlist.items.find(data.from); var fromit = self.playlist.items.find(data.from);
var afterit = chan.playlist.items.find(data.after); var afterit = self.playlist.items.find(data.after);
var aftertitle = afterit && afterit.media ? afterit.media.title : ""; var aftertitle = (afterit && afterit.media)
if(fromit) { ? afterit.media.title : "";
chan.logger.log("### " + user.name + " moved " + fromit.media.title if (fromit) {
+ (aftertitle ? " after " + aftertitle : "")); self.logger.log("### " + user.name + " moved " +
fromit.media.title +
(aftertitle ? " after " + aftertitle : ""));
}
self.sendAll("moveVideo", {
from: data.from,
after: data.after,
moveby: moveby
});
} }
chan.sendAll("moveVideo", { q.release();
from: data.from, });
after: data.after,
moveby: moveby
});
}
this.playlist.move(data.from, data.after, afterMove);
} }
Channel.prototype.tryMove = function(user, data) { Channel.prototype.tryMove = function(user, data) {

View file

@ -51,10 +51,7 @@ function Playlist(chan) {
"mediaUpdate": [], "mediaUpdate": [],
"remove": [], "remove": [],
}; };
this.lock = false;
this.action_queue = [];
this.fnqueue = new AsyncQueue(); this.fnqueue = new AsyncQueue();
this._qaInterval = false;
AllPlaylists[name] = this; AllPlaylists[name] = this;
this.channel = chan; this.channel = chan;
@ -88,28 +85,6 @@ function Playlist(chan) {
}); });
} }
Playlist.prototype.queueAction = function(data) {
this.action_queue.push(data);
if(this._qaInterval)
return;
var pl = this;
this._qaInterval = setInterval(function() {
var data = pl.action_queue.shift();
if(data.waiting) {
if(!("expire" in data))
data.expire = Date.now() + 10000;
if(Date.now() < data.expire)
pl.action_queue.unshift(data);
}
else
data.fn();
if(pl.action_queue.length == 0) {
clearInterval(pl._qaInterval);
pl._qaInterval = false;
}
}, 100);
}
Playlist.prototype.dump = function() { Playlist.prototype.dump = function() {
var arr = this.items.toArray(); var arr = this.items.toArray();
var pos = 0; var pos = 0;
@ -243,88 +218,35 @@ Playlist.prototype.addMedia = function (data) {
}; };
}; };
Playlist.prototype.addYouTubePlaylist = function(data, callback) { Playlist.prototype.remove = function (uid) {
var pos = "append"; var self = this;
if(data.pos == "next") { var item = self.items.find(uid);
if(!this.current) if (item && self.items.remove(uid)) {
pos = "prepend"; if (item === self.current) {
else setImmediate(function () { self._next(); });
pos = this.current.uid;
}
var pl = this;
this.server.infogetter.getMedia(data.id, data.type, function(err, vids) {
if(err) {
callback(err, null);
return;
} }
return true;
if(data.pos === "next") } else {
vids.reverse(); return false;
}
vids.forEach(function(media) {
if(data.maxlength && media.seconds > data.maxlength) {
callback("Media is too long!", null);
return;
}
var it = pl.makeItem(media);
it.temp = data.temp;
it.queueby = data.queueby;
pl.queueAction({
fn: function() {
var err = pl.add(it, pos);
callback(err, err ? null : it);
},
});
});
});
} }
Playlist.prototype.remove = function(uid, callback) { Playlist.prototype.move = function (from, after) {
var pl = this;
this.queueAction({
fn: function() {
var item = pl.items.find(uid);
if(pl.items.remove(uid)) {
if(callback)
callback();
if(item == pl.current)
pl._next();
}
},
waiting: false
});
}
Playlist.prototype.move = function(from, after, callback) {
var pl = this;
this.queueAction({
fn: function() {
pl._move(from, after, callback);
},
waiting: false
});
}
Playlist.prototype._move = function(from, after, callback) {
var it = this.items.find(from); var it = this.items.find(from);
if(!this.items.remove(from)) if (!this.items.remove(from))
return; return false;
if(after === "prepend") { if (after === "prepend") {
if(!this.items.prepend(it)) if (!this.items.prepend(it))
return; return false;
} else if (after === "append") {
if (!this.items.append(it))
return false;
} else if (!this.items.insertAfter(it, after)) {
return false;
} }
else if(after === "append") { return true;
if(!this.items.append(it))
return;
}
else if(!this.items.insertAfter(it, after))
return;
callback();
} }
Playlist.prototype.next = function() { Playlist.prototype.next = function() {
@ -333,13 +255,11 @@ Playlist.prototype.next = function() {
var it = this.current; var it = this.current;
if(it.temp) { if (it.temp) {
var pl = this; if (this.remove(it.uid)) {
this.remove(it.uid, function() { this.on("remove")(it);
pl.on("remove")(it); }
}); } else {
}
else {
this._next(); this._next();
} }
@ -375,10 +295,9 @@ Playlist.prototype.jump = function(uid) {
} }
if(it.temp) { if(it.temp) {
var pl = this; if (this.remove(it.uid)) {
this.remove(it.uid, function () { this.on("remove")(it);
pl.on("remove")(it); }
});
} }
return this.current; return this.current;