@uneksija/comea
v0.3.0
Published
Utilities for simplified observables
Readme
Utilities for simplified observables
Usage
Install:
npm install @uneksija/comeaImport:
import { /* functions */ } from 'comea'Definitions
Observer: A function that receives some data over time and does something with it.
const observer = data => doSomething(data)Observable: A function that receives an observer and calls it with data.
const observable = next => next(130)
const number$ = next => [1, 2, 3].forEach(next)The next example will build a timer observable and a logger observer within these concepts:
const timer = next => {
let i = 0
next(i)
setInterval(_ => {
i++
next(i)
}, 1000)
}
const logger = data => console.log('data:', data)To bind them we call the observable passing the observer as a parameter:
timer(logger)
// data: 0
// data: 1
// data: 2API
just
just: (value: any) => ObservableTakes a value and returns an observable that emits just that value.
from
from: (values: any[]) => ObservableTakes a list of values and returns an observable that emits each value at a time.
periodic
periodic: (interval: number) => ObservableTakes an interval in milliseconds and returns an observable that emits undefined at that interval.
map
map: (base: Observable, mapper: (value: any) => any) => ObservableTakes an observable and a function, returns an observable that applies the function to the values of the base observable and emits the results.
flatMap
flatMap: (base: Observable, mapper: (value: any) => Observable) => ObservableLike map, applies a function to the values emitted by the base observable but expecting that function to return observables, then returns an observable that flattens the emissions of the generated observables.
constant
constant: (base: Observable, value: any) => ObservableTakes an observable and a constant, returns an observable that emits the constant each time the base observable emits a value.
take
take: (base: Observable, ammount: number) => ObservableTakes an observable and an ammount, returns an observable that emits that many events from the base observable.
filter
filter: (base: Observable, predicate: (value: any) => boolean) => ObservableTakes an observable and a predicate, returns an observable that emits only the values from the base observable for which the predicate evaluates to true.
scan
scan: (base: Observable, reducer: (state: any, value: any) => any, initial: any) => ObservableTakes an observable, a reducer and an initial value, returns an observable that emits the partial applications of the reducer on the values from the base observable. Where the reducer is a function that takes the current state and a value, then returns the next state.
merge
merge: (...observables: Observable[]) => ObservableTakes some observables and returns an observable that emits events from all the base observables.
combine
combine: (combiner: (...values: any) => any, ...observables: Observable[]) => ObservableTakes a combiner and some observables, returns an observable that emits the application of the combiner to the values of the base observables. Where the combiner is a function that receives the values in the same order as the observables were passed.
zip
zip: (merger: (...values: any) => any, ...observables: Observable[]) => ObservableTakes a merger and some observables and returns an observable that emits the application of the merger to the values of the base observables. Where the merger is a function that receives the values in the same order as the observables were passed. The first emission will have the first events from all observables, and so on.
sample
sample: (base: Observable, trigger: Observable) => ObservableTakes two observables, a base and a trigger, the resulting observable will emit the last event from the base each time it receives an event from the trigger.
debounce
debounce: (base: Observable, interval: number) => ObservableTakes an observable and an interval in milliseconds and returns an observable that emits events from the base observable with at least that much interval between emissions, ignoring events emitted during this interval.
delay
delay: (base: Observable, interval: number) => ObservableTakes an observable and an interval in milliseconds and returns an observable that emits events from the base observable delayed by that much time.
endWhen
endWhen: (base: Observable, limiter: Observable) => ObservableTakes two observables, a base and a limiter, and returns an observable that emits events from the base observable until the limiter observable emits an event.
Motivation
License
MIT
