From 5b8887e60b09b8eda0d96f4a8e19005de5adf263 Mon Sep 17 00:00:00 2001 From: Vasiliy Loginevskiy Date: Fri, 27 Mar 2015 14:00:59 +0300 Subject: [PATCH] No need in Duplex stream. Rewrite to Readable. --- lib/index.js | 28 +++++++++++----------------- test/schemes/flat.test.js | 11 ++++++----- test/schemes/nested.test.js | 11 ++++++----- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/lib/index.js b/lib/index.js index a524a6a..a13fd37 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,13 +1,13 @@ var fs = require('fs'), path = require('path'), bemNaming = require('bem-naming'), - PassThrough = require('stream').PassThrough, + Readable = require('stream').Readable, schemes = require('./schemes'); module.exports = function (levels, options) { options || (options = {}); - var output = new PassThrough({ objectMode: true }), + var output = new Readable({ objectMode: true }), defaultScheme = options.scheme || schemes.nested, defaultNaming = options.naming ? bemNaming(options.naming) : bemNaming; @@ -27,7 +27,7 @@ module.exports = function (levels, options) { }); function add(obj) { - output.write(obj); + output.push(obj); } function scan(level, callback) { @@ -72,22 +72,16 @@ module.exports = function (levels, options) { } } - var n = 0, - l = levels.length; + var n = levels.length; - if (l === 0) { - output.end(); - } else { - for (var i = 0; i < l; ++i) { - scan(levels[i], scancb); - } - } + output._read = function () { + n || output.push(null); - function scancb(err) { - err && output.emit('error', err); - - ++n === l && output.end(); - } + levels.length && scan(levels.shift(), function (err) { + err && output.emit('error', err); + --n || output.push(null); + }); + }; return output; }; diff --git a/test/schemes/flat.test.js b/test/schemes/flat.test.js index ed773f0..0485420 100644 --- a/test/schemes/flat.test.js +++ b/test/schemes/flat.test.js @@ -22,11 +22,12 @@ describe('flat scheme', function () { it('must throw error if levels is not found', function (done) { var walker = walk(['not-existing-level']); - walker.on('error', function (err) { - err.must.throw(); - done(); - }) - .resume(); + walker + .on('error', function (err) { + err.must.throw(); + done(); + }) + .resume(); }); describe('ignore', function () { diff --git a/test/schemes/nested.test.js b/test/schemes/nested.test.js index 2d70f4c..d920aad 100644 --- a/test/schemes/nested.test.js +++ b/test/schemes/nested.test.js @@ -22,11 +22,12 @@ describe('nested scheme', function () { it('must throw error if levels is not found', function (done) { var walker = walk(['not-existing-level']); - walker.on('error', function (err) { - err.must.throw(); - done(); - }) - .resume(); + walker + .on('error', function (err) { + err.must.throw(); + done(); + }) + .resume(); }); describe('ignore', function () {