pkrumins / node-lazy

lazy lists for node.js
http://www.catonmat.net
488 stars 52 forks source link

.lines skips last unterminated line #43

Open sehrgut opened 11 years ago

sehrgut commented 11 years ago
var Lazy = require('lazy');
var l = Lazy();
l.lines.join(function (x) { console.log(x); });
l.emit('data', 'abc\ndef');
l.emit('end');

Expected output:

[ <Buffer 61 62 63>, <Buffer 64 65 66> ]

Actual output:

[ <Buffer 61 62 63> ]
sehrgut commented 11 years ago

This appears to have something to do with the relationship between how join is triggered, and how lines emits its final buffer. Here's a bit of sample code, and its output:

var Lazy = require('./lazy');
var l = new Lazy();
l.lines.join(function (x) { console.log(x); });
l.emit('data', 'abcdef');
l.emit('end');

I put logging lines at various points, which you'll see.

pushing last line
bucket emitting pipe event
join received pipe event
[]
bucket yielding final buffer on end event: "abcdef"

Those logging lines are:

  1. lines pushing the last bit of data
  2. bucket receiving and delegating the "pipe" event to its internal Lazy
  3. join receiving the "pipe" event from bucket and calling its callback
  4. My callback, printing an empty array.
  5. bucket getting the "end" event and yielding the final buffer, too late for join, and the contents of that buffer.

I hope this is helpful in resolving the issue. I wasn't able to figure out a satisfactory solution myself.