Isiah Meadows (2015-09-07T03:04:01.000Z)
d at domenic.me (2015-09-15T21:23:54.776Z)
My original email featured a use case, although I had to rewrite it to use explicit garbage collection. I need a way to watch for when the object is garbage collected. ```js function rotateStream(interval, format) { const res = {} setStream(res, format) let stream = new WeakReference(res) , format) let timer = setInterval(() => { setStream(stream.get(), format) }, interval) stream.onCollect(() => { // reference is dead, clear this timer clearTimer(timer) // break local strong references timer = stream = null }) // Return a strong reference return res } ``` The complicating part is that I also need the interval callback to weakly reference the value. Otherwise, I've always got one active strong reference to the stream. This is with a listener for when the value is garbage collected. Here's the version without a listener (it's simpler, but more crude): ```js function rotateStream(interval, format) { const res = {} setStream(res, format) let stream = new WeakReference(res) setInterval(function () { try { setStream(stream.get(), format) } catch (e) { // reference is dead, clear this timer clearInterval(this) // break strong reference to weak one stream = null } }, interval) // Return a strong reference return res } ``` Sorry for the ambiguity. (edited out a few bugs from the original)