/
timer.js
207 lines (164 loc) · 4.74 KB
/
timer.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Timers
(function(global) {
let gameTime = Citizen.getTickCount();
const timers = {};
let timerId = 0;
const tickers = {};
let tickerId = 0;
let animationFrames = [];
function printError(where, e) {
console.error(`Unhandled error in ${where}: ${e.toString()}\n${e.stack}`);
}
function setTimer(timer, callback, interval) {
timers[timer.id] = {
callback,
interval,
lastRun: gameTime
};
}
function nextTimerId() {
return {
id: ++timerId,
unref() {},
ref() {}
};
}
function nextTickerId() {
return ++tickerId;
}
function setTick(callback) {
const id = nextTickerId();
tickers[id] = {
callback,
promise: null,
};
return id;
}
function clearTick(ticker) {
if (!ticker) {
return;
}
delete tickers[ticker];
}
function resolveTicker(ticker) {
if (!tickers[ticker]) {
return;
}
tickers[ticker].promise = null;
}
function printTickerError(e) {
printError('ticker', e);
}
function clearTimer(timer) {
if (!timer) {
return;
}
delete timers[timer.id];
}
function requestAnimationFrame(callback) {
animationFrames[animationFrames.length] = callback;
}
function setInterval(callback, interval) {
const id = nextTimerId();
setTimer(id, callback, interval);
return id;
}
function setTimeout(callback, timeout, ...argsForCallback) {
const id = nextTimerId();
setTimer(
id,
function() {
try {
callback(...argsForCallback);
} finally {
clearTimer(id);
}
},
timeout
);
return id;
}
function setImmediate(callback, ...argsForCallback) {
return setTimeout(callback, 0, ...argsForCallback);
}
function onTick() {
const localGameTime = Citizen.getTickCount(); // ms
let i;
// Process timers
for (const timerId in timers) {
const timer = timers[timerId];
if ((localGameTime - timer.lastRun) > timer.interval) {
try {
timer.callback();
} catch (e) {
printError('timer', e);
}
timer.lastRun = localGameTime;
}
}
// Process tickers
for (const tickerId in tickers) {
const ticker = tickers[tickerId];
// If last call of ticker returned a promise,
// then we should wait for it
if (ticker.promise !== null) {
continue;
}
let result;
try {
result = ticker.callback();
} catch (e) {
printTickerError(e);
continue;
}
// We've got a promise!
if (typeof result.then === 'function') {
ticker.promise = result
.then(resolveTicker.bind(null, tickerId))
.catch(printTickerError);
}
}
// Process animation frames
if (animationFrames.length !== 0) {
const currentAnimationFrames = animationFrames;
animationFrames = [];
i = currentAnimationFrames.length;
while (i--) {
try {
currentAnimationFrames[i]();
} catch (e) {
printError('animationFrame', e);
}
}
}
gameTime = localGameTime;
// Manually fire callbacks that were enqueued by process.nextTick.
// Since we override setImmediate/etc, this doesn't happen automatically.
if (global.process && typeof global.process._tickCallback === 'function') {
global.process._tickCallback();
}
}
const defineGlobals = (globals) => {
Object.defineProperties(global, Object.keys(globals).reduce((acc, name) => {
acc[name] = {
value: globals[name],
writable: false,
enumerable: true,
configurable: false,
};
return acc;
}, {}));
};
defineGlobals({
setTick,
clearTick,
setTimeout,
clearTimeout: clearTimer,
setInterval,
clearInterval: clearTimer,
setImmediate,
clearImmediate: clearTimer,
requestAnimationFrame,
});
global.Citizen.setTickFunction(onTick);
})(this || window);