import { DependencyList } from 'react' import { BehaviorSubject, Observable } from 'rxjs' import { distinctUntilChanged, map } from 'rxjs/operators' import { EqualityFunction, useGuaranteedMemo } from '@spicy-hooks/core' import { Snapshot, useSnapshot } from './use-snapshot' /** * Simple unary function that is meant to select sub-part of a state. * * @typeParam S original shape * @typeParam P extracted partial shape */ export type SelectorFunction = (state: S) => P /** * A simple helper that takes a snapshot of a part of the `observable` defined by the `selector`. * The snapshot is recomputed when the `deps` change so that an updated selector can be applied. * * *Note:* An `Object.is` equality check is applied to the selected sub-part in order to avoid unnecessary re-renders. * In other words the snapshot will be updated only in case the new emission's identity differs from the previous one's. * * @param observable source observable to subscribe to * @param selector function used to select sub-part of the observable state * @param deps dependencies for the selector function * @type T type of the source emissions * @type P type of the partial state * @category Hook */ export function usePartialSnapshot (observable: BehaviorSubject, selector: SelectorFunction, deps: DependencyList): Snapshot

export function usePartialSnapshot (observable: Observable, selector: SelectorFunction, deps: DependencyList): Snapshot

export function usePartialSnapshot (observable: BehaviorSubject | undefined | null, selector: SelectorFunction, deps: DependencyList): Snapshot

export function usePartialSnapshot (observable: Observable | undefined | null, selector: SelectorFunction, deps: DependencyList): Snapshot

/** * A simple hel per that takes a snapshot of a part of the `observable` defined by the `selector`. * The snapshot is recomputed when the `deps` change so that an updated selector can be applied. * * The `equalityFunc` is applied to the selected sub-part in order to avoid unnecessary re-renders. * In other words the snapshot will be updated only in case the new emission differs from the previous one in the sense of `equalityFunc`. * * @param observable source observable to subscribe to * @param selector function used to select sub-part of the observable state * @param equalityFunc equality function to check whether the selected sub-part actually changed from the previous one * @param deps dependencies for the selector function * @type T type of the source emissions * @type P type of the partial state * @category Hook */ export function usePartialSnapshot (observable: BehaviorSubject, selector: SelectorFunction, equalityFunc: EqualityFunction

, deps: DependencyList): Snapshot

export function usePartialSnapshot (observable: Observable, selector: SelectorFunction, equalityFunc: EqualityFunction

, deps: DependencyList): Snapshot

export function usePartialSnapshot (observable: BehaviorSubject | undefined | null, selector: SelectorFunction, equalityFunc: EqualityFunction

, deps: DependencyList): Snapshot

export function usePartialSnapshot (observable: Observable | undefined | null, selector: SelectorFunction, equalityFunc: EqualityFunction

, deps: DependencyList): Snapshot

export function usePartialSnapshot (observable: Observable | undefined | null, selector: SelectorFunction, equalityFuncOrDeps: EqualityFunction

| DependencyList, possibleDeps?: DependencyList): Snapshot

{ const deps = arguments.length > 3 ? possibleDeps! : equalityFuncOrDeps as DependencyList const equalityFunc = arguments.length > 3 ? equalityFuncOrDeps as EqualityFunction

: Object.is const partial$ = useGuaranteedMemo( () => observable?.pipe(map(selector), distinctUntilChanged(equalityFunc)), [observable, ...deps] ) return useSnapshot(partial$) }