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.
65 lines • 2.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.lifecycleStack = exports.LifecycleStack = void 0;
const _debug_js_1 = require("../../debug/_debug.js");
const get_global_js_1 = require("../../runtime/get-global.js");
const array_map_js_1 = require("../../array/impl/array-map.js");
const managed_resource_links_js_1 = require("../../lifecycle/managed-resource-links.js");
const broadcast_channel_js_1 = require("../../eventing/broadcast-channel.js");
class LifecycleStack {
constructor() {
this.index = 0;
const existingOwners = (0, get_global_js_1.getGlobal)()["RC_ALLOCATION_OWNER_STACK"];
this.ownerStack = existingOwners !== null && existingOwners !== void 0 ? existingOwners : (0, array_map_js_1.arrayMap)(new Array(128), () => new BlockScopeNode());
}
push() {
if (_BUILD.DEBUG) {
_debug_js_1._Debug.assert(this.index < 128, "overflowed the allocation stack...");
}
return this.ownerStack[this.index++];
}
pop() {
_BUILD.DEBUG && _debug_js_1._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_js_1._Debug.assert(this.index > 0, "tried to get top of empty stack");
return this.ownerStack[this.index - 1];
}
getSize() {
return this.index;
}
}
exports.LifecycleStack = LifecycleStack;
/**
* @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 broadcast_channel_js_1.BroadcastChannel("onFree");
// we always have linked nodes, so no reason to lazy create this
this.linkedReferences = new managed_resource_links_js_1.ManagedResourceLinks(this);
}
getIsDestroyed() {
return false;
}
getLinked() {
return this.linkedReferences;
}
onClaimed() {
_BUILD.DEBUG && _debug_js_1._Debug.error("this should never be called");
}
onReleased() {
_BUILD.DEBUG && _debug_js_1._Debug.error("this should never be called");
}
}
/**
* @public
*/
exports.lifecycleStack = new LifecycleStack();
//# sourceMappingURL=lifecycle-stack.js.map