weak-id
v0.2.0
Published
A utility to generate unique ids for weak references
Maintainers
Readme
weak-id
Social Media Photo by Mario Amé on Unsplash
A utility to generate unique ids for weak references.
import weakID from 'weak-id';
const wid = weakID(id => {
console.log(id, 'collected');
});
let obj = {};
let [id, unknown] = wid(obj);
console.assert(unknown);
console.assert(id === wid(obj)[0]);
console.assert(!wid(obj)[1]);
obj = null;
// the log will happenTo have a reliable i32 value and ensure the next id will be actually a 32bit integer, you can also directly import the i32 variant:
import i32 from 'weak-id/i32';
// initialize a function that
// will always return a 32bit integer
const next = i32();
// i32(42) to start from 42 as initial value
console.assert(next() === 0);
console.assert(next() === 1);
// ...