• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const gcTrackerMap = new WeakMap();
6const gcTrackerTag = 'NODE_TEST_COMMON_GC_TRACKER';
7
8function onGC(obj, gcListener) {
9  const async_hooks = require('async_hooks');
10
11  const onGcAsyncHook = async_hooks.createHook({
12    init: common.mustCallAtLeast(function(id, type) {
13      if (this.trackedId === undefined) {
14        assert.strictEqual(type, gcTrackerTag);
15        this.trackedId = id;
16      }
17    }),
18    destroy(id) {
19      assert.notStrictEqual(this.trackedId, -1);
20      if (id === this.trackedId) {
21        this.gcListener.ongc();
22        onGcAsyncHook.disable();
23      }
24    },
25  }).enable();
26  onGcAsyncHook.gcListener = gcListener;
27
28  gcTrackerMap.set(obj, new async_hooks.AsyncResource(gcTrackerTag));
29  obj = null;
30}
31
32module.exports = onGC;
33