Update task.js

This commit is contained in:
Namhyeon Go 2022-09-22 17:02:41 +09:00 committed by GitHub
parent 7fa00324ee
commit e4cce764b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,5 @@
////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////
// Task API (Time-sharing based `async`, `setTimeout`, `setInterval`, `Promise` implementation in WSH.js) // Task API (Time-sharing based `async`, `setTimeout`, `setInterval`, implementation in WSH.js)
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
/* // example: /* // example:
@ -49,7 +49,7 @@ function Task(f, params) {
this.callCount = 0; this.callCount = 0;
return this; return this;
}; };
}; }
function TaskQueue() { function TaskQueue() {
this._task = null; this._task = null;
@ -146,11 +146,11 @@ function TaskQueue() {
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 {
@ -158,9 +158,9 @@ function createTask(f, params, 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(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);
@ -168,23 +168,23 @@ function putTask = function(q, task, delay) {
} catch(e) { } catch(e) {
console.error("putTask exception: " + e.message); console.error("putTask exception: " + e.message);
} }
}; }
function nextTask = function(q) { function nextTask(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(q) {
q.run(); q.run();
}; }
function stop = function(q) { function stop(q) {
q.stop(); q.stop();
}; }
//////////////////////////////////////////////////// ////////////////////////////////////////////////////
// START Global functions // START Global functions
@ -205,36 +205,9 @@ function setInterval(f, delay) {
__taskQueue__.put(task); __taskQueue__.put(task);
} }
function Promise(executor) {
this.length = 1;
this.executor = executor;
this.all = function(iterable) {
// todo
};
this.race = function(iterable) {
// todo
};
this.reject = function(reason) {
// todo
};
this.resolve = function(reason) {
// todo
};
this.then = function(successCallback, failureCallback) {
// todo
};
this.catch = function(failureCallback) {
// todo
};
this.executor(this.resolve, this.reject);
}
exports.__taskQueue__ = __taskQueue__; exports.__taskQueue__ = __taskQueue__;
exports.setTimeout = setTimeout; exports.setTimeout = setTimeout;
exports.setInterval = setInterval; exports.setInterval = setInterval;
exports.Promise = Promise;
//////////////////////////////////////////////////// ////////////////////////////////////////////////////
// END Global functions // END Global functions
@ -250,6 +223,6 @@ exports.nextTask = nextTask;
exports.run = run; exports.run = run;
exports.stop = stop; exports.stop = stop;
exports.VERSIONINFO = "Task Module (task.js) version 0.2.1"; exports.VERSIONINFO = "Task Module (task.js) version 0.2.2";
exports.global = global; exports.global = global;
exports.require = require; exports.require = require;