Added Task.unblock(), updated block check and update handling mechanism.

This commit is contained in:
Michael Jumper
2012-01-12 10:48:31 -08:00
parent e776fbfcec
commit 0281657078

View File

@@ -201,8 +201,18 @@ Guacamole.Layer = function(width, height) {
* @private * @private
* @param {function} taskHandler The function to call when this task * @param {function} taskHandler The function to call when this task
* runs, if any. * runs, if any.
* @param {boolean} blocked Whether this task should start blocked.
*/ */
function Task(taskHandler) { function Task(taskHandler, blocked) {
var task = this;
/**
* Whether this Task is blocked.
*
* @type boolean
*/
this.blocked = blocked;
/** /**
* The handler this Task is associated with, if any. * The handler this Task is associated with, if any.
@@ -211,6 +221,14 @@ Guacamole.Layer = function(width, height) {
*/ */
this.handler = taskHandler; this.handler = taskHandler;
/**
* Unblocks this Task, allowing it to run.
*/
this.unblock = function() {
task.blocked = false;
handlePendingTasks();
}
} }
/** /**
@@ -220,21 +238,22 @@ Guacamole.Layer = function(width, height) {
* *
* @private * @private
* @param {function} handler The function to call when possible, if any. * @param {function} handler The function to call when possible, if any.
* @param {boolean} blocked Whether the task should start blocked.
* @returns {Task} The Task created and added to the queue for future * @returns {Task} The Task created and added to the queue for future
* running, if any, or null if the handler was run * running, if any, or null if the handler was run
* immediately and no Task needed to be created. * immediately and no Task needed to be created.
*/ */
function scheduleTask(handler) { function scheduleTask(handler, blocked) {
// If no pending tasks, just call (if available) and exit // If no pending tasks, just call (if available) and exit
if (layer.isReady() && handler != null) { if (layer.isReady() && !blocked && handler != null) {
handler(); handler();
return null; return null;
} }
// If tasks are pending/executing, schedule a pending task // If tasks are pending/executing, schedule a pending task
// and return a reference to it. // and return a reference to it.
var task = new Task(handler); var task = new Task(handler, blocked);
tasks.push(task); tasks.push(task);
return task; return task;
@@ -256,9 +275,9 @@ Guacamole.Layer = function(width, height) {
// Draw all pending tasks. // Draw all pending tasks.
var task; var task;
while ((task = tasks[0]) != null && task.handler) { while ((task = tasks[0]) != null && !task.blocked) {
tasks.shift(); tasks.shift();
task.handler(); if (task.handler) task.handler();
} }
tasksInProgress = false; tasksInProgress = false;
@@ -345,21 +364,14 @@ Guacamole.Layer = function(width, height) {
* @param {String} url The URL of the image to draw. * @param {String} url The URL of the image to draw.
*/ */
this.draw = function(x, y, url) { this.draw = function(x, y, url) {
var task = scheduleTask(null);
var image = new Image(); var task = scheduleTask(function() {
image.onload = function() {
task.handler = function() {
if (layer.autosize != 0) fitRect(x, y, image.width, image.height); if (layer.autosize != 0) fitRect(x, y, image.width, image.height);
displayContext.drawImage(image, x, y); displayContext.drawImage(image, x, y);
}; }, true);
// As this task originally had no handler and may have blocked var image = new Image();
// other tasks, handle any blocked tasks. image.onload = task.unblock;
handlePendingTasks();
};
image.src = url; image.src = url;
}; };
@@ -398,7 +410,7 @@ Guacamole.Layer = function(width, height) {
*/ */
this.copyRect = function(srcLayer, srcx, srcy, srcw, srch, x, y) { this.copyRect = function(srcLayer, srcx, srcy, srcw, srch, x, y) {
var srcCopied = null; var srcLock = null;
function doCopyRect() { function doCopyRect() {
if (layer.autosize != 0) fitRect(x, y, srcw, srch); if (layer.autosize != 0) fitRect(x, y, srcw, srch);
@@ -408,8 +420,8 @@ Guacamole.Layer = function(width, height) {
displayContext.drawImage(srcCanvas, srcx, srcy, srcw, srch, x, y, srcw, srch); displayContext.drawImage(srcCanvas, srcx, srcy, srcw, srch, x, y, srcw, srch);
// Unblock the copy complete task, if it exists // Unblock the copy complete task, if it exists
if (srcCopied != null) if (srcLock != null)
srcCopied.handler = function() {}; srcLock.unblock();
} }
// If we ARE the source layer, no need to sync. // If we ARE the source layer, no need to sync.
@@ -422,24 +434,15 @@ Guacamole.Layer = function(width, height) {
// Task which will be unblocked only when the source layer is ready // Task which will be unblocked only when the source layer is ready
// This task will perform the copy // This task will perform the copy
var task = scheduleTask(null); var task = scheduleTask(doCopyRect, true);
// Task which will unblock the drawing task. // Task which will unblock the drawing task.
var srcReady = srcLayer.sync(function() { var srcReady = srcLayer.sync(task.unblock);
task.handler = doCopyRect;
// As this task originally had no handler and may have blocked
// other tasks, handle any blocked tasks.
handlePendingTasks();
});
// Task which will be unblocked only after the copy has // Task which will be unblocked only after the copy has
// occurred (this will only be created if the draw task // occurred (this will only be created if the draw task
// was postponed) // was postponed)
if (srcReady != null) if (srcReady != null) srcLock = srcLayer.sync(null, true);
srcCopied = srcLayer.sync(null);
} }