Skip to content

Commit db9f98a

Browse files
committed
Merge pull request hashicorp#947 from hashicorp/fix-chainable-timer
Attach Chainable timers to Engine render loop
2 parents 6f5234c + 61536cb commit db9f98a

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

website/source/assets/javascripts/app/Engine.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ Engine = Base.extend({
6868
this.background.className += ' show';
6969
this.canvas.style.opacity = 1;
7070

71-
new Chainable()
71+
// We have to pass the engine into Chainable to
72+
// enable the timers to properly attach to the
73+
// run/render loop
74+
new Chainable(this)
7275
.wait(1000)
7376
.then(function(){
7477
this.starGeneratorRate = 200;
@@ -202,6 +205,13 @@ Engine = Base.extend({
202205
this.now = Date.now() / 1000;
203206
this.tick = Math.min(this.now - this.last, 0.017);
204207

208+
// We must attach the chainable timer to the engine
209+
// run/render loop or else things can get pretty
210+
// out of wack
211+
if (this.updateChainTimer) {
212+
this.updateChainTimer(this.tick);
213+
}
214+
205215
// Update all particles... may need to be optimized
206216
for (p = 0; p < this.particles.length; p++) {
207217
this.particles[p].update(this);

website/source/assets/javascripts/lib/Chainable.js

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
(function(){
22

3-
var Chainable = function(){
3+
var Chainable = function(engine){
4+
this.engine = engine;
45
this._chain = [];
6+
this._updateTimer = this._updateTimer.bind(this);
57
this._cycle = this._cycle.bind(this);
68
};
79

810
Chainable.prototype._running = false;
911

12+
Chainable.prototype._updateTimer = function(tick){
13+
this._timer += tick;
14+
if (this._timer >= this._timerMax) {
15+
this.resetTimer();
16+
this._cycle();
17+
}
18+
};
19+
20+
Chainable.prototype.resetTimer = function(){
21+
this.engine.updateChainTimer = undefined;
22+
this._timer = 0;
23+
this._timerMax = 0;
24+
return this;
25+
};
26+
1027
Chainable.prototype.start = function(){
1128
if (this._running || !this._chain.length) {
1229
return this;
@@ -19,9 +36,8 @@ Chainable.prototype.reset = function(){
1936
if (!this._running) {
2037
return this;
2138
}
22-
clearTimeout(this._timer);
23-
this._timer = null;
24-
this._chain.length = 0;
39+
this.resetTimer();
40+
this._timer = 0;
2541
this._running = false;
2642
return this;
2743
};
@@ -40,8 +56,10 @@ Chainable.prototype._cycle = function(){
4056
return this._cycle();
4157
}
4258
if (current.type === 'wait') {
43-
clearTimeout(this._timer);
44-
this._timer = setTimeout(this._cycle, current.time || 0);
59+
this.resetTimer();
60+
// Convert timer to seconds
61+
this._timerMax = current.time / 1000;
62+
this.engine.updateChainTimer = this._updateTimer;
4563
current = null;
4664
}
4765

0 commit comments

Comments
 (0)