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.

50 lines 2.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AutomaticGcStrategy = void 0; const reference_counted_node_js_1 = require("../../lifecycle/reference-counted-node.js"); const lifecycle_stack_js_1 = require("./lifecycle-stack.js"); /** * @public * Automatically releases handles when objects are garbage collected. The implementation relies on `FinalizationRegistry`, * which comes with a large number of health warnings, you probably shouldn't use it in production for anything important... * * Do not use this for testing of library functions, instead use {@link ReferenceCountedStrategy}. */ class AutomaticGcStrategy { constructor() { this.finalizationRegistry = new FinalizationRegistry((resourceHandle) => { resourceHandle.onFreeChannel.emit(); resourceHandle.getLinked().unlinkAll(); }); } createNode(owner) { // our reference counted node is a hybrid (there's no RAII...), so we have the object graph here // this means we don't need worry about pointer cycles, the JS garbage collector is doing the heavy lifting for us owner !== null && owner !== void 0 ? owner : (owner = lifecycle_stack_js_1.lifecycleStack.getTop()); const newNode = new reference_counted_node_js_1.ReferenceCountedNode(); owner.getLinked().link(newNode); newNode.onReleased(); return newNode; } /** * @internal */ createRootNode() { return new reference_counted_node_js_1.ReferenceCountedNode(); } onSharedPointerCreated(sharedPtr) { // this strategy doesn't require this extra metadata this.onManagedObjectCreated(sharedPtr); } onManagedObjectCreated(object) { this.finalizationRegistry.register(object, object.resourceHandle); } /** * @internal */ setWrapper() { // not required... } } exports.AutomaticGcStrategy = AutomaticGcStrategy; //# sourceMappingURL=automatic-gc-strategy.js.map