File size: 1,088 Bytes
fc69895
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// This is a debouncer for the updates from the server to the client
// It is used to prevent the client from being overloaded with too many updates
// It works by keeping track of the time it takes to render the updates
// and adding a safety margin to it, to find the debounce time.

class UpdateDebouncer {
	private renderStartedAt: Date | null = null;
	private lastRenderTimes: number[] = [];

	get maxUpdateTime() {
		if (this.lastRenderTimes.length === 0) {
			return 50;
		}

		const averageTime =
			this.lastRenderTimes.reduce((acc, time) => acc + time, 0) / this.lastRenderTimes.length;

		return Math.min(averageTime * 3, 500);
	}

	public startRender() {
		this.renderStartedAt = new Date();
	}

	public endRender() {
		if (!this.renderStartedAt) {
			return;
		}

		const timeSinceRenderStarted = new Date().getTime() - this.renderStartedAt.getTime();
		this.lastRenderTimes.push(timeSinceRenderStarted);
		if (this.lastRenderTimes.length > 10) {
			this.lastRenderTimes.shift();
		}
		this.renderStartedAt = null;
	}
}

export const updateDebouncer = new UpdateDebouncer();