| 39 | // Can't delete when weakref count reaches 0 as it could increment again. |
| 40 | // Only GC can be used as a valid time to clean up the channels map. |
| 41 | class WeakRefMap extends SafeMap { |
| 42 | #finalizers = new SafeFinalizationRegistry((key) => { |
| 43 | // Check that the key doesn't have any value before deleting, as the WeakRef for the key |
| 44 | // may have been replaced since finalization callbacks aren't synchronous with GC. |
| 45 | if (!this.has(key)) this.delete(key); |
| 46 | }); |
| 47 | |
| 48 | set(key, value) { |
| 49 | this.#finalizers.register(value, key); |
| 50 | return super.set(key, new WeakReference(value)); |
| 51 | } |
| 52 | |
| 53 | get(key) { |
| 54 | return super.get(key)?.get(); |
| 55 | } |
| 56 | |
| 57 | has(key) { |
| 58 | return !!this.get(key); |
| 59 | } |
| 60 | |
| 61 | incRef(key) { |
| 62 | return super.get(key)?.incRef(); |
| 63 | } |
| 64 | |
| 65 | decRef(key) { |
| 66 | return super.get(key)?.decRef(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | function markActive(channel) { |
| 71 | // eslint-disable-next-line no-use-before-define |