Isiah Meadows (2015-09-07T17:19:51.000Z)
Resending this as a new message because of my apparently broken
WYSIWYDG (what you see is what you don't get) email client... :-(

Hopefully this doesn't itself get corrupted...

> 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)

(it got corrupted when saving somehow :-( )


On Sun, Sep 6, 2015 at 11:34 PM, Mark S. Miller <erights at google.com> wrote:
>
>
> On Sun, Sep 6, 2015 at 8:04 PM, Isiah Meadows <isiahmeadows at gmail.com>
> wrote:
>>
>> 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
>
> [...Somehow reformatted in transit...]
>
>>
>> ```
>
>
> Hi Isiah, the text in the above code block somehow got reformatted in
> transit. You can see the problem at
> https://mail.mozilla.org/pipermail/es-discuss/2015-September/044141.html
> https://esdiscuss.org/topic/weak-references#content-10
>
> Since the code below does not have the callback, I don't yet have enough
> context to understand your message. When the GC notifies that the stream is
> to be collected, by notifying some callback, why does that callback need to
> access the stream? What does the callback do with the stream during the
> notification that the stream is condemned?
>
> Is the interval callback somehow related to the GC notification callback? As
> you see, I just don't get it yet.
>>
>>
>> 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)
>>
>>
>
> --
>     Cheers,
>     --MarkM



-- 
Isiah Meadows
d at domenic.me (2015-09-15T21:24:58.619Z)
(re-post of previous message with edits, already incorporated into above)