Elffuss BraidHub

kikocis/continue public

9d8db5fc402e · 847 BRawPermalink
1import { v4 as uuidv4 } from "uuid";
2export class AutocompleteDebouncer {
3 private debounceTimeout: NodeJS.Timeout | undefined = undefined;
4 private debouncing = false;
5 private lastUUID: string | undefined = undefined;
6
7 async delayAndShouldDebounce(debounceDelay: number): Promise<boolean> {
8 // Debounce
9 const uuid = uuidv4();
10 this.lastUUID = uuid;
11
12 // Debounce
13 if (this.debouncing) {
14 this.debounceTimeout?.refresh();
15 const lastUUID = await new Promise((resolve) =>
16 setTimeout(() => {
17 resolve(this.lastUUID);
18 }, debounceDelay),
19 );
20 if (uuid !== lastUUID) {
21 return true;
22 }
23 } else {
24 this.debouncing = true;
25 this.debounceTimeout = setTimeout(async () => {
26 this.debouncing = false;
27 }, debounceDelay);
28 }
29
30 return false;
31 }
32}