Granular Utilities for pull-streams
A funnel
is a convenient way to create pull.Sink
(a.k.a reader, a.k.a writable) streams
Generates least amount of code when browserified/webpacked
var funnel = require('pultil/funnel');
var funnel = require('pultil').funnel;
funnel(fn:(data) => end?) => Sink Factory
Funnel
provides a quick and easy way to create
sinks (i.e. write streams).
To abort a stream, simply return true
from the funnel
var count = 0;
funnel(function (data) {
if (count++ > 100) { return true; }
console.log(data);
})
var funnel = require('pultil/funnel');
var src = getSomePullSourceStream();
var sink = funnel(function (data) {
console.log(data);
})
src().pipe(sink());
The above could be created with the more advanced
wsps.Sink
stream factory (as inherited from pull-stream
)
var wsps = require('websocket-pull-stream')
var ws = new WebSocket('ws://localhost:8081')
var src = wsps(ws);
var sink = wsps.Sink(function (read) {
read(null, function next (end, data) {
if (end) { return }
console.log(data);
read(null, next)
})
})
src().pipe(sink());
Evidently, funnel
takes away much of the boiler plate
involved with creating a Sink
factory. We only need
use Sink
when funnel
doesn't supply the required
flexibility.
A tunnel
is convenient way to create pull.Through
streams.
Generates least amount of code when browserified/webpacked
var tunnel = require('pultil/tunnel');
var tunnel = require('pultil').tunnel;
tunnel(fn:(data, cb?:(mutation)) => mutation?) => Through Factory
var syncTransformStream = src.Tunnel(function (data) {
return data * 100;
})
var asyncTransformStream = src.Tunnel(function (data, cb) {
cb(data / 2)
})
var logStream = src.Tunnel(function (data) {
console.info('data passing through:', data)
})
var tunnel = require('pultil/tunnel')
var src = getSomePullSourceStream();
var sink = funnel(function (data) {
console.log('final', data);
})
var throughA = tunnel(function (data) {
console.debug('initial', data)
return data * 100;
})
var throughB = tunnel(function (data) {
console.info('intermediate', data)
})
var throughC = tunnel(function (data, cb) {
cb(data / 2)
})
src()
.pipe(throughA())
.pipe(throughB())
.pipe(throughC())
.pipe(sink());