UNPKG

@zodiac-ui/ng-observable

Version:

Create powerful reactive components with Angular. AoT compatible and Ivy ready.

79 lines (78 loc) 2.33 kB
import { OnDestroy } from "@angular/core"; import { NextObserver, ObservableInput } from "rxjs"; import { Callable } from "./internals/callable"; import { Sinkable } from "./interfaces"; export declare type StreamFn = <T>(...targets: (NextObserver<T> | ((value: any) => void))[]) => (...sources: ObservableInput<T>[]) => void; export interface Stream extends StreamFn { <T>(...targets: (NextObserver<T> | ((value: any) => void))[]): (...sources: ObservableInput<T>[]) => void; } /** * Automatically subscribes to source observables and forwards their values to target observers while * discarding error and completion events. Cleans up all subscriptions when the stream completes by manually calling `complete` * or when the bound injector is destroyed. * * @usageNotes * * Basic usage * * ```ts * const stream = new Stream() * const dest = new Subject() * const source = interval(1000) * * stream(dest)(source) * * stream.sink = source * stream.add(source) * * // unsubscribe from all sources * stream.complete() * ``` * * With `State` * * ```typescript * * @Component({ * viewProviders: [StateFactory] // or providers * }) * export class MyComponent { * count: number * * constructor(stateFactory: StateFactory) { * const state = stateFactory.create(this) * * // update count once per second * // automatically unsubscribe when component is destroyed * stream(state)(interval(1000).pipe( * map(count => ({ count })) * )) * } * } * ``` * * @publicApi */ export declare class Stream extends Callable<StreamFn> implements OnDestroy { private sub; /** * A subscription or an observable of teardown logic that will be cleaned up * when the stream ends. */ sink: Sinkable | Sinkable[]; constructor(); /** * Alternative to {@link sink} * * @param sinkables A series of {@link Sinkable} to be cleaned up when the stream ends. */ add(...sinkables: Sinkable[]): void; /** * Completes the stream and unsubscribes from all sources. */ complete(): void; /** * `OnDestroy` hook called by Angular when the bound injector is destroyed */ ngOnDestroy(): void; }