weakness
v1.0.2
Published
An iterable weak sequence.
Readme
weakness
Provides 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:
interface O {
x:number
}
let keeper = {}
const w = new Weakness<O>()
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 subsequently
use to remove the item from the weakness, via its delete
method:
const w = new Weakness<O>()
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.
