mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
d495e40bf7
Incrementally making `require('../common')` less of a monolith. Move the `common.onGC()` utility to a separate standalone module that is only imported when it's actually needed. PR-URL: https://github.com/nodejs/node/pull/22446 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
33 lines
841 B
JavaScript
33 lines
841 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;
|