Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Commit

Permalink
Editorial: Fix another IterableWeakMap leak
Browse files Browse the repository at this point in the history
Fixes: #213
  • Loading branch information
ExE-Boss committed Jan 10, 2021
1 parent 99c96cf commit 4b81e79
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 4b81e79

Please sign in to comment.