Hi, I'm unable to understand how exactly readablestream 'data' event works 
(I'm using node v0.11.13).

I have this simple example : 

var util = require('util'),
    stream = require("stream");

// A simple readable stream
var R = function(){
    stream.Readable.call(this,{objectMode : true});
    this.index = 1;
}
// R inherits from Readable
util.inherits(R, stream.Readable);

// @resume
// On Resume, starts a timer pushing {index : #} every second
// Fire readable after 1.5 second
// Clear interval after 5 seconds
// So basically, a consumer should receive about 5 messages 
R.prototype.resume= function(){
    console.log("R.resume");
    var self= this;
    this.timer = setInterval(this.pusher.bind(this), 1000);
    setTimeout(this.emit.bind(this, "readable"),1500);
    setTimeout(this.emit.bind(this, null), 5000);
    setTimeout(function(){clearInterval(self.timer)}, 5000);
}


// The function invoked by resume to push messages
R.prototype.pusher = function(){
    console.log("R.push");
    var msg = {index : this.index++};
       this.push(msg);
};

R.prototype._read= function(){
    var self= this;
    console.log("R._read");
}

R.prototype.pause= function(){
    console.log("R.pause");
    clearInterval(this.timer);
}
if (!module.parent){
    var r = new R();
    // This is never called ?!
    r.on("data", function(data){
        console.log("received data", data);
    });

  // This works but good luck to manage edge cases !
    //r.on('readable', function() {
    //    var chunk;
    //    while (null !== (msg = r.read())) {
    //        console.log('got %d bytes of data', msg);
    //    }
    //});
}


The 'data' listener never gets called.
The while loop works after 'readable' event gets fired but does not look 
like what doc says.
 
Any help very welcome !







-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/26167ec6-32cc-4a41-a44d-041b5fd41e69%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to