UNPKG

otrie

Version:

Observable Path Trie Store

90 lines (89 loc) 4.29 kB
import { StateChange, StateRecord, StateValue } from './Utils'; import { Observable } from 'rxjs'; /** * Rxjs-observable Path Trie Store. * Keeps an instance of Rxjs Subject associated with every monitored field of State * and notifies observers on changes. */ export declare class TrieStore<RootStateType extends StateRecord = StateRecord> { private rootState; /** Trie of observers. */ private readonly observersTrie; /** * Current depth of active batch operations. * Changes are delivered only after all operations within a batch are completed. */ private batchDepth; /** * Current batch actions in-flight. * The actions will be dispatched when the last top-level batch completes. */ private appliedBatchActions; private rootStateBeforeBatchStart; constructor(rootState: RootStateType); /** * Returns current store state for the root path. * Note: it is unsafe to modify the returned state because the store and observers * will not be aware of the modifications and this can lead to undefined behavior. */ get state(): RootStateType; get state$(): Observable<RootStateType>; /** * Returns a state value stored in the path. * If there is no value associated with the path returns `undefined`. */ get<T extends StateValue = StateValue>(path: ReadonlyArray<string>): T | undefined; /** Creates an observable to monitor current value on the specified path and all subpaths. */ observe<T extends StateValue>(path: string[], pathsToExclude?: Array<string[]>): Observable<T>; /** Creates an observable to monitor changes on the specified path and all subpaths. */ observeChanges<T extends StateValue>(path: string[], pathsToExclude?: Array<string[]>): Observable<StateChange<T>>; /** * Sets a new state to the path. * If the state contains a referentially equal value at the path does nothing. * * The `compareFn` compares an old and new values before the action: * if the values are equal (`true` is returned), the operation makes no changes to the state. * * As a result of this operation, the store state is affected immediately. * If there is no active batch operation, the observers receive a notification immediately. * Otherwise, a notification will be sent after the top-level batch function completes. */ set<T extends StateValue = StateValue>(path: string[], value: T, compareFn?: (oldValue: T | undefined, newValue: T, path: string[]) => boolean): void; /** * Deletes a value in `path`. The path must be non-empty. * Results to no-op if there is no value stored under `path` . * * As a result of this operation, the store state is affected immediately. * If there is no active batch operation, the observers receive a notification immediately. * Otherwise, a notification will be sent after the top-level batch function completes. */ delete(path: string[]): void; /** * Completes and removes all subscriptions and resets the store state. * No update is sent to any observers as the result of this operation: all subscriptions are completed before the cleanup. */ reset(newState: RootStateType): void; /** * Runs a `batchFn` code within a batch. * * All changes done to store inside `batchFn` state are applied immediately, * but will be delivered to observers only after `batchFn` is finished. * * The store state and notifications do not depend on `batchFn` result: * the changes are made, and notifications are sent even if the `batchFn` completes with an Error. */ runInBatch(batchFn: () => unknown): void; /** * Applies changes to the store state immediately, but delays observer * notification until all active batch operations are completed. */ private _apply; /** Notifies all pending observers selected by the action path. */ private _notify; private stubForUnusedPaths; /** Creates an observable to monitor changes on the specified path and all sub-paths. */ private _observeChanges; /** Returns state value for the given root state and path. */ private _get; private selectChildPathsWithObservers; }