Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the callback to return the stream asynchronously + update readable-stream to v3 #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
npm-debug.log
node_modules/
test/tmp/
.idea
86 changes: 55 additions & 31 deletions lib/lazystream.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,78 @@
var util = require('util');
var PassThrough = require('readable-stream/passthrough');
var util = require('util')
var PassThrough = require('readable-stream').PassThrough

module.exports = {
Readable: Readable,
Writable: Writable
};
}

util.inherits(Readable, PassThrough);
util.inherits(Writable, PassThrough);
util.inherits(Readable, PassThrough)
util.inherits(Writable, PassThrough)

// Patch the given method of instance so that the callback
// is executed once, before the actual method is called the
// first time.
function beforeFirstCall(instance, method, callback) {
instance[method] = function() {
delete instance[method];
callback.apply(this, arguments);
return this[method].apply(this, arguments);
};
function beforeFirstCall (instance, method, callback) {
instance[method] = function () {
delete instance[method]
callback.apply(this, arguments)
return this[method].apply(this, arguments)
}
}

function Readable(fn, options) {
function Readable (fn, options) {
if (!(this instanceof Readable))
return new Readable(fn, options);
return new Readable(fn, options)

PassThrough.call(this, options)

PassThrough.call(this, options);
beforeFirstCall(this, '_read', function () {
var self = this
var lazy
try {
lazy = fn.call(self, options)
} catch (err) {
self.emit('error', err)
return
}

beforeFirstCall(this, '_read', function() {
var source = fn.call(this, options);
var emit = this.emit.bind(this, 'error');
source.on('error', emit);
source.pipe(this);
});
Promise.resolve(lazy)
.then(function (source) {
source.on('error', self.emit.bind(self, 'error'))
source.pipe(self)
}, function (err) {
self.emit('error', err)
})
})

this.emit('readable');
this.emit('readable')
}

function Writable(fn, options) {
function Writable (fn, options) {
if (!(this instanceof Writable))
return new Writable(fn, options);
return new Writable(fn, options)

PassThrough.call(this, options)

PassThrough.call(this, options);
beforeFirstCall(this, '_write', function () {
var self = this
var lazy
try {
lazy = fn.call(self, options)
} catch (err) {
self.emit('error', err)
return
}

beforeFirstCall(this, '_write', function() {
var destination = fn.call(this, options);
var emit = this.emit.bind(this, 'error');
destination.on('error', emit);
this.pipe(destination);
});
Promise.resolve(lazy)
.then(function (destination) {
destination.on('error', self.emit.bind(self, 'error'))
self.pipe(destination)
}, function (err) {
self.emit('error', err)
})
})

this.emit('writable');
this.emit('writable')
}

Loading