Buckets:
32.1 GB
673,093 files
Updated 5 months ago
Ctrl+K
| Name | Size | Uploaded | Xet hash |
|---|---|---|---|
| .github | 2 items | ||
| test | 6 items | ||
| LICENSE | 1.07 kB xet | 5994d2eb | |
| README.md | 1.28 kB xet | 750bd9f5 | |
| index.js | 1.96 kB xet | 1cdda449 | |
| package.json | 876 Bytes xet | 14f29f2e |
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 () {
// This object can be safely garbage collected,
// and the resulting shutdown function will not be called.
// There are no leaks.
const obj = { foo: 'bar' }
register(obj, shutdown)
// use registerBeforeExit(obj, shutdown) to execute the function only
// on beforeExit
// call unregister(obj) to remove
}
let shutdownCalled = false
// Please make sure that the function passed to register()
// does not create a closure around unnecessary objects.
function shutdown (obj, eventName) {
console.log(eventName) // beforeExit
shutdownCalled = true
assert.strictEqual(obj.foo, 'bar')
}
setup()
process.on('exit', function () {
assert.strictEqual(shutdownCalled, true)
})
License
MIT
- Total size
- 32.1 GB
- Files
- 673,093
- Last updated
- Mar 19
- Pre-warmed CDN
- US EU US EU