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.

59 lines 2.56 kB
import { PointerDebugMetadata } from "../../lifecycle/manged-resources.js"; import { _Production } from "../../production/_production.js"; /** * @public * Defines the cleanup behavior of {@link SharedObjectCleanup} */ export var ESharedObjectOwnerKind; (function (ESharedObjectOwnerKind) { ESharedObjectOwnerKind[ESharedObjectOwnerKind["NotOwning"] = 1] = "NotOwning"; ESharedObjectOwnerKind[ESharedObjectOwnerKind["SharedMemoryOwner"] = 2] = "SharedMemoryOwner"; ESharedObjectOwnerKind[ESharedObjectOwnerKind["Freeable"] = 3] = "Freeable"; })(ESharedObjectOwnerKind || (ESharedObjectOwnerKind = {})); /** * @public * * Provides reasonable defaults for cleaning up a handle to a `JsInterop::ASharedMemoryObject`. */ export class SharedObjectCleanup { static createWithCleanup(sharedObject, options) { const cleanup = new SharedObjectCleanup(sharedObject, options.ownershipKind); SharedObjectCleanup.registerCleanup(sharedObject, cleanup, options); return cleanup; } static registerCleanup(sharedObject, cleanup, options) { // regardless of owning, there might still be cleanup required sharedObject.resourceHandle.onFreeChannel.addListener(cleanup); sharedObject.getWrapper() .lifecycleStrategy .onSharedPointerCreated(sharedObject, new PointerDebugMetadata(sharedObject.pointer, options.ownershipKind != ESharedObjectOwnerKind.NotOwning, options.debugName), options.protectedView); } constructor(sharedObject, ownershipKind) { this.ownershipKind = ownershipKind; this.wrapper = sharedObject.getWrapper(); this.ptr = sharedObject.pointer; } onFree() { switch (this.ownershipKind) { case ESharedObjectOwnerKind.SharedMemoryOwner: this.wrapper.instance._jsUtilDeleteObject(this.ptr); break; case ESharedObjectOwnerKind.Freeable: this.wrapper.instance._jsUtilFree(this.ptr); break; case ESharedObjectOwnerKind.NotOwning: // we don't need to do anything... break; default: _Production.assertValueIsNever(this.ownershipKind); } } } SharedObjectCleanup.Options = class SharedObjectCleanupOptions { constructor(debugName, protectedView, ownershipKind) { this.debugName = debugName; this.protectedView = protectedView; this.ownershipKind = ownershipKind; } }; //# sourceMappingURL=shared-object-cleanup.js.map