Skip to content

Instantly share code, notes, and snippets.

@eguneys
Last active August 29, 2015 14:08
Show Gist options
  • Save eguneys/99687def9ab17713c175 to your computer and use it in GitHub Desktop.
Save eguneys/99687def9ab17713c175 to your computer and use it in GitHub Desktop.
NodeJS Stream.Readable ArticleReader Question

I have a NodeJS stream.Readable, ArticleReader. It's 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 I should implement this stream.Readable so this is possible.

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

Here's a sample implemtation 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);
        }
    });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment