From 625ef2a74f87a913eddb48deeb7964e30df18779 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 11 Jul 2013 04:12:56 +0400 Subject: [PATCH] some cleanup and refactoring... Signed-off-by: Alex A. Naanou --- ui/compatibility.js | 8 +- ui/files.js | 9 +- ui/lib/jli.js | 204 +++++++++++++++++++++++--------------------- 3 files changed, 116 insertions(+), 105 deletions(-) diff --git a/ui/compatibility.js b/ui/compatibility.js index 6ef19ac3..0ddef251 100755 --- a/ui/compatibility.js +++ b/ui/compatibility.js @@ -362,12 +362,8 @@ if(window.CEF_dumpJSON != null){ } window._PREVIW_CREATE_QUEUE = null + // Queued version of makeImagesPreviews(...) - // - // XXX check if we are leaking the tail... - // NOTE: this will remove the old deferred if it us resolved, thus - // clearing the "log" of previous operations, unless keep_log - // is set to true... window.makeImagesPreviewsQ = function(gids, sizes, mode){ gids = gids == null ? getClosestGIDs() : gids @@ -379,8 +375,10 @@ if(window.CEF_dumpJSON != null){ var queue = _PREVIW_CREATE_QUEUE } + // attach the workers to the queue... $.each(gids, function(_, gid){ queue.enqueue(makeImagePreviews, gid, sizes, mode) + // XXX do we need to report seporate previews??? //.progress(function(state){ queue.notify(state) }) .always(function(){ queue.notify(gid, 'done') }) }) diff --git a/ui/files.js b/ui/files.js index 388ec214..efe54ecd 100755 --- a/ui/files.js +++ b/ui/files.js @@ -708,22 +708,23 @@ function updateImagesOrientation(gids, no_update_loaded){ // queued version of updateImagesOrientation(...) // -// NOTE: this will ignore errors. +// XXX do we need to auto-stop this??? function updateImagesOrientationQ(gids, no_update_loaded){ gids = gids == null ? getClosestGIDs() : gids var queue = makeDeferredsQ().start() var last = null + // attach workers to queue... $.each(gids, function(_, gid){ last = queue.enqueue(updateImageOrientation, gid, no_update_loaded) - .done(function(o){ queue.notify(gid, 'done') }) + .done(function(){ queue.notify(gid, 'done') }) .fail(function(){ queue.notify(gid, 'fail') }) }) + // auto-stop the queue... if(last != null){ - // auto-stop the queue... - // NOTE: this is mostly for the saik of reporting... + // NOTE: this is mostly for reporting... // XXX do we need to auto-stop this??? last.done(function(){ queue.resolve() diff --git a/ui/lib/jli.js b/ui/lib/jli.js index 0880a64d..316e1e2f 100755 --- a/ui/lib/jli.js +++ b/ui/lib/jli.js @@ -570,6 +570,114 @@ jQuery.fn.sortChildren = function(func){ +/************************************************** Deferred utils ***/ + +// Deferred worker queue +// +// This will either create a new queue or attach to the tail of an +// existing queue (deferred) if given. +// +// This will return a deferred object with several extensions: +// +// .enqueue(worker, ...) +// Add a worker to the queue. +// A worker is triggered by the previous worker in queue +// getting resolved. +// NOTE: A worker must return a deferred. +// NOTE: all the arguments to this except for the first (the +// worker itself) will be passed to the worker when it is +// called. +// +// .start() +// Start the first worker. +// +// .kill() +// Stop the queue, preventing any new workers from starting. +// NOTE: this will not kill the currently running worker. +// NOTE: after killing a queue it can not be restarted. +// +// .isWorking() +// will return true if there is at least one worker still not +// resolved, false otherwise. +// NOTE: if the queue is killed, this will always return false. +// +// +// NOTE: the queue is not started by default. +// NOTE: one queue is guaranteed to work in a sequence, to run several +// pipelines in parallel use two or more queues. +// NOTE: running queues in parallel depends on the actual context in +// use (browser/node.js/...). +// +// XXX should this be restartable??? +// XXX check if this leaks used nodes... +function makeDeferredsQ(first){ + first = first == null ? $.Deferred() : first + + var last = first + + // XXX make this a deferred-like cleanly, rather than by monkey patching... + var queue = $.Deferred() + + // Add a worker to queue... + // + // NOTE: .enqueue(...) accepts a worker and any number of the arguments + // to be passed to the worker when it's its turn. + // NOTE: the worker must porduce a deffered/promice. + queue.enqueue = function(deffered){ + var cur = $.Deferred() + var args = Array.apply(null, arguments).slice(1) + + last.done(function(){ + + // see if we are killed... + if(queue.state() == 'resolved'){ + // this will kill the queue as we continue only on success... + cur.reject() + return + } + + // do the work... + deffered.apply(null, args) + .done(function(o){ + cur.resolve(o) + }) + .fail(function(){ + cur.resolve('fail') + }) + }) + + last = cur + + return cur + } + + // Start the work... + queue.start = function(){ + first.resolve() + return this + } + + // Kill the queue... + queue.kill = function(){ + this.resolve() + return this + } + + // Report work state... + // XXX make this a propper state, or integrate into the deferred in + // a more natural way... + queue.isWorking = function(){ + if(queue.state() != 'resolved' && last.state() != 'resolved'){ + return true + } + return false + } + + return queue +} + + + /**************************************************** JS utilities ***/ String.prototype.capitalize = function(){ @@ -678,102 +786,6 @@ function assyncCall(func){ } -// Deferred worker queue -// -// This will either create a new queue or attach to the tail of an -// existing queue (deferred) if given. -// -// This will return a deferred object with several extensions: -// -// .enqueue(worker, ...) -// Add a worker to the queue. -// A worker is executed when the previous worker is resolved. -// A worker must return a deferred. -// -// .start() -// Start the first worker. -// -// .kill() -// Stop the queue, preventing any new workers from starting. -// NOTE: this will not kill the currently running worker. -// -// .isWorking() -// will return true if there is at least one worker still not -// resolved, false otherwise. -// NOTE: if the queue is killed, this will always return false. -// -// -// NOTE: one queue is guaranteed to work in a sequence, to run several -// pipelines in parallel use two or more queues. -// NOTE: running queues in parallel depends on the actual context in -// use (browser/node.js/...). -function makeDeferredsQ(first){ - first = first == null ? $.Deferred() : first - - var last = first - - // XXX make this a deferred-like cleanly, rather than by monkey patching... - var queue = $.Deferred() - - // Add a worker to queue... - // - // NOTE: .enqueue(...) accepts a worker and any number of the arguments - // to be passed to the worker when it's its turn. - // NOTE: the worker must porduce a deffered/promice. - queue.enqueue = function(deffered){ - var cur = $.Deferred() - var args = Array.apply(null, arguments).slice(1) - - last.done(function(){ - - // see if we are killed... - if(queue.state() == 'resolved'){ - // this will kill the queue as we continue only on success... - cur.reject() - return - } - - // do the work... - deffered.apply(null, args) - .done(function(o){ - cur.resolve(o) - }) - .fail(function(){ - cur.resolve('fail') - }) - }) - - last = cur - - return cur - } - - // Start the work... - queue.start = function(){ - first.resolve() - return this - } - - // Kill the queue... - queue.kill = function(){ - this.resolve() - return this - } - - // Report work state... - // XXX make this a propper state, or integrate into the deferred in - // a more natural way... - queue.isWorking = function(){ - if(queue.state() != 'resolved' && last.state() != 'resolved'){ - return true - } - return false - } - - return queue -} - - /********************************************************************** * vim:set ts=4 sw=4 : */