Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ObservableValue<T, TInitial, TError>

ObservableValue class stores a value and notifies interested parties when the value changes.

This is often used as a property on a class that other classes may be interested in.

Because this is used as a complex property of sorts. It would need some of the functionality of getters and setters, namely: public get, public set, or public get, private set,

The default behavior of ObservableValues gives you the public getter and public setter.


const name = new Observable("John", "MY_SECRET_KEY");
name.setValue("Jared"); // Notifies parties the value changed

If you want to make the setter private you can supply the key at construction, and this will make the setter need a key to set the value or the error.


const name = new Observable("John", "MY_SECRET_KEY");
name.setValue("Jared"); // throw error
name.setValue("Jared", "MY_SECRET_KEY"); // Notifies parties the value changed

Type Parameters

  • T

  • TInitial = T

  • TError = any

Hierarchy

  • ObservableValue

Index

Constructors

Properties

_error: TError = null
_observer: { complete: (() => any); error: ((error: TError) => void); next: ((value: T) => void) } = ...

Type declaration

  • complete: (() => any)
      • (): any
      • Returns any

  • error: ((error: TError) => void)
      • (error: TError): void
      • Parameters

        • error: TError

        Returns void

  • next: ((value: T) => void)
      • (value: T): void
      • Parameters

        • value: T

        Returns void

_value: T | TInitial
errorSubject: Subject<TError> = ...
key: string
setter: ((newValue: T | TInitial, instance: ObservableValue<T, TInitial, TError>) => T)

Type declaration

    • (newValue: T | TInitial, instance: ObservableValue<T, TInitial, TError>): T
    • Parameters

      Returns T

valueSubject: Subject<T> = ...

Methods

  • dispose(): void
  • Returns void

  • getError(): TError
  • Returns TError

  • getObserver(): Observer<T>
  • ADVANCED FEATURE Use this when piping values to another existing ObservableValue like:

    getUserRunner = new AsyncActionRunner<User, null>(null);
    userName = new ObservableValue<string, null>(null);

    constructor() {
    this.getUserRunner.valueSubject
    .pipe(map(user => user.name))

    .subscribe(this.userName.getObserver()); <----- You can pipe values to another ObservableValue.
    }

    Returns Observer<T>

  • getValue(): T | TInitial
  • Returns T | TInitial

  • isCorrectKey(key: string): boolean
  • Parameters

    • key: string

    Returns boolean

  • isSecure(): boolean
  • Returns boolean

  • onChange(callback: ((value: T) => void)): Subscription
  • Parameters

    • callback: ((value: T) => void)
        • (value: T): void
        • Parameters

          • value: T

          Returns void

    Returns Subscription

  • onError(callback: ((e: TError) => void)): Subscription
  • Parameters

    • callback: ((e: TError) => void)
        • (e: TError): void
        • Parameters

          • e: TError

          Returns void

    Returns Subscription

  • setError(e: TError, key?: string): void
  • Parameters

    • e: TError
    • Optional key: string

    Returns void

  • setValue(value: T, key?: string): void
  • Parameters

    • value: T
    • Optional key: string

    Returns void

  • transformValue(cb: ((val: T | TInitial) => T), key?: string): void
  • Parameters

    • cb: ((val: T | TInitial) => T)
        • (val: T | TInitial): T
        • Parameters

          • val: T | TInitial

          Returns T

    • Optional key: string

    Returns void

  • from<T, TInitial>(observable: Observable<T | TInitial>, initialValue: T | TInitial): ObservableValue<T | TInitial, T | TInitial, any>
  • Easier way to pipe from one ObservableValue to another through rxjs operators.

    Example (inside a mediator/class):

     readonly elapsedThrottledSeconds = ObservableValue.from(
    this.elapsedSeconds.valueSubject.pipe(throttleTime(200)),
    this.elapsedSeconds.getValue()
    );

    Type Parameters

    • T

    • TInitial

    Parameters

    • observable: Observable<T | TInitial>

      any Observable (generally from a rxjs pipe though)

    • initialValue: T | TInitial

    Returns ObservableValue<T | TInitial, T | TInitial, any>

Generated using TypeDoc