mirror of
https://github.com/zsviczian/obsidian-excalidraw-plugin.git
synced 2025-08-06 05:46:28 +00:00
29 lines
598 B
TypeScript
29 lines
598 B
TypeScript
export class WeakArray<T extends object> {
|
|
private weakArray: WeakRef<T>[] = [];
|
|
|
|
constructor() {}
|
|
|
|
push(obj: T) {
|
|
this.weakArray.push(new WeakRef(obj));
|
|
}
|
|
|
|
forEach(callback: (obj: T, index: number) => void) {
|
|
this.weakArray.forEach((ref, index) => {
|
|
const obj = ref.deref();
|
|
if (obj) {
|
|
callback(obj, index);
|
|
}
|
|
});
|
|
}
|
|
|
|
removeObjects(objectsToRemove: Set<T>) {
|
|
this.weakArray = this.weakArray.filter((ref) => {
|
|
const obj = ref.deref();
|
|
return obj && !objectsToRemove.has(obj);
|
|
});
|
|
}
|
|
|
|
clear() {
|
|
this.weakArray = [];
|
|
}
|
|
} |