An iterable weak sequence.
npm install weaknessProvides an iterable weak sequence.
When adding elements to the weakness, you must specify
a "keeper" object. The keeper will maintain a strong
reference to the element. When the keeper is garbage
collected, and there are no other strong references to
the element, the element will be automatically removed
from the weakness.
A contrived example:
``TypeScript
interface O {
x:number
}
let keeper = {}
const w = new Weakness
w.add(keeper, {x:0})
w.add(keeper, {x:1})
w.add(keeper, {x:2})
console.log([...w]) // [{x:0}, {x:1}, {x:2}]
keeper = undefined
// wait for garbage collection
console.log([...w]) // []
`
The add method returns a number that you can subsequentlydelete
use to remove the item from the weakness, via its
method:
`TypeScript``
const w = new Weakness
const keeper = {}
const n = w.add(keeper, {x:0})
console.log([...w]) // [{x:0}]
w.delete(n)
console.log([...w]) // []
That's it. This is useful for signalling libraries, to
prevent consumers from having to track and clean up
their signal usage.