Permalink
Browse files

stream example now uses domains. Badassery

  • Loading branch information...
1 parent a3cac96 commit 5b91369c9060be67051241adcd6aae85cf2e89f6 @Raynos committed Oct 3, 2012
Showing with 47 additions and 70 deletions.
  1. +24 −36 README.md
  2. +23 −34 examples/stream.js
View
@@ -46,52 +46,40 @@ var pfour = pthree.then(function (v) {
one.end("one")
```
-## Same example using just streams
+## Same example using just streams and domains
``` js
var ReadWriteStream = require("read-write-stream")
, assert = require("assert")
+ , Domain = require("domain")
-var one = ReadWriteStream()
- , two = ReadWriteStream(function write(chunk, queue) {
- console.log("one", chunk)
- queue.push("two")
- })
- , three = ReadWriteStream(function write(chunk, queue) {
- console.log("two", chunk)
- queue.error("three")
- })
- , four = ReadWriteStream()
+var domain = Domain.create()
-connect([
- one.stream
- , two.stream
- , three.stream
- , four.stream
-]).on("error", function (e) {
- console.log("three", e)
-})
-
-// Flow data through one's queu
-one.end("one")
+domain.run(function () {
+ var one = ReadWriteStream()
+ , two = ReadWriteStream(function write(chunk, queue) {
+ console.log("one", chunk)
+ queue.push("two")
+ })
+ , three = ReadWriteStream(function write(chunk, queue) {
+ console.log("two", chunk)
+ queue.error("three")
+ })
+ , four = ReadWriteStream()
-// Helper to emulate error propagation functionality
-function connect(streams) {
- for (var i = 0; i < streams.length - 1; i++) {
- var curr = streams[i]
- , next = streams[i + 1]
+ one.stream
+ .pipe(two.stream)
+ .pipe(three.stream)
+ .pipe(four.stream)
- curr.pipe(next)
+ domain.on("error", function (err) {
+ console.log("three", err)
+ })
- // In an ideal world you just use domains.
- // None of this error propagation stuff.
- curr.on("error", function (err) {
- next.emit("error", err)
- })
- }
+ // Flow data through one's queu
+ one.end("one")
+})
- return next
-}
```
## Installation
View
@@ -1,41 +1,30 @@
var ReadWriteStream = require("read-write-stream")
, assert = require("assert")
+ , Domain = require("domain")
-var one = ReadWriteStream()
- , two = ReadWriteStream(function write(chunk, queue) {
- console.log("one", chunk)
- queue.push("two")
- })
- , three = ReadWriteStream(function write(chunk, queue) {
- console.log("two", chunk)
- queue.error("three")
- })
- , four = ReadWriteStream()
-
-connect([
- one.stream
- , two.stream
- , three.stream
- , four.stream
-]).on("error", function (e) {
- console.log("three", e)
-})
-
-// Flow data through one's queu
-one.end("one")
+var domain = Domain.create()
-// Helper to emulate error propagation functionality
-function connect(streams) {
- for (var i = 0; i < streams.length - 1; i++) {
- var curr = streams[i]
- , next = streams[i + 1]
+domain.run(function () {
+ var one = ReadWriteStream()
+ , two = ReadWriteStream(function write(chunk, queue) {
+ console.log("one", chunk)
+ queue.push("two")
+ })
+ , three = ReadWriteStream(function write(chunk, queue) {
+ console.log("two", chunk)
+ queue.error("three")
+ })
+ , four = ReadWriteStream()
- curr.pipe(next)
+ one.stream
+ .pipe(two.stream)
+ .pipe(three.stream)
+ .pipe(four.stream)
- curr.on("error", function (err) {
- next.emit("error", err)
- })
- }
+ domain.on("error", function (err) {
+ console.log("three", err)
+ })
- return next
-}
+ // Flow data through one's queu
+ one.end("one")
+})

0 comments on commit 5b91369

Please sign in to comment.