Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Move reducers pipe here.

  • Loading branch information...
commit f38f6e6299df361b7e08a15d8bf93ce82a3d8432 1 parent 7519afd
Irakli Gozalishvili authored November 24, 2012

Showing 2 changed files with 73 additions and 0 deletions. Show diff stats Hide diff stats

  1. 20  pipe.js
  2. 53  test/pipe.js
20  pipe.js
... ...
@@ -0,0 +1,20 @@
  1
+"use strict";
  2
+
  3
+var send = require("./send")
  4
+var reduce = require("reducible/reduce")
  5
+
  6
+function pipe(input, output) {
  7
+  /**
  8
+  Takes reducible `input` and pipes it to the `output` (which is anything
  9
+  that implements `send`). Note that first `end` or `error`  from the piped
  10
+  `input`-s will end an `output` causing subsequent `send`s return `reducers`
  11
+  stopping other `input`-s. If you need to `pipe` all values form multiple
  12
+  inputs do `pipe(merge(inputs), output)`, that way `output` will close only
  13
+  once all inputs end. If you can't merge all the inputs up front you can
  14
+  always pipe merged event. That way sending new inputs to that event will
  15
+  automatically pipe all it's items.
  16
+  **/
  17
+  reduce(input, function pipeReducible(value) { send(output, value) })
  18
+}
  19
+
  20
+module.exports = pipe
53  test/pipe.js
... ...
@@ -0,0 +1,53 @@
  1
+"use strict";
  2
+
  3
+var pipe = require("../pipe")
  4
+var event = require("../event")
  5
+var send = require("../send")
  6
+
  7
+var test = require("reducers/test/util/test")
  8
+var lazy = require("reducers/test/util/lazy")
  9
+
  10
+var concat = require("reducers/concat")
  11
+var into = require("reducers/into")
  12
+var delay = require("reducers/delay")
  13
+var merge = require("reducers/merge")
  14
+
  15
+
  16
+var cache = require("cache-reduce")
  17
+
  18
+
  19
+exports["test pipe multiple streams"] = test(function(assert) {
  20
+  var s = event()
  21
+  var actual = into(s)
  22
+  pipe([1, 2, 3], s)
  23
+  pipe([4, 5, 6], s)
  24
+
  25
+
  26
+  assert(actual, [1, 2, 3],
  27
+         "first end causes close on output all subsequent pipes are ignored")
  28
+})
  29
+
  30
+exports["test pipe multiple streams indepenently"] = test(function(assert) {
  31
+  var s = event()
  32
+  var actual = into(s)
  33
+  pipe(delay([1, 2, 3]), s)
  34
+  pipe(delay([4, 5, 6, 7]), s)
  35
+
  36
+  // note that `6` goes through since `end` is also dispatched with a delay.
  37
+  assert(actual, [1, 4, 2, 5, 3, 6],
  38
+         "parallel pipe works until first end")
  39
+})
  40
+
  41
+exports["test pipe multiple streams"] = test(function(assert) {
  42
+  var s = event()
  43
+  var actual = into(s)
  44
+
  45
+  pipe(merge([[1, 2, 3], [4, 5], [ 6, 7 ]]), s)
  46
+
  47
+  // note that `6` goes through since `end` is also dispatched with a delay.
  48
+  assert(actual, [1, 2, 3, 4, 5, 6, 7],
  49
+         "parallel pipe works until first end")
  50
+})
  51
+
  52
+if (require.main === module)
  53
+  require("test").run(exports)

0 notes on commit f38f6e6

Please sign in to comment.
Something went wrong with that request. Please try again.