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.
61 lines • 2.06 kB
JavaScript
import { _Debug } from "../../debug/_debug.js";
import { getGlobal } from "../../runtime/get-global.js";
import { arrayMap } from "../../array/impl/array-map.js";
import { ManagedResourceLinks } from "../../lifecycle/managed-resource-links.js";
import { BroadcastChannel } from "../../eventing/broadcast-channel.js";
export class LifecycleStack {
constructor() {
this.index = 0;
const existingOwners = getGlobal()["RC_ALLOCATION_OWNER_STACK"];
this.ownerStack = existingOwners !== null && existingOwners !== void 0 ? existingOwners : arrayMap(new Array(128), () => new BlockScopeNode());
}
push() {
if (_BUILD.DEBUG) {
_Debug.assert(this.index < 128, "overflowed the allocation stack...");
}
return this.ownerStack[this.index++];
}
pop() {
_BUILD.DEBUG && _Debug.assert(this.index > 0, "attempted to pop empty stack");
return this.ownerStack[--this.index];
}
/**
* For the current level, return the owning node.
*/
getTop() {
_BUILD.DEBUG && _Debug.assert(this.index > 0, "tried to get top of empty stack");
return this.ownerStack[this.index - 1];
}
getSize() {
return this.index;
}
}
/**
* @internal
* The block scoped node is mostly the same as other nodes, except they live forever (when a scope is exited
* we just unlink the refs).
*/
class BlockScopeNode {
constructor() {
this.onFreeChannel = new BroadcastChannel("onFree");
// we always have linked nodes, so no reason to lazy create this
this.linkedReferences = new ManagedResourceLinks(this);
}
getIsDestroyed() {
return false;
}
getLinked() {
return this.linkedReferences;
}
onClaimed() {
_BUILD.DEBUG && _Debug.error("this should never be called");
}
onReleased() {
_BUILD.DEBUG && _Debug.error("this should never be called");
}
}
/**
* @public
*/
export const lifecycleStack = new LifecycleStack();
//# sourceMappingURL=lifecycle-stack.js.map