mirror of
https://github.com/thangisme/notes.git
synced 2025-11-23 13:12:25 -05:00
Initial commit
This commit is contained in:
49
node_modules/duplexer2/example.js
generated
vendored
Executable file
49
node_modules/duplexer2/example.js
generated
vendored
Executable file
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
var stream = require("readable-stream");
|
||||
|
||||
var duplexer2 = require("./");
|
||||
|
||||
var writable = new stream.Writable({objectMode: true}),
|
||||
readable = new stream.Readable({objectMode: true});
|
||||
|
||||
writable._write = function _write(input, encoding, done) {
|
||||
if (readable.push(input)) {
|
||||
return done();
|
||||
} else {
|
||||
readable.once("drain", done);
|
||||
}
|
||||
};
|
||||
|
||||
readable._read = function _read(n) {
|
||||
// no-op
|
||||
};
|
||||
|
||||
// simulate the readable thing closing after a bit
|
||||
writable.once("finish", function() {
|
||||
setTimeout(function() {
|
||||
readable.push(null);
|
||||
}, 500);
|
||||
});
|
||||
|
||||
var duplex = duplexer2(writable, readable);
|
||||
|
||||
duplex.on("data", function(e) {
|
||||
console.log("got data", JSON.stringify(e));
|
||||
});
|
||||
|
||||
duplex.on("finish", function() {
|
||||
console.log("got finish event");
|
||||
});
|
||||
|
||||
duplex.on("end", function() {
|
||||
console.log("got end event");
|
||||
});
|
||||
|
||||
duplex.write("oh, hi there", function() {
|
||||
console.log("finished writing");
|
||||
});
|
||||
|
||||
duplex.end(function() {
|
||||
console.log("finished ending");
|
||||
});
|
||||
Reference in New Issue
Block a user