gc-listen

Calls a function when V8 collects an object as garbage

Usage no npm install needed!

<script type="module">
  import gcListen from 'https://cdn.skypack.dev/gc-listen';
</script>

README

gc-listen

A way to attach a JavaScript function with napi_add_finalizer. For debugging.

const gcListen = require('gc-listen');

const obj = {};

const callback = () => {
    // …
};

gcListen(obj, callback);

callback won’t be called as long as obj isn’t eligible for garbage collection. N-API even suggests it will be called when obj is collected. It will always be called asynchronously when called at all.