mirror of https://github.com/lukechilds/node.git
Browse Source
Adds simplified constructor pattern, allowing users to provide "read", "write", "transform", "flush", and "writev" functions as stream options in lieu of subclassing. Semver: minor PR-URL: https://github.com/iojs/io.js/pull/697 Fixes: https://github.com/iojs/readable-stream/issues/102 Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>v1.8.0-commit
Sam Newman
10 years ago
committed by
Chris Dickinson
7 changed files with 175 additions and 2 deletions
@ -0,0 +1,18 @@ |
|||
var common = require('../common'); |
|||
var assert = require('assert'); |
|||
|
|||
var Readable = require('stream').Readable; |
|||
|
|||
var _readCalled = false; |
|||
function _read(n) { |
|||
_readCalled = true; |
|||
this.push(null); |
|||
} |
|||
|
|||
var r = new Readable({ read: _read }); |
|||
r.resume(); |
|||
|
|||
process.on('exit', function () { |
|||
assert.equal(r._read, _read); |
|||
assert(_readCalled); |
|||
}); |
@ -0,0 +1,31 @@ |
|||
var common = require('../common'); |
|||
var assert = require('assert'); |
|||
|
|||
var Transform = require('stream').Transform; |
|||
|
|||
var _transformCalled = false; |
|||
function _transform(d, e, n) { |
|||
_transformCalled = true; |
|||
n(); |
|||
} |
|||
|
|||
var _flushCalled = false; |
|||
function _flush(n) { |
|||
_flushCalled = true; |
|||
n(); |
|||
} |
|||
|
|||
var t = new Transform({ |
|||
transform: _transform, |
|||
flush: _flush |
|||
}); |
|||
|
|||
t.end(new Buffer('blerg')); |
|||
t.resume(); |
|||
|
|||
process.on('exit', function () { |
|||
assert.equal(t._transform, _transform); |
|||
assert.equal(t._flush, _flush); |
|||
assert(_transformCalled); |
|||
assert(_flushCalled); |
|||
}); |
@ -0,0 +1,34 @@ |
|||
var common = require('../common'); |
|||
var assert = require('assert'); |
|||
|
|||
var Writable = require('stream').Writable; |
|||
|
|||
var _writeCalled = false; |
|||
function _write(d, e, n) { |
|||
_writeCalled = true; |
|||
} |
|||
|
|||
var w = new Writable({ write: _write }); |
|||
w.end(new Buffer('blerg')); |
|||
|
|||
var _writevCalled = false; |
|||
var dLength = 0; |
|||
function _writev(d, n) { |
|||
dLength = d.length; |
|||
_writevCalled = true; |
|||
} |
|||
|
|||
var w2 = new Writable({ writev: _writev }); |
|||
w2.cork(); |
|||
|
|||
w2.write(new Buffer('blerg')); |
|||
w2.write(new Buffer('blerg')); |
|||
w2.end(); |
|||
|
|||
process.on('exit', function () { |
|||
assert.equal(w._write, _write); |
|||
assert(_writeCalled); |
|||
assert.equal(w2._writev, _writev); |
|||
assert.equal(dLength, 2); |
|||
assert(_writevCalled); |
|||
}); |
Loading…
Reference in new issue