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.
40 lines • 1.16 kB
JavaScript
import { BroadcastChannel } from "../eventing/broadcast-channel.js";
import { ManagedResourceLinks } from "./managed-resource-links.js";
import { _Debug } from "../debug/_debug.js";
/**
* @internal
*/
export class ReferenceCountedNode {
constructor() {
this.onFreeChannel = new BroadcastChannel("onFree");
this.linkedReferences = null;
this.references = 1;
}
onClaimed() {
if (this.references <= 0) {
_BUILD.DEBUG && _Debug.error("object has been released already");
return;
}
++this.references;
}
onReleased() {
if (--this.references === 0) {
this.onFree();
}
}
getIsDestroyed() {
return this.references <= 0;
}
getLinked() {
if (this.linkedReferences == null) {
return this.linkedReferences = new ManagedResourceLinks(this);
}
return this.linkedReferences;
}
onFree() {
var _a;
(_a = this.linkedReferences) === null || _a === void 0 ? void 0 : _a.unlinkAll();
this.onFreeChannel.emit();
}
}
//# sourceMappingURL=reference-counted-node.js.map