Skip to content

Commit 10f4982

Browse files
committed
perf: throttle oscilloscope canvas updates when minimized or backgrounded
1 parent 6860e5f commit 10f4982

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

js/widgets/oscilloscope.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ class Oscilloscope {
6363
this.close();
6464
};
6565
widgetWindow.onmaximize = this._scale.bind(this);
66+
widgetWindow.onrollup = this._throttle.bind(this);
67+
widgetWindow.onunroll = this._wakeUp.bind(this);
68+
69+
this._handleVisibilityChange = this._handleVisibilityChange.bind(this);
70+
document.addEventListener("visibilitychange", this._handleVisibilityChange);
6671

6772
// UI state
6873
this.zoomFactor = 40.0;
@@ -106,6 +111,7 @@ class Oscilloscope {
106111

107112
if (this._rafId !== null) {
108113
cancelAnimationFrame(this._rafId);
114+
clearTimeout(this._rafId);
109115
this._rafId = null;
110116
}
111117

@@ -123,9 +129,34 @@ class Oscilloscope {
123129
this.draw();
124130
}
125131

132+
_throttle() {
133+
if (!this._running || this._rafId === null) return;
134+
cancelAnimationFrame(this._rafId);
135+
clearTimeout(this._rafId);
136+
this._rafId = setTimeout(this.draw, 1000);
137+
}
138+
139+
_wakeUp() {
140+
if (!this._running || this._rafId === null) return;
141+
cancelAnimationFrame(this._rafId);
142+
clearTimeout(this._rafId);
143+
this._rafId = null;
144+
this.draw();
145+
}
146+
147+
_handleVisibilityChange() {
148+
if (document.visibilityState === "visible") {
149+
this._wakeUp();
150+
} else {
151+
this._throttle();
152+
}
153+
}
154+
126155
close() {
127156
this._stopAnimation();
128157

158+
document.removeEventListener("visibilitychange", this._handleVisibilityChange);
159+
129160
this.drawVisualIDs = {};
130161
this._canvasState = {};
131162
this.pitchAnalysers = {};
@@ -214,6 +245,11 @@ class Oscilloscope {
214245
draw() {
215246
if (!this._running) return;
216247

248+
if (document.visibilityState === "hidden" || this.widgetWindow._rolled) {
249+
this._rafId = setTimeout(this.draw, 1000);
250+
return;
251+
}
252+
217253
this._renderFrame();
218254

219255
this._rafId = requestAnimationFrame(this.draw);

js/widgets/widgetWindows.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ class WidgetWindow {
6161
this.takeFocus();
6262

6363
this.sendToCenter = this.sendToCenter.bind(this);
64+
65+
this.onclose = () => {};
66+
this.onmaximize = () => {};
67+
this.onrollup = () => {};
68+
this.onunroll = () => {};
6469
}
6570

6671
/**
@@ -620,6 +625,7 @@ class WidgetWindow {
620625
_rollup() {
621626
this._rolled = true;
622627
this._body.style.display = "none";
628+
this.onrollup();
623629
return this;
624630
}
625631

@@ -633,6 +639,7 @@ class WidgetWindow {
633639
if (this._rollButton && this._rollButton.classList.contains("plus")) {
634640
this._rollButton.classList.remove("plus");
635641
}
642+
this.onunroll();
636643
return this;
637644
}
638645
}

0 commit comments

Comments
 (0)