UNPKG

681 BJavaScriptView Raw
1const Streamz = require('streamz');
2const util = require('util');
3
4function KeepOpen(timeout) {
5 if (!(this instanceof Streamz))
6 return new KeepOpen(timeout);
7 Streamz.call(this);
8 this.timeout = timeout || 1000;
9}
10
11util.inherits(KeepOpen,Streamz);
12
13KeepOpen.prototype._fn = function() {
14 this.last = new Date();
15 return Streamz.prototype._fn.apply(this,arguments);
16};
17
18KeepOpen.prototype.end = function(d) {
19 if (d !== null && d !== undefined)
20 this.write(d);
21
22 let timer = setInterval(() => {
23 if (new Date() - this.last > this.timeout) {
24 clearInterval(timer);
25 Streamz.prototype.end.call(this);
26 }
27 },this.timeout);
28};
29
30module.exports = KeepOpen;
\No newline at end of file