import { DisposeFun, MemoizeCache } from "../interface"; import BaseCache from "./BaseCache"; export const defaultDispose: DisposeFun = () => void 0 export default class BaseCacheWithDispose extends BaseCache { readonly weak: boolean readonly dispose: DisposeFun constructor(weak: boolean = false, dispose: DisposeFun = defaultDispose) { super(weak) this.weak = weak this.dispose = dispose } disposeValue(value: V | undefined): void { if (value) { this.dispose(value) } } disposeAllValue(cacheMap: MemoizeCache): void { for (let mapValue of (cacheMap as any)) { this.disposeValue(mapValue?.[1]) } } clear(): void { if (this.weak) { // WeakMap doesn't provide cleanup for the time being this.cacheMap = new WeakMap() } else { this.disposeAllValue(this.cacheMap) this.cacheMap.clear!() } } }