UNPKG

nope-js-node

Version:

NoPE Runtime for Nodejs. For Browser-Support please use nope-browser

49 lines (48 loc) 1.83 kB
"use strict"; /** * @modue gc * * Module to interact with the `garbage collector` (`gc`) of Nodejs. * * - Use the function `registerGarbageCallback` if you want to register a callback which will be called, if the item is getting remove by the gc. * - Try to call `forceGarbageCollection` to manually trigger the `gc`. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.registerGarbageCallback = exports.FINALIZER = exports.forceGarbageCollection = void 0; const async_1 = require("./async"); const singletonMethod_1 = require("./singletonMethod"); /** * Helper trying to call the carbage collection. * - Wont raise an exception, if the gc is not available. * - it isn't shure that the gc will be called. */ function forceGarbageCollection() { if (global.gc) { global.gc(); } } exports.forceGarbageCollection = forceGarbageCollection; /** * A `Finalizer`, provided as true `Singleton` * * Check the description: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry to get a better understanding of a `Finalizer` */ exports.FINALIZER = (0, singletonMethod_1.getSingleton)("nope.finanlizer", () => { return new FinalizationRegistry(async (callback) => { if ((0, async_1.isAsyncFunction)(callback)) { await callback(); } else { callback(); } }); }); /** * Helper to register a callback which will be called, if the item is getting remove by the gc. * @param item The item to be collected by the gc. * @param callback The callback to call. */ function registerGarbageCallback(item, callback) { exports.FINALIZER.instance.register(item, callback); } exports.registerGarbageCallback = registerGarbageCallback;