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.

31 lines 816 B
import { fpNoOp } from "../fp/impl/fp-no-op.js"; /** * @public * Weak reference counter. */ export class ReferenceCounter { constructor(onZeroReference = fpNoOp) { this.onZeroReference = onZeroReference; this.store = new WeakMap(); } add(key) { let count = this.store.get(key); count !== null && count !== void 0 ? count : (count = 0); this.store.set(key, ++count); return count; } remove(key) { 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; } } //# sourceMappingURL=reference-counter.js.map