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

View file

@ -51,10 +51,7 @@ function Playlist(chan) {
"mediaUpdate": [],
"remove": [],
};
this.lock = false;
this.action_queue = [];
this.fnqueue = new AsyncQueue();
this._qaInterval = false;
AllPlaylists[name] = this;
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() {
var arr = this.items.toArray();
var pos = 0;
@ -243,88 +218,35 @@ Playlist.prototype.addMedia = function (data) {
};
};
Playlist.prototype.addYouTubePlaylist = function(data, callback) {
var pos = "append";
if(data.pos == "next") {
if(!this.current)
pos = "prepend";
else
pos = this.current.uid;
Playlist.prototype.remove = function (uid) {
var self = this;
var item = self.items.find(uid);
if (item && self.items.remove(uid)) {
if (item === self.current) {
setImmediate(function () { self._next(); });
}
return true;
} else {
return false;
}
}
var pl = this;
this.server.infogetter.getMedia(data.id, data.type, function(err, vids) {
if(err) {
callback(err, null);
return;
}
if(data.pos === "next")
vids.reverse();
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) {
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) {
Playlist.prototype.move = function (from, after) {
var it = this.items.find(from);
if (!this.items.remove(from))
return;
return false;
if (after === "prepend") {
if (!this.items.prepend(it))
return;
}
else if(after === "append") {
return false;
} else if (after === "append") {
if (!this.items.append(it))
return;
return false;
} else if (!this.items.insertAfter(it, after)) {
return false;
}
else if(!this.items.insertAfter(it, after))
return;
callback();
return true;
}
Playlist.prototype.next = function() {
@ -334,12 +256,10 @@ Playlist.prototype.next = function() {
var it = this.current;
if (it.temp) {
var pl = this;
this.remove(it.uid, function() {
pl.on("remove")(it);
});
if (this.remove(it.uid)) {
this.on("remove")(it);
}
else {
} else {
this._next();
}
@ -375,10 +295,9 @@ Playlist.prototype.jump = function(uid) {
}
if(it.temp) {
var pl = this;
this.remove(it.uid, function () {
pl.on("remove")(it);
});
if (this.remove(it.uid)) {
this.on("remove")(it);
}
}
return this.current;