1.2 KiB
1.2 KiB
stream
A stream is a computed that works asynchronously. Dependencies can be synchronous
or asynchronous functions returning a scalar or a promise respectively. Also calling a stream
multiple overlapping times will return the same promise and result (so long as dependencies do not
change in the time gap). In this way, stream can serve as a temporal deduplicator.
Usage
Creation
const getToken = stream(
async tokenUrl => fetch(tokenUrl), // computation function
[getTokenUrl, tokenTimeout] // array of subscribable dependencies
);
Read
if (await getToken()) {
/* do stuff with the token */
}
Provide a hash function for complex result types
When the stream result is a type that is not determined to be equal with simple equality (===),
provide a hash function to the hashableStream type to create a stream that will reduce the result
value to a value that can be compared with the identity operator before comparison. The computed
result value will still be stored unmodified.
const tokenStream = hashableStream(token => token && token.id);
const getToken = tokenStream(async tokenUrl => fetch(tokenUrl), [getTokenUrl]);