Options
All
  • Public
  • Public/Protected
  • All
Menu

@spicy-hooks/observables

API reference

This is a detailed API reference of the @spicy-hooks/observables package.

For high-level information about this package and installation instructions, please see our GitHub repository.

Index

Type aliases

SelectorFunction

SelectorFunction<S, P>: (state: S) => P

Simple unary function that is meant to select sub-part of a state.

Type parameters

  • S

    original shape

  • P

    extracted partial shape

Type declaration

    • (state: S): P
    • Parameters

      • state: S

      Returns P

Snapshot

Snapshot<T, S>: []

A tuple of latest emission, state of an observable and eventual error thrown by the observable.

  • value - latest emission of the observable or null if it hasn't started emitting yet and no default value was provided
  • state - determines whether the observable already started emitting, whether it completed or failed (see SnapshotState)
  • error - eventual error thrown by the observable

Type parameters

  • T

    type of the emitted value

  • S: never | null

    null determines that state is nullable, the only only other acceptable type is never

Hook Functions

useAsyncMemo

  • useAsyncMemo<T>(factory: () => Promise<T>, deps: DependencyList): Snapshot<T | null, null>
  • Async version of useMemo where the factory is supposed to return a promise. The hook returns a snapshot of the promise as if it was an observable.

    see

    useSnapshot

    Type parameters

    • T

      type of the value generated by the async factory

    Parameters

    • factory: () => Promise<T>

      async factory

        • (): Promise<T>
        • Returns Promise<T>

    • deps: DependencyList

      dependencies of the async factory

    Returns Snapshot<T | null, null>

useLastEmitRef

  • useLastEmitRef<T>(observable: Observable<T>, initialValue: T): RefObject<T>
  • Returns a ref object whose current value is always the latest value emitted by the provided observable.

    Note that if the observable changes, the value is not reset. Therefore if the new observable is asynchronous, the ref will still have value of the previous observable until the new one emits.

    Type parameters

    • T

      type of the emitted value

    Parameters

    • observable: Observable<T>

      observable to subscribe to and whose emissions should be kept in the ref

    • initialValue: T

      an initial value of the ref to be used while waiting for the observable to emit (if it is async)

    Returns RefObject<T>

useObservableState

  • useObservableState<S>(initialValue: S | (() => S)): []
  • Similar to useState but returns an observable of the current value rather than the value itself.

    Both values of the returned tuple are identity stable.

    Type parameters

    • S

      type of the state value

    Parameters

    • initialValue: S | (() => S)

      initial value for the returned BehaviorSubject or a factory that generates that value

    Returns []

    a tuple of BehaviorSubject and a callback used to change the - or better to say to emit a new - state

usePartialSnapshot

useSnapshot

  • useSnapshot<T>(observable: BehaviorSubject<T>): Snapshot<T>
  • useSnapshot<T>(observable: Observable<T>): Snapshot<T | null>
  • useSnapshot<T>(observable: Observable<T>, defaultValue: T): Snapshot<T>
  • useSnapshot<T>(observable: BehaviorSubject<T> | undefined | null): Snapshot<T, null>
  • useSnapshot<T>(observable: Observable<T> | undefined | null): Snapshot<T | null, null>
  • useSnapshot<T>(observable: Observable<T> | undefined | null, defaultValue: T): Snapshot<T, null>
  • Takes an observable and returns its current snapshot. The snapshot consist of the latest value emitted by the observable, its state and an eventual error thrown by it.

    The snapshot is updated any time the observable emits and if the new snapshot differs from the previous one, the component is re-rendered.

    Type parameters

    • T

      type of the emitted value of the source observable

    Parameters

    • observable: BehaviorSubject<T>

      source observable to subscribe to

    Returns Snapshot<T>

  • Takes an observable and returns its current snapshot. The snapshot consist of the latest value emitted by the observable, its state and an eventual error thrown by it.

    The snapshot is updated any time the observable emits and if the new snapshot differs from the previous one, the component is re-rendered.

    Type parameters

    • T

    Parameters

    • observable: Observable<T>

      source observable to subscribe to

    Returns Snapshot<T | null>

  • Takes an observable and returns its current snapshot. The snapshot consist of the latest value emitted by the observable, its state and an eventual error thrown by it.

    The snapshot is updated any time the observable emits and if the new snapshot differs from the previous one, the component is re-rendered.

    Type parameters

    • T

    Parameters

    • observable: Observable<T>

      source observable to subscribe to

    • defaultValue: T

      default value to be returned as part of the snapshot while waiting for the observable to emit, or when the observable is nullish

    Returns Snapshot<T>

  • Takes an observable and returns its current snapshot. The snapshot consist of the latest value emitted by the observable, its state and an eventual error thrown by it.

    The snapshot is updated any time the observable emits and if the new snapshot differs from the previous one, the component is re-rendered.

    Type parameters

    • T

    Parameters

    • observable: BehaviorSubject<T> | undefined | null

      source observable to subscribe to

    Returns Snapshot<T, null>

  • Takes an observable and returns its current snapshot. The snapshot consist of the latest value emitted by the observable, its state and an eventual error thrown by it.

    The snapshot is updated any time the observable emits and if the new snapshot differs from the previous one, the component is re-rendered.

    Type parameters

    • T

    Parameters

    • observable: Observable<T> | undefined | null

      source observable to subscribe to

    Returns Snapshot<T | null, null>

  • Takes an observable and returns its current snapshot. The snapshot consist of the latest value emitted by the observable, its state and an eventual error thrown by it.

    The snapshot is updated any time the observable emits and if the new snapshot differs from the previous one, the component is re-rendered.

    Type parameters

    • T

    Parameters

    • observable: Observable<T> | undefined | null

      source observable to subscribe to

    • defaultValue: T

      default value to be returned as part of the snapshot while waiting for the observable to emit, or when the observable is nullish

    Returns Snapshot<T, null>

