Cleaning up streams with internal intervals

18 views
Skip to first unread message

Kristian Lyngbæk

unread,
Jun 18, 2015, 6:13:21 PM6/18/15
to nod...@googlegroups.com
I have subclassed a transform stream. As part of the constructor function I set up an interval. I need to make sure I clear this interval when the stream is done, so I don't have a memory leak. Where is the best place to do this?

I am considering using the end-of-stream module to detect when the stream instance is finished to trigger the clearTimeout. Is there any edge case I would miss by doing this?


var eos = require('end-of-stream');

function SomeTransformStream() {
   
Transform.call(this);

   
var self = this;
   
// on constructor, setup timeout
   
this._timeoutId = setTimeout(function () {
       
self.doSomething(); // some code I need called
   
});
   
// on end of stream destroy self
    eos
(this, function() {
       
// is this thorough enough?
        clearTimeout
(_self.timeoutId);
   
});

   
// ... some more stuff
}

Thanks in advance.
Reply all
Reply to author
Forward
0 new messages