codekingpro/portable-devtools
114k
1'use strict'
2
3const { kConnected, kSize } = require('../../core/symbols')
4
5class CompatWeakRef {
6 constructor (value) {
7 this.value = value
8 }
9
10 deref () {
11 return this.value[kConnected] === 0 && this.value[kSize] === 0
12 ? undefined
13 : this.value
14 }
15}
16
17class CompatFinalizer {
18 constructor (finalizer) {
19 this.finalizer = finalizer
20 }
21
22 register (dispatcher, key) {
23 if (dispatcher.on) {
24 dispatcher.on('disconnect', () => {
25 if (dispatcher[kConnected] === 0 && dispatcher[kSize] === 0) {
26 this.finalizer(key)
27 }
28 })
29 }
30 }
31
32 unregister (key) {}
33}
34
35module.exports = function () {
36 // FIXME: remove workaround when the Node bug is backported to v18
37 // https://github.com/nodejs/node/issues/49344#issuecomment-1741776308
38 if (process.env.NODE_V8_COVERAGE && process.version.startsWith('v18')) {
39 process._rawDebug('Using compatibility WeakRef and FinalizationRegistry')
40 return {
41 WeakRef: CompatWeakRef,
42 FinalizationRegistry: CompatFinalizer
43 }
44 }
45 return { WeakRef, FinalizationRegistry }
46}
47 