toCompute
Create a compute from a stream generator function.
canStream.toCompute( makeStream(setStream), [context] )
This returns a compute that when bound
takes on the value of the stream returned by makeStream
. makeStream
is called with:
- its
this
as thecontext
, and setStream
which is a stream of values set on the returned compute (ex:compute(5)
).
This is used to create computes from streams.
const count = Kefir.sequentially( 1000, [ 1, 2 ] );
const myCompute = canStream.toCompute( function( setStream ) {
return setStream.merge( count );
} );
// listen to the compute for it to have a value
myCompute.on( "change", function() {} );
myCompute( "A" );
// immediate value
myCompute(); //-> "A"
// 1000ms later
myCompute(); //-> 1
// 1000ms later
myCompute(); //-> 2
Parameters
- makeStream
{function(setStream)}
:A stream generator function. This function takes the stream of set values, and typically other streams and manipulates them into the final returned output stream. The output stream's values are used as the value of the returned compute.
The
setStream
is the stream of values set on the returned compute. In the following example,setStream
will emit the values1
and then2
.const returnedCompute = canStream.toCompute( function( setStream ) { return setStream; } ); returnedCompute( 1 ); returnedCompute( 2 );
- context
{Object}
:An optional context which will be the
this
ofmakeStream
.
Returns
{compute(newVal)}
:
A compute that when read will return the value of
the stream returned by setStream
. When the compute is written to, it will
update setStream
.