UNPKG

@equinor/fusion-observable

Version:
109 lines (108 loc) 4.17 kB
import type { Reducer } from 'react'; import { Observable, type Subscription } from 'rxjs'; import type { Action, ActionType, ExtractAction } from './types/actions'; import type { Flow, Effect } from './types/flow'; import type { ReducerWithInitialState } from './types/reducers'; /** * A specialized Observable that maintains internal state, which can be mutated by dispatching actions. * Actions are processed sequentially by a reducer function to produce new state values. * This class extends from Observable to allow subscribers to react to state changes over time. */ export declare class FlowSubject<S, A extends Action = Action> extends Observable<S> { #private; /** * Observable stream of actions dispatched to the subject. */ get action$(): Observable<A>; /** * The current value of state. */ get value(): S; /** * Flag to indicate if the observable is closed. */ get closed(): boolean; /** * Creates a new instance of the FlowSubject class with an initial state reducer. * @param reducer A reducer with an initial state or a reducer function. */ constructor(reducer: ReducerWithInitialState<S, A>); /** * Create a new instance of the FlowSubject class with a reducer function and initial state. * @param reducer state reducer * @param initialState initial state */ constructor(reducer: Reducer<S, A>, initialState: S); /** * Resets the state to the initial value. */ reset: VoidFunction; /** * Dispatches an action to the subject. * * @param action The action to dispatch. */ next(action: A): void; /** * Selects a derived state from the observable state and emits only when the selected state changes. * * @param selector - A function that takes the current state and returns a derived state. * @param comparator - An optional function that compares the previous and current derived states to determine if a change has occurred. * @returns An observable that emits the derived state whenever it changes. */ select<T>(selector: (state: S) => T, comparator?: (previous: T, current: T) => boolean): Observable<T>; /** * Adds an effect that listens for actions and performs side effects. * * @param fn The effect function to execute. * @returns A subscription to the effect. */ addEffect(fn: Effect<A, S>): Subscription; /** * Adds an effect that listens for a specific action type and performs side effects. * * @param actionType The type of action to listen for. * @param cb The effect function to execute when the action is dispatched. * @returns A subscription to the effect. */ addEffect<TType extends ActionType<A>>(actionType: TType, cb: Effect<ExtractAction<A, NoInfer<TType>>, S>): Subscription; /** * Adds an effect that listens for an array of action types and performs side effects. * * @param actionType The array of action types to listen for. * @param cb The effect function to execute when one of the actions is dispatched. * @returns A subscription to the effect. */ addEffect<TType extends ActionType<A>>(actionType: Array<TType>, cb: Effect<ExtractAction<A, NoInfer<TType>>, S>): Subscription; /** * Deprecated. Use `addFlow` instead. * * @deprecated use `addFlow` * * @param fn The flow function to execute. * @returns A subscription to the flow. */ addEpic(fn: Flow<A, S>): Subscription; /** * Adds a flow that listens for actions and performs side effects. * * @param fn The flow function to execute. * @returns A subscription to the flow. */ addFlow(fn: Flow<A, S>): Subscription; /** * Unsubscribes from actions and removes subscribers. */ unsubscribe(): void; /** * Finalizes the subject and completes observers. */ complete(): void; /** * Clones the subject to a simple observable. * * @returns An observable of the state. */ asObservable(): Observable<S>; } export default FlowSubject;