Skip to content

Commit 1ac1aae

Browse files
committed
Debounce server resize calls
1 parent 607af1a commit 1ac1aae

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/client.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { webLinksHandler } from "./lib/addons";
1515
import { initiateRemoteCommunicationChannelSocket } from "./lib/remote";
1616
import { Emitter } from '@gitpod/gitpod-protocol/lib/util/event';
1717
import { DisposableCollection } from '@gitpod/gitpod-protocol/lib/util/disposable';
18+
import { debounce } from './lib/helpers';
1819

1920
const onDidChangeState = new Emitter<void>();
2021
let state: IDEFrontendState = "initializing" as IDEFrontendState;
@@ -238,7 +239,8 @@ function updateTerminalSize(): void {
238239
fitAddon.fit();
239240
}
240241

241-
window.onresize = () => updateTerminalSize();
242+
const debouncedUpdateTerminalSize = debounce(updateTerminalSize, 200, true);
243+
window.onresize = () => debouncedUpdateTerminalSize();
242244

243245
window.gitpod.ideService = {
244246
get state() {

src/lib/helpers.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Debounces a function by limiting the rate at which it is called.
3+
* The debounced function will be called immediately when first called,
4+
* and then at most once every `wait` milliseconds during subsequent calls.
5+
* If `trailing` is set to `true`, the function will also be called once
6+
* after the last event is triggered.
7+
*
8+
* @param {() => void} func - The function to be debounced.
9+
* @param {number} wait - The number of milliseconds to wait before allowing the function to be called again.
10+
* @param {boolean} trailing - Whether to call the function once after the last event is triggered.
11+
* @returns {() => void} - The debounced function.
12+
*/
13+
export const debounce = (func: () => void, wait: number, trailing: boolean): () => void => {
14+
let timeout: NodeJS.Timeout | null = null;
15+
16+
return () => {
17+
const later = () => {
18+
timeout = null;
19+
if (trailing) {
20+
func();
21+
}
22+
};
23+
24+
if (timeout === null) {
25+
func();
26+
} else {
27+
clearTimeout(timeout);
28+
}
29+
timeout = setTimeout(later, wait);
30+
};
31+
}

0 commit comments

Comments
 (0)