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.
68 lines • 2.23 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ManagedResourceLinks = void 0;
const dirty_checked_unique_collection_js_1 = require("../collection/dirty-checked-unique-collection.js");
const _debug_js_1 = require("../debug/_debug.js");
/**
* @internal
*/
class ManagedResourceLinks {
constructor(owningRef) {
this.owningRef = owningRef;
this.linkedTo = new dirty_checked_unique_collection_js_1.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_js_1._Debug.assert(!DEBUG_getHasCycle(this.owningRef, ref), "detected cycle between references");
}
_debug_js_1._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();
}
}
exports.ManagedResourceLinks = ManagedResourceLinks;
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