-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathrenderloop.js
53 lines (44 loc) · 1.21 KB
/
renderloop.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
"use strict";
var event = require("./lib/event");
/**
* Batches changes (that force something to be redrawn) in the background.
**/
class RenderLoop {
constructor(onRender, win) {
this.onRender = onRender;
this.pending = false;
this.changes = 0;
this.$recursionLimit = 2;
this.window = win || window;
var _self = this;
this._flush = function (ts) {
_self.pending = false;
var changes = _self.changes;
if (changes) {
event.blockIdle(100);
_self.changes = 0;
_self.onRender(changes);
}
if (_self.changes) {
if (_self.$recursionLimit-- < 0) return;
_self.schedule();
}
else {
_self.$recursionLimit = 2;
}
};
}
schedule(change) {
this.changes = this.changes | change;
if (this.changes && !this.pending) {
event.nextFrame(this._flush);
this.pending = true;
}
}
clear(change) {
var changes = this.changes;
this.changes = 0;
return changes;
}
}
exports.RenderLoop = RenderLoop;