Options
All
  • Public
  • Public/Protected
  • All
Menu

Module hooks

Dolos started using React Hooks! This module exposes some useful hooks when interacting with Dolos.

The following hooks aren't specific to Dolos! There are hooks specifically for Dolos, but you won't find them in here.

Most of these hooks alleviate working with observables, promises, or even the combination of the two.

Index

Functions

useObservable

  • useObservable<T>(observable: BehaviorSubject<T>): T
  • useObservable<T>(observable: Observable<T>): T | undefined
  • useObservable<T, V>(observable: Observable<T>, defaultValue: V): T | V
  • Always returns the latest value emitted by the observable.

    If you provide a [[BehaviorSubject]] the first returned value will be initialised with the observables current value (i.e. [[BehaviourSubject.getValue]]). This makes providing a default value in that case useless since it'll be overwritten. If you don't want this behaviour, please pipe your observable (this essentially converts it to a normal observable).

    Type parameters

    • T

    Parameters

    • observable: BehaviorSubject<T>

    Returns T

    The latest value of the observable

  • Always returns the latest value emitted by the observable.

    If you provide a [[BehaviorSubject]] the first returned value will be initialised with the observables current value (i.e. [[BehaviourSubject.getValue]]). This makes providing a default value in that case useless since it'll be overwritten. If you don't want this behaviour, please pipe your observable (this essentially converts it to a normal observable).

    Type parameters

    • T

    Parameters

    • observable: Observable<T>

    Returns T | undefined

    The latest value of the observable

  • Always returns the latest value emitted by the observable.

    If you provide a [[BehaviorSubject]] the first returned value will be initialised with the observables current value (i.e. [[BehaviourSubject.getValue]]). This makes providing a default value in that case useless since it'll be overwritten. If you don't want this behaviour, please pipe your observable (this essentially converts it to a normal observable).

    Type parameters

    • T

    • V

    Parameters

    • observable: Observable<T>
    • defaultValue: V

    Returns T | V

    The latest value of the observable

useObservablePromise

  • useObservablePromise<T>(promise: PromiseLike<Observable<T> | undefined>): T | undefined
  • useObservablePromise<T, V>(promise: PromiseLike<Observable<T> | undefined>, defaultValue: V): T | V
  • Get the observable from a promise and return its current value

    Type parameters

    • T

    Parameters

    • promise: PromiseLike<Observable<T> | undefined>

    Returns T | undefined

  • Get the observable from a promise and return its current value

    Type parameters

    • T

    • V

    Parameters

    • promise: PromiseLike<Observable<T> | undefined>
    • defaultValue: V

    Returns T | V

useObservablePromiseMemo

  • useObservablePromiseMemo<T>(func: function): T | undefined
  • useObservablePromiseMemo<T, V>(func: function, defaultValue: V): T | V
  • Call the function once to get a promise returning an observable and return its current value .

    Like calling:

         const observable = usePromiseMemo(<func>, EMPTY);
         const value = useObservable(observable, <defaultValue>);

    This may seem like it is way too specific to be of any use but you'll find that a lot of Dolos is built using getter functions that asynchronously return observables.

    Type parameters

    • T

    Parameters

    • func: function
        • (): PromiseLike<Observable<T> | undefined>
        • Returns PromiseLike<Observable<T> | undefined>

    Returns T | undefined

  • Call the function once to get a promise returning an observable and return its current value .

    Like calling:

         const observable = usePromiseMemo(<func>, EMPTY);
         const value = useObservable(observable, <defaultValue>);

    This may seem like it is way too specific to be of any use but you'll find that a lot of Dolos is built using getter functions that asynchronously return observables.

    Type parameters

    • T

    • V

    Parameters

    • func: function
        • (): PromiseLike<Observable<T> | undefined>
        • Returns PromiseLike<Observable<T> | undefined>

    • defaultValue: V

    Returns T | V

usePromise

  • usePromise<T>(promise: PromiseLike<T>): T | undefined
  • usePromise<T, V>(promise: PromiseLike<T>, defaultValue: V): T | V
  • Return the value of the resolved promise.

    Type parameters

    • T

    Parameters

    • promise: PromiseLike<T>

    Returns T | undefined

  • Return the value of the resolved promise.

    Type parameters

    • T

    • V

    Parameters

    • promise: PromiseLike<T>
    • defaultValue: V

    Returns T | V

usePromiseMemo

  • usePromiseMemo<T>(func: function): T | undefined
  • usePromiseMemo<T, V>(func: function, defaultValue: V): T | V
  • Get the promise from a function and wait for it to resolve

    Type parameters

    • T

    Parameters

    • func: function
        • (): PromiseLike<T>
        • Returns PromiseLike<T>

    Returns T | undefined

  • Get the promise from a function and wait for it to resolve

    Type parameters

    • T

    • V

    Parameters

    • func: function
        • (): PromiseLike<T>
        • Returns PromiseLike<T>

    • defaultValue: V

    Returns T | V

useSubscription

  • useSubscription<T>(observable: Observable<T>, observerOrNext?: PartialObserver<T> | function, error?: undefined | function, complete?: undefined | function): void
  • Add a subscriber to an observable. Automatically unsubscribes.

    see

    useObservable to get the current value of an observable.

    Type parameters

    • T

    Parameters

    • observable: Observable<T>
    • Optional observerOrNext: PartialObserver<T> | function
    • Optional error: undefined | function
    • Optional complete: undefined | function

    Returns void

Generated using TypeDoc