1 | const Streamz = require('streamz');
|
2 | const util = require('util');
|
3 |
|
4 | function KeepOpen(timeout) {
|
5 | if (!(this instanceof Streamz))
|
6 | return new KeepOpen(timeout);
|
7 | Streamz.call(this);
|
8 | this.timeout = timeout || 1000;
|
9 | }
|
10 |
|
11 | util.inherits(KeepOpen,Streamz);
|
12 |
|
13 | KeepOpen.prototype._fn = function() {
|
14 | this.last = new Date();
|
15 | return Streamz.prototype._fn.apply(this,arguments);
|
16 | };
|
17 |
|
18 | KeepOpen.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 |
|
30 | module.exports = KeepOpen; |
\ | No newline at end of file |