Skip to content

Commit

Permalink
Typos and race conditions.
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmorris committed Sep 18, 2024
1 parent c8b9f3d commit c317111
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
20 changes: 17 additions & 3 deletions weakermap/WeakerMap.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
export class WeakerMap
{
registry = new FinalizationRegistry(held => this.delete(held));
map = new Map;

registry = new FinalizationRegistry(key => {
if(this.map.has(key) && this.map.get(key).deref())
{
return;
}
this.delete(key);
});

constructor(entries)
{
entries && entries.forEach(([key, value]) => this.set(key, value));
Expand Down Expand Up @@ -75,7 +82,14 @@ export class WeakerMap
return;
}

return this.map.get(key).deref();
const value = this.map.get(key).deref();

if(!value)
{
this.map.delete(key);
}

return value;
}

has(key)
Expand Down Expand Up @@ -123,4 +137,4 @@ export class WeakerMap
}
};

Object.defineProperty(WeakerMap, Symbol.species, WeakerMap);
Object.defineProperty(WeakerMap, Symbol.species, {value: WeakerMap});
2 changes: 1 addition & 1 deletion weakerset/WeakerSet.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,4 @@ export class WeakerSet
}
};

Object.defineProperty(WeakerMap, Symbol.species, WeakerMap);
Object.defineProperty(WeakerSet, Symbol.species, {value: WeakerSet});

0 comments on commit c317111

Please sign in to comment.