question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pausing a stream() doesn’t seem to have an effect - The parser proceeds reading the CSV as usual.

Take for example the following code:

const rs = fs.createReadStream(this.csvPath);
rs.pause();

let count = 0;
csv()
.fromStream(rs)
.on("json", (json) => {
  count++;
  console.log(count);
})
.on("done", () => {
  cb(null, count);
})
.on("error", (err) => {
  cb(err);
})

count is logged 200 times (equal to the amount of rows in the CSV) - I was expecting it not to log anything since the stream is paused before passing it over to fromStream()

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
Keyangcommented, Jan 23, 2017

😄 As mentioned before, this is not correct way of doing things.

Node.js has encapsulated this in its stream implementation and we should not need to do this.

Implement your own Writable implementation is the correct way of doing this. see https://nodejs.org/api/stream.html#stream_simplified_construction

here is an example of what you want to achieve:


var tmpArr=[];
rs.pipe(csv({},{objectMode:true})).pipe(new Writable({
  write: function(json, encoding,callback){
    tmpArr.push(json);
    if (tmpArr.length===10000){
      myDb.save(tmpArr,function(){
        tmpArr=[];
        callback();
      })
    }else{
      callback();
    }
  } ,
  objectMode:true
}))
.on('finish',function(){
  if (tmpArr.length>0){
    myDb.save(tmpArr,function(){
      tmpArr=[];
    })
  }
})


~Keyang

On 23 January 2017 at 14:41, Nicholas Kyriakides notifications@github.com wrote:

Got it - thanks.

Are there any plans to make “pausing” a bit more easy? I’ve got it working with your suggestions, but it feels a tad hackish.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Keyang/node-csvtojson/issues/135#issuecomment-274504943, or mute the thread https://github.com/notifications/unsubscribe-auth/ABCs93cHFZCjsymuKhGc-qvVE3Dgh5zmks5rVLwagaJpZM4LqJLl .

0reactions
nicholaswmincommented, Jan 25, 2017

Nice - that looks much cleaner. Thanks again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pause a stream | Datastream | Google Cloud
Go to the Streams page in the Google Cloud Console. Go to the Streams page · Select the check box to the left...
Read more >
Can a Livestream Be Paused? - Camera Groove
Surely there's a way of pausing the live stream? ... Yes, a Livestream can be paused by pressing the 'pause' button within the...
Read more >
Does pausing a video help or hinder the buffering process?
Its is not possible to pause a live video, you can take an image and picture it on the profile so its appears...
Read more >
What is Stream Module pause() in Node.js? - Educative.io
We can use the readable.pause() method to stop the flow of data events. This method pauses the reading of data from a file....
Read more >
14.11. Pausing and Resuming a Net Stream - O'Reilly
You can use the NetStream.pause( ) method to pause the playback of a net stream. For example, if a net stream is playing,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found