You have overridden the resume method. streams in 0.11 are in paused mode,
that's why 'data' event is never fired. resume would put the stream into
floating mode, where data-events happen, but your resume doesn't call the
original one.
tbh, your implementation looks pretty adventurous to me. what exactly do
you want to do? the readable stream should do it's work in the _read
method, nowhere else. your implementation makes the stream logic obsolete,
and probably it would be easier for you to go with a simple event emitter
instance.
Am Montag, 4. August 2014 14:46:42 UTC+2 schrieb manimal45:
>
> 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/188e9c71-b3c8-4fae-9276-797c302fb5b1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.