UNPKG

@equinor/fusion-observable

Version:
83 lines (82 loc) 3.58 kB
import { type DependencyList } from 'react'; import type { Observable, StatefulObservable } from '../types'; /** * Options for {@link useObservableState}. * * @template TType - The initial value type (may be `undefined`). */ type ObservableStateOptions<TType = undefined> = { /** * Value exposed until the observable emits its first value. * * The initial value is read when the internal state stream is created and is * not treated as a subscription dependency. Passing a new object literal on * later renders does not recreate the subscription. */ initial?: TType; /** * Teardown function added to the active observable subscription. * * The latest callback is used when a subscription is created, but changing the * callback alone does not recreate the subscription. */ teardown?: VoidFunction; /** * React-style dependency list that controls when the observable subscription * is recreated. * * When omitted, the hook follows the `subject` reference. Passing `[]` keeps * the first subject for the component lifetime. Passing custom dependencies * lets callers recreate non-memoized subjects during render while only * resubscribing when the selected dependency values change. */ deps?: DependencyList; }; /** * Return type of {@link useObservableState}. * * @template TType - The value type. * @template TError - The error type. */ export type ObservableStateReturnType<TType, TError = unknown> = { /** The most recently emitted value. */ value: TType; /** The most recent error, or `null`. */ error: TError | null; /** Whether the observable has completed. */ complete: boolean; }; /** * Subscribes to an {@link Observable} and returns its state. * * The initial `value` is `undefined` until the first emission. * * @param subject - Observable to subscribe to. Must have a stable reference. */ export declare function useObservableState<S, TError = unknown>(subject: Observable<S>): ObservableStateReturnType<S | undefined, TError>; /** * Subscribes to an {@link Observable} and returns its state. * * @param subject - Observable to subscribe to. Must have a stable reference. * @param opt - Options including an optional `initial` value shown before the first emission. */ export declare function useObservableState<TType, TError = unknown, TInitial extends TType | undefined = undefined>(subject: Observable<TType>, opt: ObservableStateOptions<TInitial>): ObservableStateReturnType<TType | TInitial, TError>; /** * Subscribes to a {@link StatefulObservable} (e.g. `BehaviorSubject`, `FlowSubject`) * and returns its state. * * The current `value` is read synchronously from the subject on mount, * so the initial state is never `undefined`. * * @param subject - Stateful observable to subscribe to. Must have a stable reference. */ export declare function useObservableState<TType, TError = unknown>(subject: StatefulObservable<TType>): ObservableStateReturnType<TType, TError>; /** * Subscribes to a {@link StatefulObservable} (e.g. `BehaviorSubject`, `FlowSubject`) * and returns its state. * * @param subject - Stateful observable to subscribe to. Must have a stable reference. * @param opt - Options including an optional `initial` override and `teardown` callback. */ export declare function useObservableState<TType, TError = unknown>(subject: StatefulObservable<TType>, options: ObservableStateOptions<TType>): ObservableStateReturnType<TType, TError>; export default useObservableState;