2.4.0
This commit is contained in:
21
extension/src/utils/DebouncedRunner.ts
Normal file
21
extension/src/utils/DebouncedRunner.ts
Normal 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();
|
||||
}
|
||||
}
|
||||
30
extension/src/utils/IncrementalMap.ts
Normal file
30
extension/src/utils/IncrementalMap.ts
Normal 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();
|
||||
}
|
||||
Reference in New Issue
Block a user