welsonjs/lib/fakeworker.js

174 lines
5.0 KiB
JavaScript
Raw Normal View History

2022-10-27 15:49:43 +00:00
var BASE64 = require("lib/base64");
function FakeWorker() {
this.__lastIntervalID__ = 0;
this.__lastTimeoutID__ = 0;
this.__intervals__ = [];
this.__timeouts__ = [];
this.__isWindow__ = (typeof window !== "undefined");
this.atob = function(encodedData) {
if (!this.__isWindow__) {
return BASE64.decode(encodedData);
} else {
return window.atob.apply(null, arguments);
}
};
2022-10-27 17:12:33 +00:00
this.btoa = function(stringToEncode) {
2022-10-27 15:49:43 +00:00
if (!this.__isWindow__) {
return BASE64.encode(stringToEncode);
} else {
return window.btoa.apply(null, arguments);
}
};
this.clearInterval = function(intervalID) {
2022-10-27 16:31:20 +00:00
for (var i = 0; i < this.__intervals__.length; i++) {
if (this.__intervals__[i].id == intervalID) {
this.__intervals__[i].cleared = true;
break;
2022-10-27 15:49:43 +00:00
}
}
};
this.clearTimeout = function(timeoutID) {
2022-10-27 16:31:20 +00:00
for (var i = 0; i < this.__timeouts__.length; i++) {
if (this.__timeouts__[i].id == timeoutID) {
this.__timeouts__[i].cleared = true;
2022-10-27 15:49:43 +00:00
}
}
};
this.setInterval = function(code, delay) {
2022-10-27 16:31:20 +00:00
this.__lastIntervalID__++;
this.__intervals__.push({
'id': this.__lastIntervalID__,
'code': code,
'delay': delay,
'arguments': (arguments.length > 2 ? arguments.slice(2) : []),
'timestamp': Date.now(),
'cleared': false,
'callback': null
});
return this.__lastIntervalID__;
2022-10-27 15:49:43 +00:00
};
this.setTimeout = function(code, delay) {
2022-10-27 16:31:20 +00:00
this.__lastTimeoutID__++;
this.__timeouts__.push({
'id': this.__lastTimeoutID__,
'code': code,
'delay': delay,
'arguments': (arguments.length > 2 ? arguments.slice(2) : []),
'timestamp': Date.now(),
'cleared': false
});
return this.__lastTimeoutID__;
2022-10-27 15:49:43 +00:00
};
2022-10-27 16:22:44 +00:00
2022-10-27 16:31:20 +00:00
this.setIntervalWithCallback = function(code, callback) {
2022-10-27 16:29:25 +00:00
this.__lastIntervalID__++;
this.__intervals__.push({
'id': this.__lastIntervalID__,
'code': code,
2022-11-14 08:06:25 +00:00
'delay': 0, // replace to the callback
2022-10-27 16:29:25 +00:00
'arguments': [],
'timestamp': Date.now(),
'cleared': false,
'callback': callback
});
return this.__lastIntervalID__;
};
2022-11-25 17:10:59 +00:00
2022-10-27 16:29:25 +00:00
2022-11-25 17:10:59 +00:00
this.getIntervals = function(cur) {
2022-10-27 16:22:44 +00:00
var intervals = [];
for (var i = 0; i < this.__intervals__.length; i++) {
2022-11-25 17:10:59 +00:00
var work = this.__intervals__[i];
if (!work.cleared && (work.timestamp + work.delay) <= cur) {
//console.debug("timestamp:", work.timestamp);
//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]);
2022-10-27 16:22:44 +00:00
}
}
return intervals;
};
2022-11-25 17:10:59 +00:00
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) {
2022-10-27 16:22:44 +00:00
var timeouts = [];
2022-10-27 16:31:20 +00:00
2022-10-27 16:22:44 +00:00
for (var i = 0; i < this.__timeouts__.length; i++) {
2022-11-25 17:10:59 +00:00
var work = this.__intervals__[i];
if (!work.cleared && (work.timestamp + work.delay) <= cur) {
timeouts.push([i, work]);
work.cleared = true;
this.__timeouts__[i] = work;
2022-10-27 16:22:44 +00:00
}
}
2022-11-25 17:10:59 +00:00
2022-10-27 16:22:44 +00:00
return timeouts;
};
2022-10-27 15:49:43 +00:00
}
exports.create = function() {
return new FakeWorker();
};
2022-10-27 17:45:13 +00:00
exports.repeat = function(target, worker, onError) {
2022-10-27 17:12:33 +00:00
if (!(worker instanceof FakeWorker)) return;
if (["number", "boolean"].indexOf(typeof target) > -1) {
var ms = target;
var cur = Date.now();
var end = cur + ms;
while (ms === true ? true : (cur < end)) {
2022-11-25 17:10:59 +00:00
var intervals = worker.getIntervals(cur);
2022-10-27 17:12:33 +00:00
for (var i = 0; i < intervals.length; i++) {
2022-11-25 17:10:59 +00:00
var k = intervals[i][0];
var work = intervals[i][1];
2022-10-27 17:12:33 +00:00
try {
2022-11-25 17:10:59 +00:00
if (typeof work.code === "function") {
work.code(i);
worker.update(k, Date.now());
}
2022-10-27 17:12:33 +00:00
} catch (e) {
2022-11-25 17:10:59 +00:00
if (typeof onError === "function") {
onError(e, i);
}
2022-10-27 17:12:33 +00:00
}
};
2022-11-25 17:10:59 +00:00
cur = Date.now();
2022-10-27 17:12:33 +00:00
}
2022-11-14 08:06:25 +00:00
end = cur;
2022-10-27 17:12:33 +00:00
}
return end;
};
2022-11-25 17:10:59 +00:00
exports.VERSIONINFO = "FakeWorker module (fakeworker.js) version 0.0.6";
2022-11-25 14:11:37 +00:00
exports.AUTHOR = "abuse@catswords.net";
2022-10-27 15:49:43 +00:00
exports.global = global;
exports.require = global.require;