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.

35 lines 988 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ReferenceCounter = void 0; const fp_no_op_js_1 = require("../fp/impl/fp-no-op.js"); /** * @public * Weak reference counter. */ class ReferenceCounter { constructor(onZeroReference = fp_no_op_js_1.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; } } exports.ReferenceCounter = ReferenceCounter; //# sourceMappingURL=reference-counter.js.map