https://github.com/webreflection/weak-value
A Map with weakly referenced values, instead of keys
https://github.com/webreflection/weak-value
Last synced: over 1 year ago
JSON representation
A Map with weakly referenced values, instead of keys
- Host: GitHub
- URL: https://github.com/webreflection/weak-value
- Owner: WebReflection
- License: isc
- Created: 2020-07-01T15:27:22.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-27T13:59:41.000Z (over 3 years ago)
- Last Synced: 2025-04-12T03:38:13.101Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 17.6 KB
- Stars: 32
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# WeakValue
📣 Check out [not-so-weak](https://github.com/WebReflection/not-so-weak) module for better and faster `WValue` alternative
- - -
[](https://travis-ci.com/WebReflection/weak-value) [](https://coveralls.io/github/WebReflection/weak-value?branch=master)
A Map with weakly referenced values, instead of keys, with an optional *onValueCollected* callback to pass aslong.
In Node.js, it requires [V8 release v8.4](https://v8.dev/blog/v8-release-84) or greater.
```js
import WeakValue from 'weak-value';
// const WeakValue = require('weak-value');
const wv = new WeakValue;
(() => {
const value = {};
wv.set('any-key', value, /* optional */ (key, map) => {
// will log "any-key value collected" once GC kicks in
console.log(key, 'value collected');
console.log(map === wv); // true
});
})();
```
Please note that explicit `wv.delete(key)` will *NOT* invoke *onValueCollected*, as deleting a key does not mean its referenced value has been collected.