This commit is contained in:
Henning Dieterichs
2022-12-01 16:14:17 +01:00
parent 1b6fabfe29
commit 904d0060ea
24 changed files with 1841 additions and 580 deletions

View File

@@ -0,0 +1,21 @@
export class DebouncedRunner {
private timeout: NodeJS.Timeout | undefined;
constructor(private readonly debounceTimeout: number) {}
public run(action: () => void): void {
this.clear();
this.timeout = setTimeout(action, this.debounceTimeout);
}
private clear() {
if (this.timeout) {
clearTimeout(this.timeout);
this.timeout = undefined;
}
}
public dispose(): void {
this.clear();
}
}

View File

@@ -0,0 +1,30 @@
import { Disposable } from "@hediet/std/disposable";
import { observable, autorun } from "mobx";
export class IncrementalMap<TKey, TValue extends Disposable> {
public readonly map: Map<TKey, TValue> = new Map();
constructor(
private readonly getKeys: () => TKey[],
private readonly getValue: (key: TKey) => TValue
) {
this.dispose.track({
dispose: autorun(() => {
const keys = this.getKeys();
const toRemove = new Set(this.map.keys());
for (const key of keys) {
toRemove.delete(key);
if (!this.map.has(key)) {
this.map.set(key, this.getValue(key));
}
}
for (const key of toRemove) {
this.map.get(key)!.dispose();
this.map.delete(key);
}
}),
});
}
public readonly dispose = Disposable.fn();
}