-
Notifications
You must be signed in to change notification settings - Fork 22
/
faketime.js
280 lines (240 loc) · 7.54 KB
/
faketime.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// API for controlling time in JavaScript.
// Makes it possible to run most web applications faster
// or slower than real time.
(function() {
// Frame rate when time is running "normally".
var FRAME_RATE = 30;
function FakeTime() {
this.animationFrameRate = FRAME_RATE;
// Used to track virtual time.
this._startTime = new Date().getTime();
this._currentTime = this._startTime;
// Queue of pending virtual timeouts.
// Each object has keys: 'deadline', 'func', and 'id'.
this._timers = [];
// Used to pass time normally when time is not
// being manipulated.
this._realtimeTimeout = null;
// Backups of overridden window functions.
this._backups = {};
this._install();
this.play();
}
// Play the application at normal speed.
FakeTime.prototype.play = function(speed) {
speed = speed || 1;
if (this._realtimeTimeout !== null) {
// Allow re-playing at a different speed.
this.pause();
}
var lastTime = this._realTime();
var backup = this._backups.setTimeout.bind(window);
var timePerTick = 1000 / FRAME_RATE;
var tickFunc = function() {
this.advance(speed * timePerTick);
var newTime = this._realTime();
var elapsed = newTime - lastTime;
var timeoutTime = Math.max(1, timePerTick-elapsed);
this._realtimeTimeout = backup.call(window, tickFunc, timeoutTime);
lastTime = newTime;
}.bind(this);
tickFunc();
};
// Stop the application from running at normal speed.
// Time will only advance if advance() is called.
FakeTime.prototype.pause = function() {
if (this._realtimeTimeout !== null) {
this._backups.clearTimeout.call(window, this._realtimeTimeout);
this._realtimeTimeout = null;
}
};
// Advance the current timestamp.
FakeTime.prototype.advance = function(millis) {
this._triggerTimers();
while (true) {
var nextTimer = this._nextTimer();
if (nextTimer === null) {
break;
}
var advance = 1 + (nextTimer.deadline - this._currentTime);
if (advance > millis) {
break;
}
millis -= advance;
this._currentTime += advance;
this._triggerTimers();
}
this._currentTime += millis;
}
FakeTime.prototype._triggerTimers = function() {
for (var i = 0; i < this._timers.length; ++i) {
var timer = this._timers[i];
// Intentionally avoid using >= so that doing
//
// var f;
// f = () => setTimeout(f, 0);
//
// doesn't cause an infinite loop.
if (this._currentTime > timer.deadline) {
this._timers.splice(i, 1);
try {
timer.func();
} catch (e) {
console.log('exception in timer', e);
}
--i;
}
}
};
FakeTime.prototype._nextTimer = function() {
var firstDeadline = -1;
var firstTimer = null;
for (var i = 0; i < this._timers.length; ++i) {
var timer = this._timers[i];
if (timer.deadline < firstDeadline || firstTimer === null) {
firstTimer = timer;
firstDeadline = timer.deadline;
}
}
return firstTimer;
};
FakeTime.prototype._install = function() {
this._backups['performance_now'] = performance.now;
var overriddenProps = [
'Date', 'setTimeout', 'setInterval',
'clearTimeout', 'clearInterval',
'requestAnimationFrame',
'cancelAnimationFrame'
];
overriddenProps.forEach(function(prop) {
this._backups[prop] = window[prop];
}.bind(this));
this._installTimeout();
this._installInterval();
this._installDate();
this._installPerformance();
this._installAnimationFrame();
};
FakeTime.prototype._installTimeout = function() {
var timeoutID = 0;
window.setTimeout = function(func, millis) {
if ('string' === typeof func) {
func = new Function(func);
}
millis = millis || 1;
var extraArgs = [];
for (var i = 2; i < arguments.length; ++i) {
extraArgs.push(arguments[i]);
}
this._timers.push({
func: function() {
func.apply(window, extraArgs);
},
id: ++timeoutID,
deadline: this._currentTime + millis
});
return timeoutID;
}.bind(this);
window.clearTimeout = function(id) {
for (var i = 0, len = this._timers.length; i < len; ++i) {
var timer = this._timers[i];
if (timer.id === id) {
this._timers.splice(i, 1);
break;
}
}
}.bind(this);
}
FakeTime.prototype._installInterval = function() {
var intervalID = 0;
var timeouts = {};
window.setInterval = function() {
var id = ++intervalID;
var timeoutArgs = [];
for (var i = 0; i < arguments.length; ++i) {
timeoutArgs.push(arguments[i]);
}
if ('string' === typeof timeoutArgs[0]) {
timeoutArgs[0] = new Function(timeoutArgs[0]);
}
var tickHandler = timeoutArgs[0];
timeoutArgs[0] = function() {
try {
tickHandler.apply(this, arguments);
} catch (e) {
console.log('exception in interval', e);
}
// Deal with case where clearInterval is called
// from within the handler.
if (timeouts.hasOwnProperty(id)) {
timeouts[id] = setTimeout.apply(window, timeoutArgs);
}
};
timeouts[id] = setTimeout.apply(window, timeoutArgs);
return id;
};
window.clearInterval = function(id) {
if (timeouts.hasOwnProperty(id)) {
clearTimeout(timeouts[id]);
delete timeouts[id];
}
}
};
FakeTime.prototype._installDate = function() {
var faketime = this;
var RealDate = this._backups.Date;
function VirtualDate() {
if (arguments.length === 0) {
this._date = new RealDate(Math.round(faketime._currentTime));
} else {
var bindArgs = [RealDate];
for (var i = 0; i < arguments.length; ++i) {
bindArgs.push(arguments[i]);
}
var bound = RealDate.bind.apply(RealDate, bindArgs);
this._date = new bound();
}
}
VirtualDate.now = function() {
return new VirtualDate().getTime();
};
VirtualDate.UTC = function() {
var res = Object.create(VirtualDate);
res._date = RealDate.UTC.apply(RealDate, arguments);
return res;
};
VirtualDate.length = RealDate.length;
var protoNames = Object.getOwnPropertyNames(RealDate.prototype);
protoNames.forEach(function(name) {
VirtualDate.prototype[name] = function() {
return this._date[name].apply(this._date, arguments);
};
});
VirtualDate.prototype.constructor = window.Date;
window.Date = VirtualDate;
};
FakeTime.prototype._installPerformance = function() {
window.performance.now = function() {
return this._currentTime;
}.bind(this);
};
FakeTime.prototype._installAnimationFrame = function() {
// TODO: synchronize all animation frame callbacks to
// have the same timestamp.
// This will require some kind of queueing mechanism.
window.requestAnimationFrame = function(callback) {
return setTimeout(function() {
// Yup, for some reason `this` is the callback.
callback.call(callback, performance.now());
}, 1000/this.animationFrameRate);
}.bind(this);
window.webkitRequestAnimationFrame = window.requestAnimationFrame;
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
}
};
FakeTime.prototype._realTime = function() {
return new this._backups.Date().getTime();
}
window.faketime = new FakeTime();
})();