cognitive
A signals library for functional reactive programming. Author: Tim Farland
Inspired by Elm and Bacon.js.
Written without the use of this
, new
, or prototype
- only simple objects and functions.
Miniscule size - ~1kb minified/gzipped.
For use as an ES6 module.
Install
npm install --save tote
Test
Requires global webpack installation.
npm test
Api
Signal type
Signal A :: {
listeners: [(A -> _)],
active: boolean,
value: A || null,
error: error || null
}
Creating signals
Capture events on a dom node.
// DomNode -> String -> Signal DomEventconst clicks =
A signal that will emit one value, then terminate.
// (A -> _) -> Signal Aconst later =
A signal that will emit one value or an error from a Promise, then terminate.
// Promise A -> Signal Aconst wait =
A signal that emits an integer count of millisecond intervals since it was started.
// Int -> Signal Intconst seconds =
Low-level signal creation.
// Signal Aconst rawSignal =
Interacting with signals
Subscribe to values emitted by the signal.
// Signal A -> (A -> _) -> Signal A
Send a value to a signal.
// Signal A -> A -> Signal A
Stop a signal - no more values will be emitted.
// Signal A -> Signal A
Transforming signals
Map values of a signal
// (... _ -> B) -> ... Signal _ -> Signal Bconst values =
Map (zip) the latest value of multiple signals
// (... _ -> B) -> ... Signal _ -> Signal Bconst areas =
Filter a signal, will only emit event that pass the test
// (A -> Bool) -> Signal A -> Signal A const evens =
Only emit if the current value is different to the previous (as compared by ===
). Not a full deduplication.
// Signal A -> Signal A
Fold a signal over an initial seed value.
// (A -> B -> B) -> B -> Signal A -> Signal Bconst sum =
Merge many signals into one that emits values from all.
// ... Signal _ -> Signal _const events =
Take the last value of a stream when another stream emits.
// Signal A -> Signal B -> Signal Aconst mousePositionsBySeconds =
Emit an array of the last n values of a signal.
// Int -> Signal A -> Signal [A]const trail =
Map values of a signal to a new signal, then flatten the results of all emitted into one signal.
// (A -> Signal B) -> Signal A -> Signal Bconst responses =
The same as above, but only emits values from the latest child signal.
// (A -> Signal B) -> Signal A -> Signal B