Spaces:
Runtime error
Runtime error
Download src/lib/utils/updates.ts from evijit/pluralchat: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/spaces/evijit/pluralchat/resolve/refs%2Fpr%2F1/src/lib/utils/updates.ts
- Command line
-
hf download hf://spaces/evijit/pluralchat@refs/pr/1/src/lib/utils/updates.ts
-
curl -L -o updates.ts https://huggingface.co/spaces/evijit/pluralchat/resolve/refs%2Fpr%2F1/src/lib/utils/updates.ts
1.09 kB
| // 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(); | |