pull-switch
Advanced tools
+14
-5
@@ -1,2 +0,2 @@ | ||
| 'use strict'; | ||
| var pull = require('pull-stream') | ||
@@ -8,11 +8,10 @@ var pushable = require('pull-pushable') | ||
| return pull.drain(function (item) { | ||
| var sink = pull.drain(function (item) { | ||
| var key = select(item) | ||
| if(!streams[key]) { | ||
| streams[key] = pushable(function (err) { | ||
| delete streams[key] | ||
| if(--n) return | ||
| --n | ||
| }) | ||
| streams[key].pipe(createStream(item)) | ||
| pull(streams[key], createStream(key)) | ||
| } | ||
@@ -25,2 +24,12 @@ streams[key].push(item) | ||
| }) | ||
| sink.add = function (key, subsink) { | ||
| streams[key] = pushable(function (err) { | ||
| delete streams[key] | ||
| --n | ||
| }) | ||
| pull(streams[key], subsink) | ||
| } | ||
| return sink | ||
| } |
+2
-2
| { | ||
| "name": "pull-switch", | ||
| "description": "split a pull-stream into many streams", | ||
| "version": "2.0.0", | ||
| "version": "3.0.1", | ||
| "homepage": "https://github.com/dominictarr/pull-switch", | ||
@@ -12,3 +12,3 @@ "repository": { | ||
| "pull-pushable": "~1.1.0", | ||
| "pull-stream": "~2.18.2" | ||
| "pull-stream": "~2.23.0" | ||
| }, | ||
@@ -15,0 +15,0 @@ "devDependencies": {}, |
+17
-11
@@ -7,17 +7,23 @@ # pull-switch | ||
| var pull = require('pull-stream') | ||
| var switch = require('pull-switch') | ||
| var pswitch = require('pull-switch') | ||
| pull.count(100) | ||
| .pipe(switch(function select (e) { | ||
| // return key for which stream this is directed to. | ||
| return e % 2 ? 'even' : 'odd' | ||
| }, function createStream(key) { | ||
| //return a stream that will be piped to... | ||
| return pull.drain(function (d) { | ||
| console.log(key+'>', d) | ||
| pull( | ||
| pull.count(100), | ||
| pswitch(function select (e) { | ||
| // return key for which stream this is directed to. | ||
| return e % 2 ? 'even' : 'odd' | ||
| }, function createStream(key) { // 'even' or 'odd' | ||
| //return a stream that will be piped to... | ||
| return pull.drain(function (d) { | ||
| console.log(key+'>', d) | ||
| }) | ||
| }) | ||
| }) | ||
| ) | ||
| ``` | ||
| has the same api as [pull-fork](https://github.com/dominictarr/pull-fork) | ||
| but reads the source at full power instead of trying to follow | ||
| backpressure closely. In somecases it's simpler to do this, | ||
| as waiting (for back-pressure to release) creates the possibility of | ||
| deadlocks. | ||
@@ -24,0 +30,0 @@ ## License |
3546
16.8%42
27.27%32
23.08%+ Added
Updated