2021-06-22 21:04:11 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
2022-09-22 08:02:41 +00:00
|
|
|
// Task API (Time-sharing based `async`, `setTimeout`, `setInterval`, implementation in WSH.js)
|
2021-06-22 21:04:11 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-06-23 03:03:44 +00:00
|
|
|
/* // example:
|
|
|
|
* // var TASK = require("lib/task");
|
2021-06-23 03:05:39 +00:00
|
|
|
* // var taskQueue = TASK.createTaskQueue();
|
2021-07-05 16:20:39 +00:00
|
|
|
* // 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]))
|
2021-06-23 03:08:38 +00:00
|
|
|
* // ;
|
2021-07-05 16:20:39 +00:00
|
|
|
* // 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]))
|
2021-06-23 03:03:44 +00:00
|
|
|
* // ;
|
2021-06-23 03:05:39 +00:00
|
|
|
* // taskQueue.run();
|
2021-06-23 03:03:44 +00:00
|
|
|
*/
|
|
|
|
|
2022-09-22 06:57:14 +00:00
|
|
|
function Task(f, params) {
|
2022-09-22 06:57:30 +00:00
|
|
|
this.f = f;
|
|
|
|
this.params = params;
|
|
|
|
this.nextTask = null;
|
|
|
|
this.failTask = null;
|
|
|
|
this.when = 0;
|
|
|
|
this.delay = 0;
|
|
|
|
this.callCount = 0;
|
|
|
|
|
|
|
|
this.setNextTask = function(task) {
|
|
|
|
this.nextTask = task;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
this.setFailTask = function(task) {
|
|
|
|
this.failTask = task;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
this.setWhen = function(when) {
|
|
|
|
this.when = when;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
this.setDelay = function(delay) {
|
|
|
|
this.delay = delay;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
this.upCallCount = function() {
|
|
|
|
this.callCount++;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
this.resetCount = function() {
|
|
|
|
this.callCount = 0;
|
|
|
|
return this;
|
|
|
|
};
|
2022-09-22 08:02:41 +00:00
|
|
|
}
|
2021-06-22 21:04:11 +00:00
|
|
|
|
2022-09-22 06:57:14 +00:00
|
|
|
function TaskQueue() {
|
2022-09-22 06:57:30 +00:00
|
|
|
this._task = null;
|
|
|
|
this._keepalive = true;
|
|
|
|
this.queue = [];
|
|
|
|
|
|
|
|
this.put = function(task, delay) {
|
|
|
|
var now = new Date();
|
|
|
|
|
|
|
|
try {
|
|
|
|
task.setWhen(now.getTime());
|
|
|
|
if (typeof(delay) !== "undefined") {
|
|
|
|
task.setDelay(delay);
|
|
|
|
}
|
|
|
|
this._task = task;
|
|
|
|
this.queue.push(task);
|
|
|
|
} catch(e) {
|
|
|
|
console.error("TaskQueue.put exception: " + e.message);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
this.get = function() {
|
|
|
|
var task = null;
|
|
|
|
var now = new Date();
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (this.queue.length > 0) {
|
|
|
|
var delta = this.queue.map(function(task) {
|
|
|
|
return task.when + task.delay - now.getTime();
|
|
|
|
});
|
|
|
|
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() {
|
|
|
|
var result = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
var task = this.get();
|
|
|
|
|
|
|
|
if (task != null) {
|
|
|
|
try {
|
|
|
|
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));
|
|
|
|
console.error("task.params: " + typeof(task.params));
|
|
|
|
|
|
|
|
// 태스크 개별 오류 처리
|
|
|
|
if (task.failTask) {
|
|
|
|
this.put(task.failTask, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
console.error("TaskQueue.next: " + e.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
this.then = function(task, delay) {
|
|
|
|
try {
|
|
|
|
if (typeof(delay) !== "undefined") {
|
|
|
|
task.setDelay(delay);
|
|
|
|
}
|
|
|
|
this._task.setNextTask(task);
|
|
|
|
this._task = task;
|
|
|
|
return this;
|
|
|
|
} catch(e) {
|
|
|
|
console.error("TaskQueue.then: " + e.message);
|
|
|
|
console.error(this._task);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.run = function() {
|
|
|
|
this._keepalive = true;
|
|
|
|
while(this._keepalive) {
|
|
|
|
this.next();
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.stop = function() {
|
|
|
|
this._keepalive = false;
|
|
|
|
};
|
2022-09-22 08:02:41 +00:00
|
|
|
}
|
2021-06-22 21:04:11 +00:00
|
|
|
|
2022-09-22 06:57:14 +00:00
|
|
|
function createTaskQueue() {
|
2022-09-22 06:57:30 +00:00
|
|
|
return new TaskQueue();
|
2022-09-22 08:02:41 +00:00
|
|
|
}
|
2021-06-22 21:04:11 +00:00
|
|
|
|
2022-09-22 06:57:14 +00:00
|
|
|
function createTask(f, params, failTask) {
|
2022-09-22 06:57:30 +00:00
|
|
|
try {
|
|
|
|
return (new Task(f, params)).setFailTask(failTask);
|
|
|
|
} catch(e) {
|
|
|
|
console.error("createTask exception: " + e.message);
|
|
|
|
}
|
2022-09-22 08:02:41 +00:00
|
|
|
}
|
2021-06-22 21:04:11 +00:00
|
|
|
|
2022-09-22 08:02:41 +00:00
|
|
|
function putTask(q, task, delay) {
|
2022-09-22 06:57:30 +00:00
|
|
|
try {
|
|
|
|
if (q instanceof TaskQueue && task instanceof Task) {
|
|
|
|
return q.put(task, delay);
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
console.error("putTask exception: " + e.message);
|
|
|
|
}
|
2022-09-22 08:02:41 +00:00
|
|
|
}
|
2021-06-22 21:04:11 +00:00
|
|
|
|
2022-09-22 08:02:41 +00:00
|
|
|
function nextTask(q) {
|
2022-09-22 06:57:30 +00:00
|
|
|
try {
|
|
|
|
return q.next();
|
|
|
|
} catch(e) {
|
|
|
|
console.error("nextTask exception: " + e.message);
|
|
|
|
}
|
2022-09-22 08:02:41 +00:00
|
|
|
}
|
2021-06-23 02:13:32 +00:00
|
|
|
|
2022-09-22 08:02:41 +00:00
|
|
|
function run(q) {
|
2022-09-22 06:57:30 +00:00
|
|
|
q.run();
|
2022-09-22 08:02:41 +00:00
|
|
|
}
|
2021-06-23 02:18:42 +00:00
|
|
|
|
2022-09-22 08:02:41 +00:00
|
|
|
function stop(q) {
|
2022-09-22 06:57:30 +00:00
|
|
|
q.stop();
|
2022-09-22 08:02:41 +00:00
|
|
|
}
|
2021-06-23 05:20:45 +00:00
|
|
|
|
2021-07-28 04:32:25 +00:00
|
|
|
////////////////////////////////////////////////////
|
|
|
|
// START Global functions
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
var __taskQueue__ = new TaskQueue();
|
|
|
|
|
|
|
|
function setTimeout(f, delay) {
|
2022-09-22 06:57:30 +00:00
|
|
|
var params = (arguments.length > 2 ? arguments.slice(2) : []);
|
|
|
|
var task = new Task(f, delay);
|
|
|
|
__taskQueue__.put(task);
|
2021-07-28 04:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: compatiblity with clearInterval()
|
|
|
|
function setInterval(f, delay) {
|
2022-09-22 06:57:30 +00:00
|
|
|
var params = (arguments.length > 2 ? arguments.slice(2) : []);
|
|
|
|
var task = new Task(f, delay);
|
|
|
|
task.setNextTask(task);
|
|
|
|
__taskQueue__.put(task);
|
2021-07-28 04:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.__taskQueue__ = __taskQueue__;
|
|
|
|
exports.setTimeout = setTimeout;
|
|
|
|
exports.setInterval = setInterval;
|
2022-09-22 06:57:14 +00:00
|
|
|
|
2021-07-28 04:32:25 +00:00
|
|
|
////////////////////////////////////////////////////
|
|
|
|
// END Global functions
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
|
2021-07-16 16:27:22 +00:00
|
|
|
exports.Task = Task;
|
|
|
|
exports.TaskQueue = TaskQueue;
|
|
|
|
|
2022-09-22 06:57:14 +00:00
|
|
|
exports.createTaskQueue = createTaskQueue;
|
|
|
|
exports.createTask = createTask;
|
|
|
|
exports.putTask = putTask;
|
|
|
|
exports.nextTask = nextTask;
|
|
|
|
exports.run = run;
|
|
|
|
exports.stop = stop;
|
|
|
|
|
2022-09-22 08:02:41 +00:00
|
|
|
exports.VERSIONINFO = "Task Module (task.js) version 0.2.2";
|
2021-06-24 18:43:54 +00:00
|
|
|
exports.global = global;
|
|
|
|
exports.require = require;
|