adaltas / node-stream-transform

Object transformations implementing the Node.js `stream.Transform` API
https://csv.js.org/transform/
49 stars 13 forks source link

finish event not emitted #16

Closed luissquall closed 8 years ago

luissquall commented 8 years ago

Calling transform callback multiple times doesn't emit second transform finish event. For instance:

var transform = require('stream-transform');
var parse = require('csv-parse');

var nums = [];
var parser = parse({delimiter: ','});
var t1 = transform(function tf(data, callback){
    for (index = 0; index < data.length; index++) {
        if (data[index] % 2 == 0) {
            callback(null, data[index]);
        }
    }
    // callback(null, 1);
});

var t2 = transform(function(data) {
    nums.push(data);
    console.log(data);
}).on('finish', function() {
    console.log(nums);
});

parser.write('1,2,3,4');
parser.end();

parser.pipe(t1).pipe(t2);

The output looks like this:

2
4

However, if we remove the for which breaks the 1-1 relationship between tf and callback, t2 finish event is emitted.

var t1 = transform(function tf(data, callback){
    callback(null, data[0]);
});

outputs:

1
[ '1' ]

Is it possible to make t2 finish event to be emitted? In the real example t1 lives inside a module.

wdavidw commented 8 years ago

before going any further, your code isnt correct in its usage. you should always call the callback and only once, the doc mention:

Skipping records is easily achieved by (..) passing null to the callback handler in asynchonous mode. Generating multiple records is only supported in asynchonous mode by providing n-arguments after the error argument instead of simply one.

luissquall commented 8 years ago

Thank you very much, problem solved.