Created
November 12, 2010 00:34
-
-
Save bobpoekert/673518 to your computer and use it in GitHub Desktop.
tail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tail = function(fname, onData, ender) { | |
fs.open(fname, 'a+', function(err, fd) { | |
if (err) { | |
puts('error!: '+err); | |
if (ender(fd, 0) !== false) { | |
setTimeout(function(){ | |
tail(fname, onData); | |
}, 10); | |
} | |
} else { | |
puts('allocating buffer'); | |
var size = 1024; | |
var buffer = new Buffer(size); | |
var pos = 0; | |
var reader = function() { | |
fs.read(fd, buffer, 0, size, pos, function(err, bytesRead) { | |
if (bytesRead > 0) { | |
pos += bytesRead; | |
onData(buffer.slice(0, bytesRead).toString('utf-8'), fd); | |
} | |
if (ender && ender(fd, pos) === false) { | |
fs.close(fd); | |
} else { | |
if (bytesRead == 0) { | |
setTimeout(reader, 10); | |
} else { | |
reader(); | |
} | |
} | |
}); | |
}; | |
reader(); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment