An open API service indexing awesome lists of open source software.

https://github.com/webreflection/not-so-weak

Iterable WeakMap, WeakSet and WeakValue
https://github.com/webreflection/not-so-weak

Last synced: over 1 year ago
JSON representation

Iterable WeakMap, WeakSet and WeakValue

Awesome Lists containing this project

README

          

# Not So Weak

[![build status](https://github.com/WebReflection/not-so-weak/actions/workflows/node.js.yml/badge.svg)](https://github.com/WebReflection/not-so-weak/actions) [![Coverage Status](https://coveralls.io/repos/github/WebReflection/not-so-weak/badge.svg?branch=main)](https://coveralls.io/github/WebReflection/not-so-weak?branch=main)

**Social Media Photo by [Pete Nuij](https://unsplash.com/@pete_nuij) on [Unsplash](https://unsplash.com/)**

Iterable [WeakMap](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap) (*WKey*) and [WeakSet](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet) (*WSet*) through [FinalizationRegistry](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry) and [WeakRef](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef) primitives, reimplementing also the [WeakValue](https://github.com/WebReflection/weak-value#readme) (*WValue*) module, including the optional *callback* for collected values.

```js
// const {WSet, WKey, WValue} = require('not-so-weak');
import {WSet, WKey, WValue} from 'not-so-weak';

// class WSet extends WeakSet implements Set {}
// class WKey extends WeakMap implements Map {}
// class WValue extends Map {}

// node --expose-gc example
const ws = new WSet([{}]);
const wm = new WKey([[{}, 'value']]);

const wv = new WValue;
wv.set('value', {}, function (key) {
console.assert(this === wv);
console.assert(key === 'value');
console.log(key, 'value collected');
});

console.assert(ws.size === 1);
console.assert(wm.size === 1);
console.assert([...wm.values()][0] === 'value');
console.assert([...wv.keys()][0] === 'value');

setTimeout(() => {
gc();
console.assert(ws.size === 0);
console.assert(wm.size === 0);
console.assert(wv.size === 0);
});
```

### Suitable For

* Weak key/value based state/store
* Server Side related tasks that can't bother with manual removal of weakly referenced entries
* every case where you end up swapping to `Map` or `Set` because you realize you cannot iterate over their *Weak* counterpart
* every case where you think there's a memory leak due possibly missing `weakThing.delete(ref)` operations

### Not Suitable For

* raw performance or benchmarks against `Map` or `Set`
* every case where `weakThing.delete(ref)` is already handled by the library or framework logic