Update task.js

This commit is contained in:
Namhyeon Go 2022-09-22 15:57:30 +09:00 committed by GitHub
parent 63a0f3c492
commit 7fa00324ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,173 +17,173 @@
*/ */
function Task(f, params) { function Task(f, params) {
this.f = f; this.f = f;
this.params = params; this.params = params;
this.nextTask = null; this.nextTask = null;
this.failTask = null; this.failTask = null;
this.when = 0; this.when = 0;
this.delay = 0; this.delay = 0;
this.callCount = 0; this.callCount = 0;
this.setNextTask = function(task) { this.setNextTask = function(task) {
this.nextTask = task; this.nextTask = task;
return this; return this;
}; };
this.setFailTask = function(task) { this.setFailTask = function(task) {
this.failTask = task; this.failTask = task;
return this; return this;
}; };
this.setWhen = function(when) { this.setWhen = function(when) {
this.when = when; this.when = when;
return this; return this;
}; };
this.setDelay = function(delay) { this.setDelay = function(delay) {
this.delay = delay; this.delay = delay;
return this; return this;
}; };
this.upCallCount = function() { this.upCallCount = function() {
this.callCount++; this.callCount++;
return this; return this;
}; };
this.resetCount = function() { this.resetCount = function() {
this.callCount = 0; this.callCount = 0;
return this; return this;
}; };
}; };
function TaskQueue() { function TaskQueue() {
this._task = null; this._task = null;
this._keepalive = true; this._keepalive = true;
this.queue = []; this.queue = [];
this.put = function(task, delay) { this.put = function(task, delay) {
var now = new Date(); var now = new Date();
try { try {
task.setWhen(now.getTime()); task.setWhen(now.getTime());
if (typeof(delay) !== "undefined") { if (typeof(delay) !== "undefined") {
task.setDelay(delay); task.setDelay(delay);
} }
this._task = task; this._task = task;
this.queue.push(task); this.queue.push(task);
} catch(e) { } catch(e) {
console.error("TaskQueue.put exception: " + e.message); console.error("TaskQueue.put exception: " + e.message);
} }
return this; return this;
}; };
this.get = function() { this.get = function() {
var task = null; var task = null;
var now = new Date(); var now = new Date();
try { try {
if (this.queue.length > 0) { if (this.queue.length > 0) {
var delta = this.queue.map(function(task) { var delta = this.queue.map(function(task) {
return task.when + task.delay - now.getTime(); return task.when + task.delay - now.getTime();
}); });
var i = delta.indexOf(Math.min.apply(Math, delta)); var i = delta.indexOf(Math.min.apply(Math, delta));
var d = this.queue[i].when + this.queue[i].delay - now.getTime(); var d = this.queue[i].when + this.queue[i].delay - now.getTime();
if (i > -1 && d <= 0) { if (i > -1 && d <= 0) {
console.log("Task", i, d); console.log("Task", i, d);
task = this.queue.splice(i, 1)[0]; task = this.queue.splice(i, 1)[0];
} }
} }
} catch(e) { } catch(e) {
console.error("TaskQueue.get: " + e.message); console.error("TaskQueue.get: " + e.message);
} }
return task; return task;
}; };
this.next = function() { this.next = function() {
var result = null; var result = null;
try { try {
var task = this.get(); var task = this.get();
if (task != null) { if (task != null) {
try { try {
result = task.f.apply(Task, [task].concat(task.params)); result = task.f.apply(Task, [task].concat(task.params));
if (task.nextTask != null) { if (task.nextTask != null) {
this.put(task.nextTask, task.nextTask.delay); this.put(task.nextTask, task.nextTask.delay);
} }
task.upCallCount(); // 호출 횟수 기록 task.upCallCount(); // 호출 횟수 기록
} catch (e) { } catch (e) {
console.error("Task exception: " + e.message); console.error("Task exception: " + e.message);
console.error("task.f: " + typeof(task.f)); console.error("task.f: " + typeof(task.f));
console.error("task.params: " + typeof(task.params)); console.error("task.params: " + typeof(task.params));
// 태스크 개별 오류 처리 // 태스크 개별 오류 처리
if (task.failTask) { if (task.failTask) {
this.put(task.failTask, 0); this.put(task.failTask, 0);
} }
} }
} }
} catch(e) { } catch(e) {
console.error("TaskQueue.next: " + e.message); console.error("TaskQueue.next: " + e.message);
} }
return result; return result;
}; };
this.then = function(task, delay) { this.then = function(task, delay) {
try { try {
if (typeof(delay) !== "undefined") { if (typeof(delay) !== "undefined") {
task.setDelay(delay); task.setDelay(delay);
} }
this._task.setNextTask(task); this._task.setNextTask(task);
this._task = task; this._task = task;
return this; return this;
} catch(e) { } catch(e) {
console.error("TaskQueue.then: " + e.message); console.error("TaskQueue.then: " + e.message);
console.error(this._task); console.error(this._task);
} }
}; };
this.run = function() { this.run = function() {
this._keepalive = true; this._keepalive = true;
while(this._keepalive) { while(this._keepalive) {
this.next(); this.next();
sleep(1); sleep(1);
} }
}; };
this.stop = function() { this.stop = function() {
this._keepalive = false; this._keepalive = false;
}; };
}; };
function createTaskQueue() { function createTaskQueue() {
return new TaskQueue(); return new TaskQueue();
}; };
function createTask(f, params, failTask) { function createTask(f, params, failTask) {
try { try {
return (new Task(f, params)).setFailTask(failTask); return (new Task(f, params)).setFailTask(failTask);
} catch(e) { } catch(e) {
console.error("createTask exception: " + e.message); console.error("createTask exception: " + e.message);
} }
}; };
function putTask = function(q, task, delay) { function putTask = function(q, task, delay) {
try { try {
if (q instanceof TaskQueue && task instanceof Task) { if (q instanceof TaskQueue && task instanceof Task) {
return q.put(task, delay); return q.put(task, delay);
} }
} catch(e) { } catch(e) {
console.error("putTask exception: " + e.message); console.error("putTask exception: " + e.message);
} }
}; };
function nextTask = function(q) { function nextTask = function(q) {
try { try {
return q.next(); return q.next();
} catch(e) { } catch(e) {
console.error("nextTask exception: " + e.message); console.error("nextTask exception: " + e.message);
} }
}; };
function run = function(q) { function run = function(q) {
q.run(); q.run();
}; };
function stop = function(q) { function stop = function(q) {
q.stop(); q.stop();
}; };
//////////////////////////////////////////////////// ////////////////////////////////////////////////////
@ -192,43 +192,43 @@ function stop = function(q) {
var __taskQueue__ = new TaskQueue(); var __taskQueue__ = new TaskQueue();
function setTimeout(f, delay) { function setTimeout(f, delay) {
var params = (arguments.length > 2 ? arguments.slice(2) : []); var params = (arguments.length > 2 ? arguments.slice(2) : []);
var task = new Task(f, delay); var task = new Task(f, delay);
__taskQueue__.put(task); __taskQueue__.put(task);
} }
// TODO: compatiblity with clearInterval() // TODO: compatiblity with clearInterval()
function setInterval(f, delay) { function setInterval(f, delay) {
var params = (arguments.length > 2 ? arguments.slice(2) : []); var params = (arguments.length > 2 ? arguments.slice(2) : []);
var task = new Task(f, delay); var task = new Task(f, delay);
task.setNextTask(task); task.setNextTask(task);
__taskQueue__.put(task); __taskQueue__.put(task);
} }
function Promise(executor) { function Promise(executor) {
this.length = 1; this.length = 1;
this.executor = executor; this.executor = executor;
this.all = function(iterable) { this.all = function(iterable) {
// todo // todo
}; };
this.race = function(iterable) { this.race = function(iterable) {
// todo // todo
}; };
this.reject = function(reason) { this.reject = function(reason) {
// todo // todo
}; };
this.resolve = function(reason) { this.resolve = function(reason) {
// todo // todo
}; };
this.then = function(successCallback, failureCallback) { this.then = function(successCallback, failureCallback) {
// todo // todo
}; };
this.catch = function(failureCallback) { this.catch = function(failureCallback) {
// todo // todo
}; };
this.executor(this.resolve, this.reject); this.executor(this.resolve, this.reject);
} }
exports.__taskQueue__ = __taskQueue__; exports.__taskQueue__ = __taskQueue__;