import { DepItem } from './_computed_queue'; import { IDisposableOwner } from './dispose'; import { Listener } from './emit'; import { IKnockoutReadObservable } from './kowrap'; import { BaseObservable as Obs } from './observable'; export interface ISubscribableObs { /** @internal */ _getDepItem(): DepItem | null; addListener(callback: (val: any, prev: any) => void, optContext?: object): Listener; get(): any; } export type ISubscribable = ISubscribableObs | IKnockoutReadObservable; export type InferUseType | IKnockoutReadObservable> = TObs extends Obs ? T : TObs extends { peek(): infer U; } ? U : never; export type UseCB = | IKnockoutReadObservable>(obs: TObs) => InferUseType; export interface UseCBOwner extends UseCB { owner: IDisposableOwner; } /** * `Subscription` allows subscribing to several observables at once. It's the foundation for a * `Computed`, but may also be used directly. * * E.g. if we have some existing observables (which may be instances of `Computed`), * we can subscribe to them explicitly: * ```ts * const obs1 = observable(5), obs2 = observable(12); * subscribe(obs1, obs2, (use, v1, v2) => console.log(v1, v2)); * ``` * * or implicitly by using `use(obs)` function, which allows dynamic subscriptions: * ```ts * subscribe(use => console.log(use(obs1), use(obs2))); * ``` * * In either case, if `obs1` or `obs2` is changed, the callbacks will get called automatically. * * Creating a subscription allows any number of dependencies to be specified explicitly, and their * values will be passed to the `callback`. These may be combined with automatic dependencies * detected using `use()`. Note that constructor dependencies have less overhead. * ```ts * subscribe(...deps, ((use, ...depValues) => READ_CALLBACK)); * ``` */ export declare class Subscription { private readonly _depItem; private readonly _dependencies; private readonly _depListeners; private _dynDeps; private _callback; private _useFunc; constructor(callback: (use: UseCB, ...args: any[]) => void, dependencies: ReadonlyArray, owner?: any); /** * Disposes the computed, unsubscribing it from all observables it depends on. */ dispose(): void; /** * For use by computed(): returns this subscription's hook into the _computed_queue. * @internal */ _getDepItem(): DepItem; /** * Gets called when the callback calls `use(obs)` for an observable. It creates a * subscription to `obs` if one doesn't yet exist. * @param obs - The observable being used as a dependency. */ private _useDependency; /** * Calls the callback() with appropriate args, and updates subscriptions when it is done. * I.e. adds dynamic subscriptions created via `use(obs)`, and disposes those no longer used. */ private _evaluate; /** * Subscribes this computed to another observable that it depends on. * @param obs - The observable to subscribe to. * @returns Listener object. */ private _subscribeTo; /** * Adds this item to the recompute queue. */ private _enqueue; } /** * Creates a new Subscription. * @param observables - The initial params, of which there may be zero or more, are * observables on which this computed depends. When any of them change, the `callback` * will be called with the values of these observables as arguments. * @param callback - will be called with arguments `(use, ...values)`, i.e. the * `use` function and values for all of the `...observables` that precede this argument. * This callback is called immediately, and whenever any dependency changes. * @returns The new `Subscription` which may be disposed to unsubscribe. */ export declare function subscribe(cb: (use: UseCB) => void): Subscription; export declare function subscribe(a: Obs, cb: (use: UseCB, a: A) => void): Subscription; export declare function subscribe(a: Obs, b: Obs, cb: (use: UseCB, a: A, b: B) => void): Subscription; export declare function subscribe(a: Obs, b: Obs, c: Obs, cb: (use: UseCB, a: A, b: B, c: C) => void): Subscription; export declare function subscribe(a: Obs, b: Obs, c: Obs, d: Obs, cb: (use: UseCB, a: A, b: B, c: C, d: D) => void): Subscription; export declare function subscribe(a: Obs, b: Obs, c: Obs, d: Obs, e: Obs, cb: (use: UseCB, a: A, b: B, c: C, d: D, e: E) => void): Subscription;