Skip to content

Commit

Permalink
Exposed also a clear method to allow remote drivers to free the who…
Browse files Browse the repository at this point in the history
…le heap when needed
  • Loading branch information
WebReflection committed Sep 30, 2024
1 parent 96b2d75 commit 66c215c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 10 additions & 2 deletions esm/heap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ export const create = () => {
const values = new Map;
let uid = 0;
return {
/**
* Clear all references retained in the current heap.
*/
clear: () => {
ids.clear();
values.clear();
},

/**
* Remove by id or value any previously stored reference.
* @param {number | unknown} id the held value by id or the value itself.
Expand Down Expand Up @@ -48,5 +56,5 @@ export const create = () => {
};

// globally shared heap
const { drop, get, hold } = create();
export { drop, get, hold };
const { clear, drop, get, hold } = create();
export { clear, drop, get, hold };
6 changes: 5 additions & 1 deletion test/heap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { drop, get, hold } from '../esm/heap.js';
import { clear, drop, get, hold } from '../esm/heap.js';
import { assert, collect } from './utils.js';

let o = {};
Expand All @@ -25,3 +25,7 @@ assert(id !== hold(o), true);
assert(drop(o), true);
assert(drop(o), false);
assert(drop(id), false);

hold(o);
clear();
assert(drop(o), false);

0 comments on commit 66c215c

Please sign in to comment.