UNPKG

rc-js-util

Version:

A collection of TS and C++ utilities to help writing performant and correct applications, achieved through strict typing and (removable) invariant checking.

45 lines (38 loc) 855 B
import { fpNoOp } from "../fp/impl/fp-no-op.js"; /** * @public * Weak reference counter. */ export class ReferenceCounter<TKey extends object> { public constructor ( private onZeroReference: (key: TKey) => void = fpNoOp, ) { } public add(key: TKey): number { let count = this.store.get(key); count ??= 0; this.store.set(key, ++count); return count; } public remove(key: TKey): number { let count = this.store.get(key); if (count == null) { return 0; } if (count === 1) { this.onZeroReference(key); this.store.delete(key); return 0; } this.store.set(key, --count); return count; } private readonly store = new WeakMap<object, number>(); }