summaryrefslogtreecommitdiffstats
path: root/Processing-js/libs/js/Window/Queue.js
diff options
context:
space:
mode:
Diffstat (limited to 'Processing-js/libs/js/Window/Queue.js')
-rw-r--r--Processing-js/libs/js/Window/Queue.js87
1 files changed, 87 insertions, 0 deletions
diff --git a/Processing-js/libs/js/Window/Queue.js b/Processing-js/libs/js/Window/Queue.js
new file mode 100644
index 0000000..890b61b
--- /dev/null
+++ b/Processing-js/libs/js/Window/Queue.js
@@ -0,0 +1,87 @@
+/*
+ ----------------------------------------
+ window.Queue : 0.1.1 : http://mudcu.be
+ ----------------------------------------
+ var queue = new Queue({
+ items: list,
+ oncomplete: function() {
+ queue.reset(); // infinite loop!
+ queue.next();
+ },
+ next: function(item) {
+ if (item[0] !== "." && item.indexOf(".") === -1) {
+ readDir(dir + item + "/", queue.next);
+ } else {
+ setTimeout(queue.next, 1)
+ }
+ }
+ });
+*/
+
+if (typeof(window) === "undefined") window = {};
+
+window.Queue = function(conf) {
+ var that = this;
+ /// Request the next item in stack.
+ this.next = function() {
+ var arr = that.queue;
+ /// Emit the progress of the queue.
+ if (conf.onprogress) {
+ conf.onprogress(that.length ? 1 - that.remaining / that.length : 1);
+ }
+ /// Check whether the queue is complete.
+ if (!arr.length) {
+ if (conf.oncomplete) {
+ conf.oncomplete();
+ }
+ return;
+ }
+ /// Indicate previous element as processed.
+ that.remaining --;
+ /// Cleanup previous completed dimension.
+
+ if (String(arr[0]) === "[object Object]" && !arr[0].length) {
+ arr.shift();
+ }
+ /// Process next item in multi-dimensional stack.
+ if (String(arr[0]) === "[object Object]" && arr[0].length) {
+ conf.next(arr[0].shift());
+ } else { // ditto for single-dimensional stack.
+ conf.next(arr.shift());
+ }
+ };
+ ///
+ this.reset = function(items) {
+ items = items || conf.items;
+ this.length = 0;
+ this.remaining = -1;
+ this.queue = [];
+ /// Flatten multi-dimensional objects.
+ for (var key in items) {
+ if (String(items[key]) === "[object Object]") {
+ var sub = [];
+ this.queue.push(sub);
+ for (var id in items[key]) {
+ sub.push(items[key][id]);
+ this.length ++;
+ this.remaining ++;
+ }
+ } else {
+ this.queue.push(items[key]);
+ this.length ++;
+ this.remaining ++;
+ }
+ }
+ };
+ ///
+ this.reset();
+ /// Escape event loop.
+ setTimeout(this.next, 1);
+ ///
+ return this;
+};
+
+/// For NodeJS
+if (typeof (module) !== "undefined" && module.exports) {
+ module.exports = window.Queue;
+} \ No newline at end of file