11cb0ef41Sopenharmony_ci'use strict'
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci/* istanbul ignore file: only for Node 12 */
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst { kConnected, kSize } = require('../core/symbols')
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciclass CompatWeakRef {
81cb0ef41Sopenharmony_ci  constructor (value) {
91cb0ef41Sopenharmony_ci    this.value = value
101cb0ef41Sopenharmony_ci  }
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci  deref () {
131cb0ef41Sopenharmony_ci    return this.value[kConnected] === 0 && this.value[kSize] === 0
141cb0ef41Sopenharmony_ci      ? undefined
151cb0ef41Sopenharmony_ci      : this.value
161cb0ef41Sopenharmony_ci  }
171cb0ef41Sopenharmony_ci}
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciclass CompatFinalizer {
201cb0ef41Sopenharmony_ci  constructor (finalizer) {
211cb0ef41Sopenharmony_ci    this.finalizer = finalizer
221cb0ef41Sopenharmony_ci  }
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  register (dispatcher, key) {
251cb0ef41Sopenharmony_ci    if (dispatcher.on) {
261cb0ef41Sopenharmony_ci      dispatcher.on('disconnect', () => {
271cb0ef41Sopenharmony_ci        if (dispatcher[kConnected] === 0 && dispatcher[kSize] === 0) {
281cb0ef41Sopenharmony_ci          this.finalizer(key)
291cb0ef41Sopenharmony_ci        }
301cb0ef41Sopenharmony_ci      })
311cb0ef41Sopenharmony_ci    }
321cb0ef41Sopenharmony_ci  }
331cb0ef41Sopenharmony_ci}
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_cimodule.exports = function () {
361cb0ef41Sopenharmony_ci  // FIXME: remove workaround when the Node bug is fixed
371cb0ef41Sopenharmony_ci  // https://github.com/nodejs/node/issues/49344#issuecomment-1741776308
381cb0ef41Sopenharmony_ci  if (process.env.NODE_V8_COVERAGE) {
391cb0ef41Sopenharmony_ci    return {
401cb0ef41Sopenharmony_ci      WeakRef: CompatWeakRef,
411cb0ef41Sopenharmony_ci      FinalizationRegistry: CompatFinalizer
421cb0ef41Sopenharmony_ci    }
431cb0ef41Sopenharmony_ci  }
441cb0ef41Sopenharmony_ci  return {
451cb0ef41Sopenharmony_ci    WeakRef: global.WeakRef || CompatWeakRef,
461cb0ef41Sopenharmony_ci    FinalizationRegistry: global.FinalizationRegistry || CompatFinalizer
471cb0ef41Sopenharmony_ci  }
481cb0ef41Sopenharmony_ci}
49