mirror of
https://github.com/nodejs/node.git
synced 2025-08-17 06:38:47 +02:00

PR-URL: https://github.com/nodejs/node/pull/45550 Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
32 lines
842 B
JavaScript
32 lines
842 B
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const gcTrackerMap = new WeakMap();
|
|
const gcTrackerTag = 'NODE_TEST_COMMON_GC_TRACKER';
|
|
|
|
function onGC(obj, gcListener) {
|
|
const async_hooks = require('async_hooks');
|
|
|
|
const onGcAsyncHook = async_hooks.createHook({
|
|
init: common.mustCallAtLeast(function(id, type) {
|
|
if (this.trackedId === undefined) {
|
|
assert.strictEqual(type, gcTrackerTag);
|
|
this.trackedId = id;
|
|
}
|
|
}),
|
|
destroy(id) {
|
|
assert.notStrictEqual(this.trackedId, -1);
|
|
if (id === this.trackedId) {
|
|
this.gcListener.ongc();
|
|
onGcAsyncHook.disable();
|
|
}
|
|
},
|
|
}).enable();
|
|
onGcAsyncHook.gcListener = gcListener;
|
|
|
|
gcTrackerMap.set(obj, new async_hooks.AsyncResource(gcTrackerTag));
|
|
obj = null;
|
|
}
|
|
|
|
module.exports = onGC;
|