# observable-fns – API ## filter(test): Pipeable ```ts function filter( test: (input: In) => Promise | boolean ): (input: ObservableLike) => Observable ``` Filters the values emitted by another observable. To be applied to an input observable using `pipe()`. ## flatMap(mapper): Pipeable ```ts function flatMap( mapper: (input: In) => Promise | AsyncIterableIterator | IterableIterator | Out[] ): (input: ObservableLike) => Observable ``` Maps the values emitted by another observable. In contrast to `map()` the `mapper` function returns an array of values that will be emitted separately. Use `flatMap()` to map input values to zero, one or multiple output values. To be applied to an input observable using `pipe()`. ## interval(period): Observable ```ts function interval(period: number): Observable ``` Creates an observable that yields a new value every `period` milliseconds. The first value emitted is 0, then 1, 2, etc. The first value is not emitted * immediately, but after the first interval. ## map(mapper): Pipeable ```ts function map( mapper: (input: In) => Promise| Out ): (input: ObservableLike) => Observable ``` Maps the values emitted by another observable to different values. To be applied to an input observable using `pipe()`. ## merge(...observables): Observable ```ts function merge( ...observables: ObservableLike[] ): Observable ``` Creates an observable that emits the values emitted by any of its input observables. It completes once all input observables completed. ## multicast(observable): Observable ```ts function multicast(observable: ObservableLike): Observable ``` Takes a "cold" observable and returns a wrapping "hot" observable that proxies the input observable's values and errors. An observable is called "cold" when its initialization function is run for each new subscriber. This is how observable-fns's `Observable` implementation works. A hot observable is an observable where new subscribers subscribe to the upcoming values of an already-initialiazed observable. ## new Observable(init) ```ts class Observable { private _subscriber constructor(subscriber: Subscriber) subscribe( onNext: (value: T) => void, onError?: (error: any) => void, onComplete?: () => void ): Subscription subscribe(observer: Observer): Subscription tap( onNext: (value: T) => void, onError?: (error: any) => void, onComplete?: () => void ): Observable tap(observer: Observer): Observable forEach(fn: (value: T, done: UnsubscribeFn) => void): Promise pipe(...fns: Array<(input: ObservableLike) => ObservableLike>): ObservableLike map(fn: (value: T) => R): Observable filter(fn: (value: T) => boolean): Observable reduce(fn: (accumulated: R | T, value: T) => R): Observable reduce(fn: (accumulated: R, value: T) => R, seed: R): Observable concat(...sources: Array>): Observable flatMap(fn: (value: T) => ObservableLike): Observable static from(x: Observable | ObservableLike | ArrayLike): Observable static of(...items: I[]): Observable } ``` The basic Observable class. This primitive is used to wrap asynchronous data streams in a common standardized data type that is interoperable between libraries and can be composed to represent more complex processes. ## new Subject() ```ts class Subject extends Observable { constructor() next(value: T): void error(error: any): void complete(): void } ``` A subject is a "hot" observable (see `multicast`) that has its observer methods (`.next(value)`, `.error(error)`, `.complete()`) exposed. Be careful, though! With great power comes great responsibility. Only use the `Subject` when you really need to trigger updates "from the outside" and try to keep the code that can access it to a minimum. Return `Observable.from(mySubject)` to return an observable that cannot easily be mutated. ## scan(accumulator, seed?): Pipeable ```ts function scan( accumulator: (accumulated: Out, value: In, index: number) => Out, seed?: Out ): (input: ObservableLike) => Observable ``` Applies an accumulator function over the source Observable, and returns each intermediate result. It is basically the same as `.reduce()`, but it continuously yields accumulated values, not just after the input completed. If no accumulator seed is supplied then the first input value will be used as a seed. To be applied to an input observable using `pipe()`. ## unsubscribe(observableLike): void ```ts function unsubscribe( subscription: (UnsubscribeFn | { unsubscribe: UnsubscribeFn } | void) ): void type UnsubscribeFn = () => void ``` Unsubscribe from a subscription returned by something that looks like an observable, but is not necessarily our observable implementation.