Weak References

Mark S. Miller erights at google.com
Mon Sep 7 18:29:58 UTC 2015


function rotateStreamHelper(weakRes, interval, format) {
  let timer = setInterval(() => setStream(weakRes.get(), format),
                          interval);
  weakRes.register(() => clearInterval(timer));
}

function rotateStream(interval, format) {
  const res = {};
  setStream(res, format);

  rotateStreamHelper(new WeakRef(res), interval, format);

  // Return a strong reference
  return res;
}


Given
http://wiki.ecmascript.org/doku.php?id=strawman:weak_references
does this implement your intention?



On Mon, Sep 7, 2015 at 10:53 AM, Isiah Meadows <isiahmeadows at gmail.com>
wrote:

> It's replacing the prototype of a stream periodically to point to a new
> writable file output stream pointing to a new file, but I want to kill the
> interval timer when the object is garbage collected.
>
> Something like this:
>
> 1. Create new stream.
> 2. Point it to a log file.
> 3. Every tick on a given interval, do this:
> 3.1. Change the destination of the stream to a new log file.
> 4. When the stream itself is GC'd, do this:
> 4.1. Clear the timer.
> 4.2. Clear all local references to both the timer and the stream.
> 5. Return the stream.
>
> The catch is that I don't want the stream strongly referenced in any local
> closures because it would end up never being collected - there would always
> be an active strong reference to it. I don't think a
> `Reflect.onOnlyOneReferenceLeft(obj, callback)` would make it into the
> language, so weak references are the best way to avoid this. And if I were
> doing this in Java or C++, I would most definitely use weak
> references/pointers for this use case. (This is out of the land of what
> automatic garbage collection can handle at this point.)
>
> On Mon, Sep 7, 2015, 13:33 Mark S. Miller <erights at google.com> wrote:
>
>> I also don't get the purpose of this code. What is it trying to achieve?
>>
>>


-- 
    Cheers,
    --MarkM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150907/6cfb189c/attachment.html>


More information about the es-discuss mailing list