From 4b81e792d13dd5ce239fcb4dfbd5702b5c5efd62 Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Mon, 11 Jan 2021 00:50:00 +0100 Subject: [PATCH] =?UTF-8?q?Editorial:=20Fix=C2=A0another=20`IterableWeakMa?= =?UTF-8?q?p`=C2=A0leak?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://github.com/tc39/proposal-weakrefs/issues/213 --- README.md | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 5744f77..45a3541 100644 --- a/README.md +++ b/README.md @@ -243,26 +243,36 @@ class IterableWeakMap { set.delete(ref); } - constructor(iterable) { - for (const [key, value] of iterable) { - this.set(key, value); + constructor(iterable = null) { + if (iterable !== null) { + for (const { 0: key, 1: value } of iterable) { + this.set(key, value); + } } } set(key, value) { - const ref = new WeakRef(key); - - this.#weakMap.set(key, { value, ref }); - this.#refSet.add(ref); - this.#finalizationGroup.register(key, { - set: this.#refSet, - ref - }, ref); + const entry = this.#weakMap.get(key); + if (entry) { + entry.value = value; + } else { + const ref = new WeakRef(key); + + this.#weakMap.set(key, { value, ref }); + this.#refSet.add(ref); + this.#finalizationGroup.register(key, { + set: this.#refSet, + ref + }, ref); + } } get(key) { - const entry = this.#weakMap.get(key); - return entry && entry.value; + return this.#weakMap.get(key)?.value; + } + + has(key) { + return this.#weakMap.has(key); } delete(key) { @@ -291,13 +301,13 @@ class IterableWeakMap { } *keys() { - for (const [key, value] of this) { + for (const { 0: key } of this) { yield key; } } *values() { - for (const [key, value] of this) { + for (const { 1: value } of this) { yield value; } }