Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have a NodeJS stream.Readable, ArticleReader. Its job is to read articles, Meantime I have to give it a date to signify the date of the articles, say the default date is today, now I start reading with .on('data') event, and the stream ends. Later I want to change the date and start reading yesterday's articles. In what way should I implement this stream.Readable so this is possible?

One way to think about this is to construct a new ArticleReader for each date. Is this a good approach or is there a better way?

Here's a sample implementation for reference:

var async = require('async'),
    util = require('util'),
    ReadableStream = require('readable-stream').Readable;

function ArticleReader(date) {
    ReadableStream.call(this, { objectMode: true });
    this.strategy = new BasicStrategy();
    this.date = date;
}

util.inherits(ArticleReader, ReadableStream);

ArticleReader.prototype._read = function() {
    var strategy = this.strategy, date = this.date;
    var articles = strategy.getArticleList(date);
    var self = this;
    async.each(articles, function(link, callback) {
        strategy.parseLink(link, function(article) {
            self.push(article);
            callback();
        });
    }, function(err) {
        if (err) {
            self.emit('error', err);
        } else {
            self.push(null);
        }
    });
};
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.