Skip to content

Commit e061241

Browse files
committed
use weak ref for cache
1 parent a00f9b7 commit e061241

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/cache.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ export interface Clearable {
22
clear(): void;
33
}
44
export class Cache {
5-
#caches: Array<Clearable> = [];
5+
#caches: Set<WeakRef<Clearable>> = new Set();
66
add(cache: Clearable): void {
7-
this.#caches.push(cache);
7+
this.#caches.add(new WeakRef(cache));
88
}
99
clear(): void {
10-
for (const cache of this.#caches) {
11-
cache.clear();
10+
for (const ref of this.#caches) {
11+
const cache = ref.deref();
12+
if (cache != null) {
13+
cache.clear();
14+
} else {
15+
this.#caches.delete(ref);
16+
}
1217
}
1318
}
1419
}

0 commit comments

Comments
 (0)