Nav apraksta

example.js 988B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env node
  2. var stream = require("readable-stream");
  3. var duplexer2 = require("./");
  4. var writable = new stream.Writable({objectMode: true}),
  5. readable = new stream.Readable({objectMode: true});
  6. writable._write = function _write(input, encoding, done) {
  7. if (readable.push(input)) {
  8. return done();
  9. } else {
  10. readable.once("drain", done);
  11. }
  12. };
  13. readable._read = function _read(n) {
  14. // no-op
  15. };
  16. // simulate the readable thing closing after a bit
  17. writable.once("finish", function() {
  18. setTimeout(function() {
  19. readable.push(null);
  20. }, 500);
  21. });
  22. var duplex = duplexer2(writable, readable);
  23. duplex.on("data", function(e) {
  24. console.log("got data", JSON.stringify(e));
  25. });
  26. duplex.on("finish", function() {
  27. console.log("got finish event");
  28. });
  29. duplex.on("end", function() {
  30. console.log("got end event");
  31. });
  32. duplex.write("oh, hi there", function() {
  33. console.log("finished writing");
  34. });
  35. duplex.end(function() {
  36. console.log("finished ending");
  37. });