Skip to content

Commit d5b70f9

Browse files
committed
Fixes/cleanup to pauseRafOnIdle and onIdle
1 parent 0485dff commit d5b70f9

File tree

3 files changed

+35
-42
lines changed

3 files changed

+35
-42
lines changed

src/platforms/browser/WebPlatform.mjs

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ export default class WebPlatform {
3333
// Alternative handler to avoid RAF when idle
3434
this._loopHandler = null;
3535
this._idleLoopCounter = 0;
36-
this._idleLoopDelay = 60;
37-
this._onIdle = false;
36+
this._idleLoopDelay = 30;
3837

3938
if (this.stage.getOption("useImageWorker")) {
4039
if (!window.createImageBitmap || !window.Worker) {
@@ -52,6 +51,7 @@ export default class WebPlatform {
5251
this._imageWorker.destroy();
5352
}
5453

54+
this._looping = false;
5555
clearInterval(this._loopHandler);
5656

5757
this._removeKeyHandler();
@@ -75,44 +75,41 @@ export default class WebPlatform {
7575
this._looping = false;
7676
}
7777

78-
switchLoop() {
79-
if (this._onIdle === false) {
80-
this._onIdle = true;
81-
this.stage.onIdle();
82-
}
83-
84-
if (this._idleLoopCounter < this._idleLoopDelay) {
85-
this._idleLoopCounter++;
86-
return;
87-
}
88-
if (!this.stage.ctx.hasRenderUpdates()) {
89-
this.stopLoop();
90-
this._loopHandler = setInterval(() => {
91-
this.stage.updateFrame();
92-
this.stage.idleFrame();
93-
if (this.stage.ctx.hasRenderUpdates()) {
94-
clearInterval(this._loopHandler);
95-
this.startLoop();
96-
};
97-
}, 1000 / 60);
98-
} else {
78+
switchLoop(hasChanges) {
79+
if (hasChanges) {
9980
this._idleLoopCounter = 0;
81+
return false;
10082
}
83+
if (++this._idleLoopCounter < this._idleLoopDelay) {
84+
return false;
85+
}
86+
87+
this.stopLoop();
88+
this._loopHandler = setInterval(() => {
89+
this.stage.updateFrame();
90+
const hasChanges = this.stage.renderFrame();
91+
if (hasChanges) {
92+
clearInterval(this._loopHandler);
93+
this.startLoop();
94+
}
95+
}, 1000 / 30);
96+
97+
this.stage.onIdle();
98+
return true;
10199
}
102100

103101
loop() {
104-
let self = this;
105-
let lp = function () {
106-
self._awaitingLoop = false;
107-
self._onIdle = false;
108-
if (self._looping) {
109-
self.stage.updateFrame();
110-
if (self.stage.getOption("pauseRafLoopOnIdle")) {
111-
self.switchLoop();
102+
const pauseRafLoopOnIdle = this.stage.getOption("pauseRafLoopOnIdle");
103+
const lp = () => {
104+
this._awaitingLoop = false;
105+
if (this._looping) {
106+
this.stage.updateFrame();
107+
const hasChanges = this.stage.renderFrame();
108+
if (pauseRafLoopOnIdle && this.switchLoop(hasChanges)) {
109+
return;
112110
}
113-
self.stage.renderFrame();
114111
requestAnimationFrame(lp);
115-
self._awaitingLoop = true;
112+
this._awaitingLoop = true;
116113
}
117114
}
118115
requestAnimationFrame(lp);

src/tree/Stage.d.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ declare class Stage extends EventEmitter<Stage.EventMap> {
427427
*/
428428
isUpdatingFrame(): boolean;
429429

430-
// renderFrame(): void;
430+
// renderFrame(): boolean;
431431
// - Only used internally
432432

433433
/**

src/tree/Stage.mjs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export default class Stage extends EventEmitter {
170170
_setOptions(o) {
171171
this._options = {};
172172

173-
let opt = (name, def) => {
173+
const opt = (name, def) => {
174174
let value = o[name];
175175

176176
if (value === undefined) {
@@ -315,7 +315,7 @@ export default class Stage extends EventEmitter {
315315
this._updateSourceTextures.forEach(texture => {
316316
texture._performUpdateSource();
317317
});
318-
this._updateSourceTextures = new Set();
318+
this._updateSourceTextures.clear();
319319
}
320320
}
321321

@@ -337,12 +337,6 @@ export default class Stage extends EventEmitter {
337337
this.emit('update');
338338
}
339339

340-
idleFrame() {
341-
this.textureThrottler.processSome();
342-
this.emit('frameEnd');
343-
this.frameCounter++;
344-
}
345-
346340
onIdle() {
347341
this.emit('idle');
348342
}
@@ -366,6 +360,8 @@ export default class Stage extends EventEmitter {
366360
this.emit('frameEnd');
367361

368362
this.frameCounter++;
363+
364+
return changes;
369365
}
370366

371367
isUpdatingFrame() {

0 commit comments

Comments
 (0)