Use asyncqueue clientside too

This commit is contained in:
calzoneman 2013-10-01 13:35:29 -05:00
parent acb5136c15
commit a1c72aaa8d
4 changed files with 115 additions and 75 deletions

View File

@ -1,3 +1,14 @@
/*
The MIT License (MIT)
Copyright (c) 2013 Calvin Montgomery
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var AsyncQueue = function () {
this._q = [];
this._lock = false;

View File

@ -764,33 +764,35 @@ Callbacks = {
},
queue: function(data) {
queueAction({
fn: function () {
var li = makeQueueEntry(data.item, true);
if (data.item.uid === PL_CURRENT)
li.addClass("queue_active");
li.hide();
var q = $("#queue");
li.attr("title", data.item.queueby
? ("Added by: " + data.item.queueby)
: "Added by: Unknown");
if (data.after === "prepend") {
li.prependTo(q);
li.show("blind");
return true;
} else if (data.after === "append") {
li.appendTo(q);
li.show("blind");
return true;
} else {
var liafter = playlistFind(data.after);
if (!liafter) {
return false;
}
li.insertAfter(liafter);
li.show("blind");
return true;
PL_ACTION_QUEUE.queue(function (plq) {
var li = makeQueueEntry(data.item, true);
if (data.item.uid === PL_CURRENT)
li.addClass("queue_active");
li.hide();
var q = $("#queue");
li.attr("title", data.item.queueby
? ("Added by: " + data.item.queueby)
: "Added by: Unknown");
if (data.after === "prepend") {
li.prependTo(q);
li.show("blind", function () {
plq.release();
});
} else if (data.after === "append") {
li.appendTo(q);
li.show("blind", function () {
plq.release();
});
} else {
var liafter = playlistFind(data.after);
if (!liafter) {
plq.release();
return;
}
li.insertAfter(liafter);
li.show("blind", function () {
plq.release();
});
}
});
},
@ -848,26 +850,23 @@ Callbacks = {
},
"delete": function(data) {
queueAction({
fn: function () {
PL_WAIT_SCROLL = true;
var li = $(".pluid-" + data.uid);
li.hide("blind", function() {
li.remove();
PL_WAIT_SCROLL = false;
});
return true;
}
PL_ACTION_QUEUE.queue(function (plq) {
PL_WAIT_SCROLL = true;
var li = $(".pluid-" + data.uid);
li.hide("blind", function() {
li.remove();
plq.release();
PL_WAIT_SCROLL = false;
});
});
},
moveVideo: function(data) {
if(data.moveby != CLIENT.name) {
queueAction({
fn: function () {
playlistMove(data.from, data.after);
return true;
}
if (data.moveby != CLIENT.name) {
PL_ACTION_QUEUE.queue(function (plq) {
playlistMove(data.from, data.after, function () {
plq.release();
});
});
}
},

View File

@ -9,7 +9,7 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var CL_VERSION = "2.0.0";
var CL_VERSION = "2.4.3";
var CLIENT = {
rank: -1,
@ -44,9 +44,6 @@ if($("#videowidth").length > 0) {
VWIDTH = $("#videowidth").css("width").replace("px", "");
VHEIGHT = ""+parseInt(parseInt(VWIDTH) * 9 / 16);
}
var PL_MOVING = false;
var PL_ADDING = false;
var PL_DELETING = false;
var REBUILDING = false;
var socket = {
emit: function() {

View File

@ -1191,29 +1191,58 @@ function addLibraryButtons(li, id, source) {
/* queue stuff */
var PL_QUEUED_ACTIONS = [];
var PL_ACTION_INTERVAL = false;
var AsyncQueue = function () {
this._q = [];
this._lock = false;
this._tm = 0;
};
function queueAction(data) {
PL_QUEUED_ACTIONS.push(data);
if(PL_ACTION_INTERVAL)
return;
PL_ACTION_INTERVAL = setInterval(function () {
var data = PL_QUEUED_ACTIONS.shift();
if(!("expire" in data))
data.expire = Date.now() + 5000;
if(!data.fn()) {
if(data.can_wait && Date.now() < data.expire)
PL_QUEUED_ACTIONS.push(data);
else if(Date.now() < data.expire)
PL_QUEUED_ACTIONS.unshift(data);
AsyncQueue.prototype.next = function () {
if (this._q.length > 0) {
if (!this.lock())
return;
var item = this._q.shift();
var fn = item[0], tm = item[1];
this._tm = Date.now() + item[1];
fn(this);
}
};
AsyncQueue.prototype.lock = function () {
if (this._lock) {
if (this._tm > 0 && Date.now() > this._tm) {
this._tm = 0;
return true;
}
if(PL_QUEUED_ACTIONS.length == 0) {
clearInterval(PL_ACTION_INTERVAL);
PL_ACTION_INTERVAL = false;
}
}, 100);
}
return false;
}
this._lock = true;
return true;
};
AsyncQueue.prototype.release = function () {
var self = this;
if (!self._lock)
return false;
self._lock = false;
self.next();
return true;
};
AsyncQueue.prototype.queue = function (fn) {
var self = this;
self._q.push([fn, 20000]);
self.next();
};
AsyncQueue.prototype.reset = function () {
this._q = [];
this._lock = false;
};
var PL_ACTION_QUEUE = new AsyncQueue();
// Because jQuery UI does weird things
function playlistFind(uid) {
@ -1227,10 +1256,12 @@ function playlistFind(uid) {
return false;
}
function playlistMove(from, after) {
function playlistMove(from, after, cb) {
var lifrom = $(".pluid-" + from);
if(lifrom.length == 0)
return false;
if(lifrom.length == 0) {
cb(false);
return;
}
var q = $("#queue");
@ -1238,24 +1269,26 @@ function playlistMove(from, after) {
lifrom.hide("blind", function() {
lifrom.detach();
lifrom.prependTo(q);
lifrom.show("blind");
lifrom.show("blind", cb);
});
}
else if(after === "append") {
lifrom.hide("blind", function() {
lifrom.detach();
lifrom.appendTo(q);
lifrom.show("blind");
lifrom.show("blind", cb);
});
}
else {
var liafter = $(".pluid-" + after);
if(liafter.length == 0)
return false;
if(liafter.length == 0) {
cb(false);
return;
}
lifrom.hide("blind", function() {
lifrom.detach();
lifrom.insertAfter(liafter);
lifrom.show("blind");
lifrom.show("blind", cb);
});
}
}