New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use wait? #29

Closed
legomind opened this Issue May 3, 2013 · 10 comments

Comments

Projects
None yet
4 participants
@legomind

legomind commented May 3, 2013

I tried:

  rs = fs.create-read-stream in-file
  ws = fs.create-write-stream out-file

  es.pipe rs,                                 # load inventory file
    es.replace //^[\r\n]+|\.|[\r\n]+$//g, ''  # remove leading and trailing newlines
    es.split '\r'                             # seperate records
    to-obj in-columns                         # convert record to object
    transform!                                # add records, change as needed
    to-csv out-columns                        # convert back to comma seperated values
    es.replace //\u001d//g, ''                # remove unicode group separator
    es.replace //^[\r\n]+|\.|[\r\n]+$//g, ''  # remove leading and trailing newlines
    ws                                        # write to new file
    es.wait ->
      console.log 'done'

The file writes fine, but console.log never gets called. I know this must be something simple, but it escapes me.

@contra

This comment has been minimized.

contra commented May 3, 2013

Can you compile this to JS?

@legomind

This comment has been minimized.

legomind commented May 3, 2013

var rs, ws;
rs = fs.createReadStream(inFile);
ws = fs.createWriteStream(outFile);
es.pipe( rs, 
  es.replace(/^[\r\n]+|\.|[\r\n]+$/g, ''), 
  es.split('\r'), 
  toObj(inColumns), 
  transform(),
  toCsv(outColumns), 
  es.replace(/\u001d/g, ''), 
  es.replace(/^[\r\n]+|\.|[\r\n]+$/g, ''), 
  ws, 
  es.wait(function(){
    console.log('done');
  })
);
@dominictarr

This comment has been minimized.

Owner

dominictarr commented May 3, 2013

the problem is that ws doesn't have any output. so it never emits end.
this should probably throw if you have a one-way stream in a position other than the first or last slot.

@dominictarr

This comment has been minimized.

Owner

dominictarr commented May 3, 2013

ws.on('close', function () {console.log('done')}) should work.

@legomind

This comment has been minimized.

legomind commented May 3, 2013

@dominictarr unfortunately, your suggestion ws.on('close', function () {console.log('done')}); did not work.
however, I did shift the wait function in the pipeline just before the write-stream, and the event fires. inside the callback I used a setTimeout statement to wait for the file to close. Is that acceptable?

@timoxley

This comment has been minimized.

timoxley commented May 3, 2013

@legomind this should work:

es.pipe(rs,
  es.replace(/^[\r\n]+|\.|[\r\n]+$/g, ''),
  // etc
  ws
).on('close', function() {
  console.log('done')
})

I used a setTimeout statement to wait for the file to close. Is that acceptable?

Doesn't actually guarantee the file has closed. Is that acceptable?

FYI having the write stream before the end throws in Node 0.10.x:

es.pipe(rs,
  es.replace(/^[\r\n]+|\.|[\r\n]+$/g, ''),
  // etc
  ws,
  es.wait(function() {console.log('done')})
) // => Error: Cannot pipe. Not readable.
@dominictarr

This comment has been minimized.

Owner

dominictarr commented May 4, 2013

It's time like this you have to read the code.

in 0.8 all streams emitted close.
but, it 0.10... some streams emit close, and now fs.WriteStream emits 'finish' instead...

I'm not sure why that is a good idea, but:

https://github.com/joyent/node/blob/master/lib/fs.js#L1632

there you go.

@legomind

This comment has been minimized.

legomind commented May 4, 2013

@dominictarr We have a winner! Thanks. I knew it had to be something simple like that! ws.on('finish', function(){...}) did the trick!

@timoxley I put thees.wait() call before the writeStream, so no error presented itself.

Perhaps es.wait should also attach to the finish event as well as close

@legomind legomind closed this May 4, 2013

@dominictarr

This comment has been minimized.

Owner

dominictarr commented May 4, 2013

hmm, actually, reading the code further still, it looks like close should have worked...
I think this may be a bug in streams2.

@dominictarr

This comment has been minimized.

Owner

dominictarr commented May 4, 2013

@legomind can you please post all the code I need to run this, and reproduce?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment