forked from tmikov/hermes-jsi-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjslib.js.inc
60 lines (52 loc) · 1.55 KB
/
jslib.js.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
R"((function() {
"use strict";
var tasks = [];
var nextTaskID = 0;
var curTime = 0;
// Return the deadline of the next task, or -1 if there is no task.
function peekMacroTask() {
return tasks.length ? tasks[0].deadline : -1;
}
// Run the next task if it's time.
// `tm` is the current time.
function runMacroTask(tm) {
curTime = tm;
if (tasks.length && tasks[0].deadline <= tm) {
var task = tasks.shift();
task.fn.apply(undefined, task.args);
}
}
function setTimeout(fn, ms = 0, ...args) {
var id = nextTaskID++;
var task = {id, fn, deadline: curTime + Math.max(0, ms | 0), args};
// Insert the task in the sorted list.
var i = 0;
for (i = 0; i < tasks.length; ++i) {
if (tasks[i].deadline > task.deadline) {
break;
}
}
tasks.splice(i, 0, task);
return id;
}
function clearTimeout(id) {
for (var i = 0; i < tasks.length; i++) {
if (tasks[i].id === id) {
tasks.splice(i, 1);
break;
}
}
}
function setImmediate(fn, ...args) {
return setTimeout(fn, 0, ...args);
}
function clearImmediate(id) {
return clearTimeout(id);
}
globalThis.setTimeout = setTimeout;
globalThis.clearTimeout = clearTimeout;
globalThis.setImmediate = setImmediate;
globalThis.clearImmediate = clearImmediate;
return {peek: peekMacroTask, run: runMacroTask};
})();
)"