diff --git a/lib/task.js b/lib/task.js index 25b6667..8246097 100644 --- a/lib/task.js +++ b/lib/task.js @@ -5,25 +5,34 @@ /* // example: * // var TASK = require("lib/task"); * // var taskQueue = TASK.createTaskQueue(); - * // TASK.putTask(queue, TASK.createTask(function() { console.log(a + b + c); sleep(100); }, [1, 2, 3])) - * // .then(TASK.createTask(function(a, b, c) { console.log(a + b + c); sleep(200); }, [4, 5, 6])) - * // .then(TASK.createTask(function(a, b, c) { console.log(a + b + c); sleep(300); }, [7, 8, 9])) + * // TASK.putTask(queue, TASK.createTask(function(task, a, b, c) { console.log(a + b + c); sleep(100); }, [1, 2, 3])) + * // .then(TASK.createTask(function(task, a, b, c) { console.log(a + b + c); sleep(200); }, [4, 5, 6])) + * // .then(TASK.createTask(function(task, a, b, c) { console.log(a + b + c); sleep(300); }, [7, 8, 9])) * // ; - * // TASK.putTask(queue, TASK.createTask(function() { console.log(a + b + c); sleep(100); }, [3, 2, 1]) - * // .then(TASK.createTask(function(a, b, c) { console.log(a + b + c); sleep(200); }, [6, 5, 4])) - * // .then(TASK.createTask(function(a, b, c) { TASK.stop(); console.log(a + b + c); sleep(300); }, [9, 8, 7])) + * // TASK.putTask(queue, TASK.createTask(function(task, a, b, c) { console.log(a + b + c); sleep(100); }, [3, 2, 1]) + * // .then(TASK.createTask(function(task, a, b, c) { console.log(a + b + c); sleep(200); }, [6, 5, 4])) + * // .then(TASK.createTask(function(task, a, b, c) { TASK.stop(); console.log(a + b + c); sleep(300); }, [9, 8, 7])) * // ; * // taskQueue.run(); */ -var Task = function(f, params, when) { +var Task = function(f, params) { this.f = f; this.params = params; this.nextTask = null; - this.when = (typeof(when) === "number" ? when : 0); + this.when = 0; this.setNextTask = function(task) { this.nextTask = task; }; + this.setWhen = function(when) { + this.when = when; + }; + this.clone = function() { + var task = new Task(this.f, this.params); + task.setNextTask(this.nextTask); + task.setWhen(this.when); + return task; + }; }; var TaskQueue = function() { @@ -42,25 +51,21 @@ var TaskQueue = function() { }; this.get = function() { var task = null; - //var now = new Date().getTime(); - //var delta = now; + var now = new Date(); try { if (this.queue.length > 0) { - /* - var k = 0; - for (var i = 0; i < this.queue.length; i++) { - var _delta = now - this.queue[i].when; - if (delta > _delta) { - k = i; - delta = _delta; - } - } - task = this.queue[k]; - this.queue = this.queue.splice(k, 1); - */ task = this.queue[0]; this.queue = this.queue.slice(1); + + if (task.when > 0) { + var delta = task.when - now.getTime(); + if (delta > 0) { + var _task = task; + this.put(_task); + task = null; + } + } } } catch(e) { console.error("TaskQueue.get: " + e.message); @@ -76,16 +81,16 @@ var TaskQueue = function() { if (task != null) { try { + task.params.unshift(task); result = task.f.apply(null, task.params); + if (task.nextTask != null) { + this.put(task.nextTask); + } } catch (e) { console.error("Task exception: " + e.message); console.error("task.f: " + typeof(task.f)); console.error("task.params: " + typeof(task.params)); } - - if (task.nextTask != null) { - this.put(task.nextTask); - } } } catch(e) { console.error("TaskQueue.next: " + e.message);