From 53fb22495785a7de2319ec228319049be4139466 Mon Sep 17 00:00:00 2001 From: Timur Sevimli Date: Sat, 17 May 2025 00:49:37 +0300 Subject: [PATCH] Add WeakSet to prevent double dispose --- JavaScript/7-ref-count.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/JavaScript/7-ref-count.js b/JavaScript/7-ref-count.js index 4fea052..78005a6 100644 --- a/JavaScript/7-ref-count.js +++ b/JavaScript/7-ref-count.js @@ -9,6 +9,7 @@ class RefCount { #dispose = null; #context = null; #count = 0; + #freed = new WeakSet(); constructor(create, dispose) { this.#dispose = dispose; @@ -27,6 +28,8 @@ class RefCount { this.#count++; const disposable = Object.create(this.#resource); disposable[Symbol.asyncDispose] = async () => { + if (this.#freed.has(disposable)) return; + this.#freed.add(disposable); console.log('👉 Dispose'); this.#count--; if (this.#count > 0) return;