Skip to content

Commit 0677dc8

Browse files
committed
implement realtime stream
1 parent 3a3d2b1 commit 0677dc8

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

index.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,31 @@ module.exports = function(filename, opts) {
2424
var fd = opts && opts.fd
2525
var ended, closeNext, busy, _cb;
2626
var _buffer = new Buffer(bufferSize)
27+
var live = !!opts.live
28+
var _cb
29+
30+
if(live) {
31+
fs.watch(filename, {
32+
persistent: opts.persistent !== false,
33+
},
34+
function (event, filename, stat) {
35+
if(_cb) {
36+
var cb = _cb
37+
_cb = null
38+
closeNext = false
39+
readNext(cb)
40+
}
41+
})
42+
43+
}
2744

2845
var flags = opts && opts.flags || 'r'
2946

3047
function readNext(cb) {
31-
if(closeNext) return close(cb)
48+
if(closeNext) {
49+
if(!live) return close(cb)
50+
else return _cb = cb
51+
}
3252
var toRead = Math.min(end - start, bufferSize);
3353
busy = true
3454

@@ -42,7 +62,7 @@ module.exports = function(filename, opts) {
4262
busy = false
4363
start += count;
4464
// if we have received an end noticiation, just discard this data
45-
if(closeNext) {
65+
if(closeNext && !live) {
4666
close(_cb)
4767
return cb(closeNext)
4868
}
@@ -139,12 +159,3 @@ module.exports = function(filename, opts) {
139159

140160
};
141161

142-
143-
144-
145-
146-
147-
148-
149-
150-

0 commit comments

Comments
 (0)