()
| 13 | const { emitExperimentalWarning } = require('internal/util'); |
| 14 | |
| 15 | function createFinalization() { |
| 16 | /** |
| 17 | * @type {SafeFinalizationRegistry} |
| 18 | */ |
| 19 | let registry = null; |
| 20 | |
| 21 | const refs = { |
| 22 | __proto__: null, |
| 23 | exit: [], |
| 24 | beforeExit: [], |
| 25 | }; |
| 26 | |
| 27 | const functions = { |
| 28 | __proto__: null, |
| 29 | exit: onExit, |
| 30 | beforeExit: onBeforeExit, |
| 31 | }; |
| 32 | |
| 33 | function install(event) { |
| 34 | if (refs[event].length > 0) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | process.on(event, functions[event]); |
| 39 | } |
| 40 | |
| 41 | function uninstall(event) { |
| 42 | if (refs[event].length > 0) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | process.removeListener(event, functions[event]); |
| 47 | |
| 48 | if (refs.exit.length === 0 && refs.beforeExit.length === 0) { |
| 49 | registry = null; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | function onExit() { |
| 54 | callRefsToFree('exit'); |
| 55 | } |
| 56 | |
| 57 | function onBeforeExit() { |
| 58 | callRefsToFree('beforeExit'); |
| 59 | } |
| 60 | |
| 61 | function callRefsToFree(event) { |
| 62 | for (const ref of refs[event]) { |
| 63 | const obj = ref.deref(); |
| 64 | const fn = ref.fn; |
| 65 | |
| 66 | // This should always happen, however GC is |
| 67 | // indeterministic so it might not happen. |
| 68 | /* istanbul ignore else */ |
| 69 | if (obj !== undefined) { |
| 70 | fn(obj, event); |
| 71 | } |
| 72 | } |
no outgoing calls
no test coverage detected
searching dependent graphs…