Update fakeworker.js

This commit is contained in:
Namhyeon Go 2022-11-26 02:10:59 +09:00
parent 37b8404f0b
commit 7caeed23ed

View File

@ -80,37 +80,51 @@ function FakeWorker() {
}); });
return this.__lastIntervalID__; return this.__lastIntervalID__;
}; };
this.getIntervals = function() { this.getIntervals = function(cur) {
var intervals = []; var intervals = [];
var cur = Date.now();
for (var i = 0; i < this.__intervals__.length; i++) { for (var i = 0; i < this.__intervals__.length; i++) {
if (!this.__intervals__[i].cleared && (this.__intervals__[i].timestamp + this.__intervals__[i].delay) >= cur) { var work = this.__intervals__[i];
intervals.push(this.__intervals__[i]);
this.__intervals__[i].timestamp = cur; if (!work.cleared && (work.timestamp + work.delay) <= cur) {
if (typeof this.__intervals__[i].callback === "function") { //console.debug("timestamp:", work.timestamp);
this.__intervals__[i].delay = this.__intervals__[i].callback(); //console.debug("delay:", work.delay);
} //console.debug("timestamp+delay:", work.timestamp + work.delay);
//console.debug("cur:", cur);
//console.debug("cur-(timestamp+delay):", cur - (work.timestamp + work.delay));
intervals.push([i, work]);
} }
cur = Date.now();
} }
return intervals; return intervals;
}; };
this.getTimeouts = function() { this.update = function(i, timestamp) {
var work = this.__intervals__[i];
work.timestamp = timestamp;
if (typeof work.callback === "function") {
work.delay = work.callback();
}
this.__intervals__[i] = work;
};
this.getTimeouts = function(cur) {
var timeouts = []; var timeouts = [];
var cur = Date.now();
for (var i = 0; i < this.__timeouts__.length; i++) { for (var i = 0; i < this.__timeouts__.length; i++) {
if (!this.__timeouts__[i].cleared && (this.__timeouts__[i].timestamp + this.__timeouts__[i].delay) >= cur) { var work = this.__intervals__[i];
timeouts.push(this.__timeouts__[i]);
this.__timeouts__[i].cleared = true; if (!work.cleared && (work.timestamp + work.delay) <= cur) {
timeouts.push([i, work]);
work.cleared = true;
this.__timeouts__[i] = work;
} }
cur = Date.now();
} }
return timeouts; return timeouts;
}; };
} }
@ -128,15 +142,23 @@ exports.repeat = function(target, worker, onError) {
var end = cur + ms; var end = cur + ms;
while (ms === true ? true : (cur < end)) { while (ms === true ? true : (cur < end)) {
var intervals = worker.getIntervals(); var intervals = worker.getIntervals(cur);
for (var i = 0; i < intervals.length; i++) { for (var i = 0; i < intervals.length; i++) {
var k = intervals[i][0];
var work = intervals[i][1];
try { try {
if (typeof intervals[i].code === "function") intervals[i].code(i); if (typeof work.code === "function") {
work.code(i);
worker.update(k, Date.now());
}
} catch (e) { } catch (e) {
if (typeof onError === "function") onError(e, i); if (typeof onError === "function") {
onError(e, i);
}
} }
cur = Date.now();
}; };
cur = Date.now();
} }
end = cur; end = cur;
@ -145,7 +167,7 @@ exports.repeat = function(target, worker, onError) {
return end; return end;
}; };
exports.VERSIONINFO = "FakeWorker module (fakeworker.js) version 0.0.4"; exports.VERSIONINFO = "FakeWorker module (fakeworker.js) version 0.0.6";
exports.AUTHOR = "abuse@catswords.net"; exports.AUTHOR = "abuse@catswords.net";
exports.global = global; exports.global = global;
exports.require = global.require; exports.require = global.require;