Update task.js

This commit is contained in:
Namhyeon Go 2021-07-17 01:27:22 +09:00 committed by GitHub
parent 1e3b0dc47e
commit 23a3e27058
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,6 +22,7 @@ var Task = function(f, params) {
this.nextTask = null;
this.when = 0;
this.delay = 0;
this.callCount = 0;
this.setNextTask = function(task) {
this.nextTask = task;
};
@ -34,6 +35,10 @@ var Task = function(f, params) {
this.delay = delay;
return this;
};
this.upCallCount = function() {
this.callCount++;
return this;
};
};
var TaskQueue = function() {
@ -67,13 +72,14 @@ var TaskQueue = function() {
var i = delta.indexOf(Math.min.apply(Math, delta));
var d = this.queue[i].when + this.queue[i].delay - now.getTime();
if (i > -1 && d <= 0) {
console.log("Task", i, d);
task = this.queue.splice(i, 1)[0];
}
}
} catch(e) {
console.error("TaskQueue.get: " + e.message);
}
return task;
};
this.next = function() {
@ -84,10 +90,11 @@ var TaskQueue = function() {
if (task != null) {
try {
result = task.f.apply(null, [task].concat(task.params));
result = task.f.apply(Task, [task].concat(task.params));
if (task.nextTask != null) {
this.put(task.nextTask, task.nextTask.delay);
}
task.upCallCount(); // 호출 횟수 기록
} catch (e) {
console.error("Task exception: " + e.message);
console.error("task.f: " + typeof(task.f));
@ -164,6 +171,9 @@ exports.stop = function(q) {
q.stop();
};
exports.Task = Task;
exports.TaskQueue = TaskQueue;
exports.VERSIONINFO = "Task Module (task.js) version 0.2";
exports.global = global;
exports.require = require;