Skip to content

Commit

Permalink
Ensuring weakermap doesnt drop reassigned keys after delete or clear
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmorris committed Sep 20, 2024
1 parent 930b0e7 commit 27ddf94
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "weaker",
"version": "0.0.9",
"version": "0.0.10",
"description": "Weaker maps by value, and enumerable weaker sets",
"author": "Sean Morris",
"main": "index.mjs",
Expand Down
30 changes: 24 additions & 6 deletions weakermap/WeakerMap.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
export class WeakerMap
{
map = new Map;
const getRegistry = weakerMap => {
const registry = new FinalizationRegistry(key => {
if(weakerMap.registry !== registry)
{
return;
}

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

weakerMap.delete(key);
});

return registry;
};

export class WeakerMap
{
map = new Map;
registry = getRegistry(this);

constructor(entries)
{
entries && entries.forEach(([key, value]) => this.set(key, value));
Expand All @@ -22,11 +33,18 @@ export class WeakerMap

clear()
{
this.registry = getRegistry(this);
this.map.clear();
}

delete(key)
{
if(!this.has(key))
{
return;
}

this.registry.unregister(this.get(key));
this.map.delete(key);
}

Expand Down
2 changes: 1 addition & 1 deletion weakermap/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "weakermap",
"version": "0.0.9",
"version": "0.0.10",
"author": "Sean Morris",
"description": "A WeakerMap is an enumerable Map with with weak values rather than keys",
"main": "WeakerMap.mjs",
Expand Down
12 changes: 6 additions & 6 deletions weakerset/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion weakerset/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "weakerset",
"version": "0.0.9",
"version": "0.0.10",
"author": "Sean Morris",
"description": "A WeakerSet an enumerable WeakSet",
"main": "WeakerSet.mjs",
Expand Down

0 comments on commit 27ddf94

Please sign in to comment.