useSubscription

  • useSubscription<T>(observable: Observable<T> | null | undefined, observer: PartialObserver<T> | null | undefined, deps: DependencyList): void
  • A simple helper that subscribes to the observable with the provided observer, optionally resubscribes whenever any of the deps changes, and finally unsubscribes when the component unmounts.

    Type parameters

    • T

      type of the emission of the observable we are subscribing to

    Parameters

    • observable: Observable<T> | null | undefined

      source observable to subscribe to

    • observer: PartialObserver<T> | null | undefined

      observer to subscribe with

    • deps: DependencyList

      dependencies of the observer

    Returns void

useSyncObservable

  • useSyncObservable<T>(observable: Observable<T>): BehaviorSubject<T | null>
  • useSyncObservable<T>(observable: BehaviorSubject<T>): BehaviorSubject<T>
  • useSyncObservable<T>(observable: Observable<T>, defaultValue: T): BehaviorSubject<T>
  • useSyncObservable<T>(observable: Observable<T> | null | undefined): BehaviorSubject<T | null> | null
  • useSyncObservable<T>(observable: BehaviorSubject<T> | null | undefined): BehaviorSubject<T> | null
  • useSyncObservable<T>(observable: Observable<T> | null | undefined, defaultValue: T): BehaviorSubject<T> | null

Operator Functions

bind

  • bind<I, O>(func: UnaryFunction<I, O>): OperatorFunction<I, () => O>
  • Binds the provided unary function to an emitted value as its only argument. Emits a parameter less function.

    Type parameters

    • I

      type of the source observable

    • O

      return type of the provided function

    Parameters

    • func: UnaryFunction<I, O>

      a function to bind the emitted value to

    Returns OperatorFunction<I, () => O>

coldFrom

  • coldFrom<T>(): OperatorFunction<() => ObservableInput<T>, Observable<T>>
  • Makes a cold observable from any emitted parameterless function that returns ObservableInput. This operator is especially handy for turning asynchronous functions into cold observables.

    Type parameters

    • T

    Returns OperatorFunction<() => ObservableInput<T>, Observable<T>>

concurrentLatest

  • concurrentLatest<T>(cancelError?: any): OperatorFunction<Observable<T>, Observable<T>>
  • Subscribes to any emitted observable, but "cancels" it immediately when another one arrives. Canceling means:

    • the source observable is immediately unsubscribed from
    • the re-emitted hot observable is completed or failed (if cancelError is specified and different from undefined)

    The observables are re-emitted immediately as hot.

    Warning: The source observable is always subscribed to, therefore this concurrency strategy cannot prevent eventual side-effects when cancelling.

    Type parameters

    • T

      type of both the accepted and emitted observable

    Parameters

    • Optional cancelError: any

      when provided the re-emitted observable will throw the provided error instead of completing when it is cancelled

    Returns OperatorFunction<Observable<T>, Observable<T>>

concurrentOne

  • concurrentOne<T>(): OperatorFunction<Observable<T>, Observable<T>>
  • Turns the first incoming observable hot and ignores any further observable until the first completes (or fails). Once that happens the operator is ready to accept new observables.

    The hot observables are emitted, the others not.

    Type parameters

    • T

      type of both the accepted and emitted observable

    Returns OperatorFunction<Observable<T>, Observable<T>>

concurrentOneAndLatest

  • concurrentOneAndLatest<T>(): OperatorFunction<Observable<T>, Observable<T>>
  • Ensures that the first emitted observable will become hot. Latest succeeding emitted observable is queued until previous completes. Any further emitted observable replaces the previously queued one.

    The observables are emitted as soon as they become hot.

    Type parameters

    • T

      type of both the accepted and emitted observable

    Returns OperatorFunction<Observable<T>, Observable<T>>

concurrentQueue

  • concurrentQueue<T>(): OperatorFunction<Observable<T>, Observable<T>>
  • Queues all incoming observable and turns each of them hot after the previous completes.

    The observables are emitted as soon as they become hot.

    Type parameters

    • T

      type of both the accepted and emitted observable

    Returns OperatorFunction<Observable<T>, Observable<T>>

shareReplayReset

  • shareReplayReset<T>(): OperatorFunction<T, T>

Test Helper Functions

createAsyncObservable

  • createAsyncObservable(value: number, delay: number, log?: string[]): Observable<number>
  • Create a dummy async observable that emits a single value after the specified delay and then completes.

    Parameters

    • value: number

      the value to emit or -1 to throw an exception instead

    • delay: number

      delay in milliseconds before the observable should emit

    • Optional log: string[]

      optional string array where a simple log messages get appended as the flow of the observable proceeds

    Returns Observable<number>

getSynchronousEmit

  • getSynchronousEmit<T>(observable: Observable<T>): T
  • Retrieves next emit of an observable in a synchronous operation. Fails if the observable throws an exception, if it completes before emitting or if it is not synchronous.

    Type parameters

    • T

      type of the observable value

    Parameters

    • observable: Observable<T>

      source observable to get a synchronous emit from

    Returns T

Type Guard Functions

isBehaviorSubject

  • isBehaviorSubject<T>(observable: Observable<T>): observable is BehaviorSubject<T>
  • Check whether the provided Observable is an instance of BehaviorSubject.

    Type parameters

    • T

      type of the observable value

    Parameters

    • observable: Observable<T>

      observable to be checked

    Returns observable is BehaviorSubject<T>

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Type alias with type parameter
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc