|
|
import { ICancellation } from "@implab/core-amd/interfaces";
|
|
|
|
|
|
/**
|
|
|
* The interface for the consumer of an observable sequence
|
|
|
*/
|
|
|
export interface Observer<T> {
|
|
|
/**
|
|
|
* Called for the next element in the sequence
|
|
|
*/
|
|
|
next?(value: T): void;
|
|
|
|
|
|
/**
|
|
|
* Called once when the error occurs in the sequence.
|
|
|
*/
|
|
|
error?(e: unknown): void;
|
|
|
|
|
|
/**
|
|
|
* Called once at the end of the sequence.
|
|
|
*/
|
|
|
complete?(): void;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* The group of functions to feed an observable. These methods are provided to
|
|
|
* the producer to generate a stream of events.
|
|
|
*/
|
|
|
export type Sink<T> = {
|
|
|
/**
|
|
|
* Call to send the next element in the sequence
|
|
|
*/
|
|
|
next: (value: T) => void;
|
|
|
|
|
|
/**
|
|
|
* Call to notify about the error occurred in the sequence.
|
|
|
*/
|
|
|
error: (e: unknown) => void;
|
|
|
|
|
|
/**
|
|
|
* Call to signal the end of the sequence.
|
|
|
*/
|
|
|
complete: () => void;
|
|
|
|
|
|
/**
|
|
|
* Checks whether the sink is accepting new elements. It's safe to
|
|
|
* send elements to the closed sink.
|
|
|
*
|
|
|
* This method is useful to notify a producer while it's emitting the series
|
|
|
* of synchronous events.
|
|
|
*/
|
|
|
isClosed: () => boolean;
|
|
|
};
|
|
|
|
|
|
export type Producer<T> = (sink: Sink<T>) => (void | (() => void));
|
|
|
|
|
|
export type FusedSink<T> = Omit<Sink<T>, "isClosed">;
|
|
|
|
|
|
export type FusedProducer<T> = (sink: FusedSink<T>) => (void | (() => void));
|
|
|
|
|
|
export interface Unsubscribable {
|
|
|
unsubscribe(): void;
|
|
|
}
|
|
|
|
|
|
export interface Subscribable<T> {
|
|
|
/** Subscribes a consumer to events. If a consumer isn't specified
|
|
|
* this method activates the producer to achieve side affects if any.
|
|
|
*/
|
|
|
subscribe(consumer?: Observer<T>): Unsubscribable;
|
|
|
}
|
|
|
|
|
|
export type AccumulatorFn<T, A> = (acc: A, value: T) => A;
|
|
|
|
|
|
export type OperatorFn<T, U> = (source: Observable<T>) => Observable<U>;
|
|
|
|
|
|
/** The observable source of items. */
|
|
|
export interface Observable<T> extends Subscribable<T> {
|
|
|
/** Transforms elements of the sequence with the specified mapper
|
|
|
*
|
|
|
* @param mapper The mapper used to transform the values
|
|
|
*/
|
|
|
map<T2>(mapper: (value: T) => T2): Observable<T2>;
|
|
|
|
|
|
/** Injects the specified observer into the each producer to consumer chain.
|
|
|
* The method is used to add side effect to the events processing.
|
|
|
*
|
|
|
* @param observer The consumer for the events
|
|
|
*/
|
|
|
tap(observer: Observer<T>): Observable<T>;
|
|
|
|
|
|
/** Filters elements of the sequence. The resulting sequence will
|
|
|
* contain only elements which match the specified predicate.
|
|
|
*
|
|
|
* @param predicate The filter predicate.
|
|
|
*/
|
|
|
filter(predicate: (value: T) => boolean): Observable<T>;
|
|
|
|
|
|
/** Completes the sequence once the condition is met.
|
|
|
* @param predicate The condition which should be met to complete the sequence
|
|
|
*/
|
|
|
until(predicate: (value: T) => boolean): Observable<T>;
|
|
|
|
|
|
/** Keeps the sequence running while elements satisfy the condition.
|
|
|
*
|
|
|
* @param predicate The condition which should be met to continue.
|
|
|
*/
|
|
|
while(predicate: (value: T) => boolean): Observable<T>;
|
|
|
|
|
|
/** Applies accumulator to each value in the sequence and
|
|
|
* emits the accumulated value for each source element
|
|
|
*
|
|
|
* @param accumulator
|
|
|
* @param initial
|
|
|
*/
|
|
|
scan<A>(accumulator: AccumulatorFn<T, A>, initial: A): Observable<A>;
|
|
|
scan(accumulator: AccumulatorFn<T, T>): Observable<T>;
|
|
|
|
|
|
/** Applies accumulator to each value in the sequence and
|
|
|
* emits the accumulated value at the end of the sequence
|
|
|
*
|
|
|
* @param accumulator
|
|
|
* @param initial
|
|
|
*/
|
|
|
reduce<A>(accumulator: AccumulatorFn<T, A>, initial: A): Observable<A>;
|
|
|
reduce(accumulator: AccumulatorFn<T, T>): Observable<T>;
|
|
|
|
|
|
/** Concatenates the specified sequences with this observable
|
|
|
*
|
|
|
* @param seq sequences to concatenate with the current observable
|
|
|
*
|
|
|
* The concatenation doesn't accumulate values from the specified sequences,
|
|
|
* The result of the concatenation is the new observable which will switch
|
|
|
* to the next observable after the previous one completes. Values emitted
|
|
|
* before the next observable being active are lost.
|
|
|
*/
|
|
|
cat(...seq: Subscribable<T>[]): Observable<T>;
|
|
|
|
|
|
|
|
|
/** Pipes the specified operator to produce the new observable
|
|
|
* @param op The operator consumes this observable and produces a new one
|
|
|
*
|
|
|
* The operator is a higher order function which takes a source observable
|
|
|
* and returns a producer for the new observable.
|
|
|
*
|
|
|
* This function can be used to create a complex mapping between source and
|
|
|
* resulting observables. The operator may have a state (or a side effect)
|
|
|
* and can be connected to multiple observables.
|
|
|
*/
|
|
|
pipe<U>(op: OperatorFn<T, U>): Observable<U>;
|
|
|
|
|
|
/** Waits for the next event to occur and returns a promise for the next value
|
|
|
* @param ct Cancellation token
|
|
|
*/
|
|
|
next(ct?: ICancellation): Promise<T>;
|
|
|
|
|
|
/** Collects items of the sequence to the array. */
|
|
|
collect(ct?: ICancellation): Promise<T[]>;
|
|
|
}
|