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.

64 lines 2 kB
import { DirtyCheckedUniqueCollection } from "../collection/dirty-checked-unique-collection.js"; import { _Debug } from "../debug/_debug.js"; /** * @internal */ export class ManagedResourceLinks { constructor(owningRef) { this.owningRef = owningRef; this.linkedTo = new DirtyCheckedUniqueCollection(); } isLinkedTo(ref) { return this.linkedTo.has(ref); } link(ref) { if (!this.linkedTo.has(ref)) { if (_BUILD.DEBUG) { // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions if (!_BUILD.WASM_DISABLE_CYCLE_CHECKS) { _Debug.assert(!DEBUG_getHasCycle(this.owningRef, ref), "detected cycle between references"); } _Debug.assert(!ref.getIsDestroyed() && !this.owningRef.getIsDestroyed(), "attempted to link to dead ref"); } this.linkedTo.add(ref); ref.onClaimed(this.owningRef); } return this; } unlink(ref) { const removed = this.linkedTo.delete(ref); if (removed) { ref.onReleased(this.owningRef); } return this; } unlinkAll() { this.clearRefs(); } clearRefs() { const refs = this.linkedTo.getArray(); const ref = this.owningRef; for (let i = 0, iEnd = refs.length; i < iEnd; ++i) { refs[i].onReleased(ref); } this.linkedTo.clear(); } getLinkedNodes() { return this.linkedTo.getArray(); } } function DEBUG_getHasCycle(referencingTo, referencingFrom) { if (referencingTo === referencingFrom) { return true; } const refs = referencingFrom .getLinked() .getLinkedNodes(); for (let i = 0, iEnd = refs.length; i < iEnd; ++i) { if (DEBUG_getHasCycle(referencingTo, refs[i])) { return true; } } return false; } //# sourceMappingURL=managed-resource-links.js.map