So, I've been playing with this a bit further and just thought I'd make one
more comment for the sake of posterity. Here's the protip:

Don't ever call stream.push before you've registered your "readable"
listeners. If you do, you'll probably miss the "readable" event.

In my example I was calling stream.push from inside the constructor, but
that in turn triggers stream.read(0) which triggers the "readable" event
which you'll miss if you're not already listening for it. In the case of a
constructor, you don't have anything to call "addListener" on yet, so
you'll most definitely miss it.

Took me a while to get to the bottom of that. Thanks so much Nathan for
your help.

--
Michael Jackson
@mjackson


On Mon, Mar 11, 2013 at 4:41 PM, Nathan Rajlich <[email protected]>wrote:

> You don't `return` the chunk in the _read() function... you call
> `this.push(chunk)` instead.
>
>
> On Monday, March 11, 2013 2:57:14 PM UTC-7, Michael Jackson wrote:
>>
>> I've been messing around with 0.10's Stream.Readable this morning trying
>> to get a super simple example to work, but I can't get my subclass to emit
>> any "readable" events. Here's the subclass I'm using:
>>
>>     function Content(string, options) {
>>       Stream.Readable.call(this, options);
>>       this.push(string);
>>     }
>>
>>     util.inherits(Content, Stream.Readable);
>>
>>     Content.prototype._read = function (size) {
>>       // Ignore size argument and return the entire buffer, for now.
>>       var state = this._readableState;
>>       var chunk = Buffer.concat(state.buffer);
>>       state.buffer = [];
>>       return chunk;
>>     };
>>
>> All I'm trying to do is wrap a string with a Stream.Readable subclass and
>> emit that string. My _read function ignores the size argument for the sake
>> of simplicity.
>>
>> Here's my test case:
>>
>>     var test = new Content('hello world');
>>     // console.log(test._**readableState.buffer.map(**function (o) {
>> return o.toString() }))
>>
>>     test.on('readable', function () {
>>       console.log('readable');
>>       console.log(test.read());
>>     });
>>
>>     test.on('error', function (error) {
>>       console.log(error);
>>     });
>>
>>     test.on('end', function () {
>>       console.log('end');
>>     });
>>
>> When I run this code, I don't get anything printed to the console. Not
>> even if I try a test.read(0) afterwards to refresh the internal buffer.
>>
>> I know there's stuff in the internal buffer because when I run the line
>> that's commented out it shows me that my string is there.
>>
>> What am I doing wrong? Thanks in advance.
>>
>> --
>> Michael Jackson
>> @mjackson
>>
>  --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> 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 post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> 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].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
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 post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
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].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to