dispatcher-weakref.js 761 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict'
  2. /* istanbul ignore file: only for Node 12 */
  3. const { kConnected, kSize } = require('../core/symbols')
  4. class CompatWeakRef {
  5. constructor (value) {
  6. this.value = value
  7. }
  8. deref () {
  9. return this.value[kConnected] === 0 && this.value[kSize] === 0
  10. ? undefined
  11. : this.value
  12. }
  13. }
  14. class CompatFinalizer {
  15. constructor (finalizer) {
  16. this.finalizer = finalizer
  17. }
  18. register (dispatcher, key) {
  19. dispatcher.on('disconnect', () => {
  20. if (dispatcher[kConnected] === 0 && dispatcher[kSize] === 0) {
  21. this.finalizer(key)
  22. }
  23. })
  24. }
  25. }
  26. module.exports = function () {
  27. return {
  28. WeakRef: global.WeakRef || CompatWeakRef,
  29. FinalizationRegistry: global.FinalizationRegistry || CompatFinalizer
  30. }
  31. }