Creating chains
run
run is used when we have a source at the same time we’re setting up the chain of processing.
Lets say we want a stream of x,y
values from the ‘pointermove’ event. In this case, we know the source in advance, and we can specify one or more links to process the data.
The return result of run
is a regular Javascript asynchronous generator, see below for how to use the data.
A disadvantage of run
is that the source is ‘baked-in’ to the definition, and you can’t reuse the chain with a different source.
prepare
If you don’t have a source ready, prepare allows you to define the chain and call it with a source later.
Chains made with prepare can be re-used with a different source:
single
Chains are built of the idea of iterable data sources - several bits of data. However, there are cases where you want to use a chain for single input and expect a single output. Use single
Sources
Module Chains.From has various functions for creating chains.
Array
Items from an array can be a source, reading them one at a time with a given interval. Use: From.array
Events
Event data can be a chain source. Specify the event target (eg a DOM element) and the name of the event. Use: From.event
Example: Emits relative x,y coordinates
Timestamp
Generates the current time in milliseconds at a given interval. Use: From.timestamp
By default it runs forever, but you can use loops
or elapsed
options to limit the number of times it runs
Function
Repeatedly calls a function, yielding its values. Use: From.func
An example, yields five random numbers