on-exit-leak-free
This module helps dispose of an object gracefully when the Node.js process exits.
It executes a function with a given parameter
on 'exit'
without leaking memory,
cleaning things up appropriately if the object is garbage collected.
Requires WeakRef
and FinalizationRegistry
, i.e. use Node v14+.
Install
npm i on-exit-leak-free
Example
'use strict'
const { register, unregister } = require('on-exit-leak-free')
const assert = require('assert')
function setup () {
const obj = { foo: 'bar' }
register(obj, shutdown)
}
let shutdownCalled = false
function shutdown (obj, eventName) {
console.log(eventName)
shutdownCalled = true
assert.strictEqual(obj.foo, 'bar')
}
setup()
process.on('exit', function () {
assert.strictEqual(shutdownCalled, true)
})
License
MIT