simple
Maximally Minimal Streams
A strm
is a function.
Minimal Valid STRM
//import itvar strm =//create a noop strm.var as =//pass it another function to pipe it//call it to write data
this is what the code looks like:
var noop = exports = module {var destreturn {if'function' == typeof datareturn dest = dataop &&return}}
notice that the dest
is returned when piping,
also, there is an op
argument.
//piping returns the destination,//os, can connect many streams left to right.;bs =consolelog/* ==>1 '3 streams!'2 '3 streams!'3 '3 streams!'3 streams! undefined*/
Something A Little More Interesting (and useful)
map-strm
var map = mapvar ds =; ;//=> 2\n 4\n 6\n...
this is the code:
var { return e }var map = exports {var dest; map = map || idreturn {if'function' == typeof datareturn dest = data//if `end` is truthy, the stream endsifend returnvar m = data &&return m ? : true}}
there are a lot of things Grown-Up streams need, but this stream doesn't do. Things like back-pressure, and buffering.
Hovewer! this stream may be dumb, but it doesn't prevent smarter streams from doing those things!
Back Pressure
Sometimes a resource tells needs to tell you to slow down.
People do this by putting a confused look on their face,
but strm
does this by returning an array.
Callback when a strm
ends, but only accept one element per tick.
var write = exports {var waiting = paused = false array =return {if'function' === typeof datathrow 'write-only'ifpausedthrow 'cannot write -- paused'paused = trueifdatareturn array waitingifend != nullreturn}}
Notice that the map stream (and the noop stream) have
return dest(data)
this means the array will propagate back up the pipeline until someone can handle the back-pressure.
here is an strm
that can pause.
read
an array, and waits until the destination has drained.
var read = exports {return {if'function' != typeof destthrow 'read-only'ifdestvar i = 0return dest}}
This is just a few basic examples, more to come!
NOTES
what do simple streams need to be able to do?
-
pipe
-
end
-
end as error
-
nak upstream.
-
destroy a stream (pass it an error)
-
disconnect streams
-
interface with pull streams?