Options
Menu

Class UnitBase<T>

UnitBase serves as the base for all the ActiveJS Units: GenericUnit, BoolUnit, ListUnit, etc. It extends Base.

This is an internal construct, normally you'd never have to use this class directly. However, if you're just reading the documentation, or want to learn more about how ActiveJS works, or want to extend this class to build something on your own, please continue.

UnitBase creates the foundation of all the ActiveJS Units. It implements the features like:

  • dispatching, clearing and resetting the value
  • caching the dispatched values
  • navigating through the cached values, by methods like goBack, goForward, jump etc.
  • observable events to listen to including but not limited to the above mentioned actions
  • freezing/unfreezing the Unit
  • muting/unmuting the Unit
  • persisting and retrieving the value to/from persistent-storage
  • debouncing the dispatch
  • resetting the Unit
  • etc.

Hierarchy

Properties

Readonly cacheSize

cacheSize: number

Size of the cache, dictating how many values can be cached at a given time.

default

2

minimum

1

maximum

Infinity

Readonly config

config: Readonly<UnitConfig<T>>

Configured options. Combination of global-options GlobalUnitConfig and the options passed on instantiation.

Readonly events$

events$: Observable<UnitEvents<T>>

On-demand observable events.

Readonly future$

future$: Observable<T> = this.futureSubject.asObservable()

An Observable to observe future values, unlike the default Observable it doesn't replay when subscribed to, rather it waits for the next value.

Accessors

cacheIndex

  • get cacheIndex(): number

cachedValuesCount

  • get cachedValuesCount(): number

emitCount

  • get emitCount(): number

isEmpty

  • get isEmpty(): boolean

isFrozen

  • get isFrozen(): boolean

isMuted

  • get isMuted(): boolean

Access Value Methods

cachedValues

  • cachedValues(): T[]

initialValue

  • initialValue(): T

rawValue

  • rawValue(): T
  • If the Unit has a non-primitive value, use it to get access to the current value, without creating a deep-copy.

    This can come in handy if the Unit is configured to be immutable, and you want to perform a non-mutating action without creating a deep-copy of the value.

    Returns T

value

  • value(): T

Cache Navigation Methods

goBack

  • goBack(): boolean
  • Go back in the cache and re-emit the previous value from the cache,
    without creating a new entry in the cache.

    It can be used as Undo.

    It doesn't work if the Unit is frozen isFrozen. It only works if there's a previously dispatched value in the cache.
    ie: the cacheIndex is not 0

    triggers

    EventUnitJump

    Returns boolean

    true if the cache-navigation was successful, otherwise false.

goForward

  • goForward(): boolean
  • After going back in the cache (ie: re-emitting an old value from the cache),
    use this method to go to the next value, without creating a new entry in the cache.

    It can be used as Redo.

    It doesn't work if the Unit is frozen isFrozen. It only works if the current value is not the last value in the cache.
    ie: the cacheIndex is not equal to cachedValuesCount - 1

    triggers

    EventUnitJump

    Returns boolean

    true if the cache-navigation was successful, otherwise false

jump

  • jump(steps: number): boolean
  • Use this method to re-emit a value from the cache, by jumping specific steps backwards or forwards,
    without creating a new entry in the cache.

    It doesn't work if the Unit is frozen isFrozen or steps is not a number. It only works if the new calculated index is in the bounds of cachedValues,
    ie: the new-index is >= 0, and less than cachedValuesCount, but
    not equal to current cacheIndex.

    triggers

    EventUnitJump

    Parameters

    • steps: number

      Number of steps to jump in the cache, negative to jump backwards, positive to jump forwards

    Returns boolean

    true if the cache-navigation was successful, otherwise false.

jumpToEnd

  • jumpToEnd(): boolean
  • After going back in the cache (ie: re-emitting an old value from the cache),
    use this method to re-dispatch the last (latest) value in the cache,
    without creating a new entry in the cache.

    It doesn't work if the Unit is frozen isFrozen. It only works if the cacheIndex is not already at the last value in the cache.

    triggers

    EventUnitJump

    Returns boolean

    true if the cache-navigation was successful, otherwise false

jumpToStart

  • jumpToStart(): boolean

Common Methods

asObservable

  • asObservable(): Observable<T>
  • Creates a new Observable using the default Observable as source. Use this to conceal other aspects of a Unit, System, Action or Cluster except the Observable part.

    Returns Observable<T>

    An Observable with the value of a Unit, System, Action or Cluster.

createStream

  • A helper method that creates a stream by subscribing to the Observable returned by the param observableProducer callback.

    Ideally the callback function creates an Observable by applying Observable.pipe.

    Just know that you should catch the error in a sub-pipe (ie: do not let it propagate to the main-pipe), otherwise as usual the stream will stop working, and will not react on any further emissions.

    Type parameters

    • R

    Parameters

    Returns Stream

replay

  • replay(): boolean

toJsonString

  • toJsonString(): string

Common Action/Units Methods

dispatch

  • Method to dispatch new value by passing the value directly, or
    by passing a value-producer-function that produces the value using the current value.

    Given a value, it either gets dispatched if it's allowed by wouldDispatch,
    or it gets ignored if not allowed.

    If the Unit is not configured to be immutable, then
    the value-producer-function (param valueOrProducer) should not mutate the current value,
    which is provided as an argument to the function.

    If you mutate the value, then the cached-value might also get mutated,
    as the cached-value is saved by reference, which can result in unpredictable state.

    triggers

    EventUnitDispatch, or EventUnitDispatchFail, depending on the success of dispatch.

    Parameters

    Returns boolean | undefined

    true if value got dispatched, otherwise false. If UnitConfig.dispatchDebounce is enabled, then it'll return undefined.

  • Method to dispatch new value by passing the value directly, or
    by passing a value-producer-function that produces the value using the current value.

    Given a value, it either gets dispatched if it's allowed by wouldDispatch,
    or it gets ignored if not allowed.

    If the Unit is not configured to be immutable, then
    the value-producer-function (param valueOrProducer) should not mutate the current value,
    which is provided as an argument to the function.

    If you mutate the value, then the cached-value might also get mutated,
    as the cached-value is saved by reference, which can result in unpredictable state.

    triggers

    EventUnitDispatch, or EventUnitDispatchFail, depending on the success of dispatch.

    Parameters

    Returns boolean | undefined

    true if value got dispatched, otherwise false. If UnitConfig.dispatchDebounce is enabled, then it'll return undefined.

Common Units Methods

clear

clearCache

  • Clears the cached values, current value stays intact, but it gets removed from the cache.
    Meaning, if you dispatch a new value you can't goBack.
    To keep the last value in the cache, pass {leaveLast: true} in the param options.

    It only works if the Unit is not frozen and there's something left to clear after evaluating the param options.

    Similar to preserving the last value, you can preserve the first value by passing {leaveFirst: true}. Or preserve both first and last value by passing both options together.

    triggers

    EventUnitClearCache

    Parameters

    Returns boolean

    true if the cache was cleared, otherwise false

clearPersistedValue

  • clearPersistedValue(): boolean
  • Clears persisted value from persistent storage.
    It doesn't turn off persistence, future values will get persisted again.

    It only works if the Unit is configured to be persistent. ie: options.persistent is true.

    triggers

    EventUnitClearPersistedValue

    Returns boolean

    true if the Unit is configured to be persistent, otherwise false.

clearValue

  • clearValue(): boolean

freeze

  • freeze(): void
  • Temporarily disables most of the functions of the Unit, except unfreeze,
    mute/unmute, clear and reset.

    It's not the same as Object.freeze.

    Freezing prevents any new values from getting dispatched,
    it disables all the mutating functions.
    Which eventually ensures that no event is emitted while the Unit is frozen,
    however all the read operations and operations that do not emit a value are allowed.

    triggers

    EventUnitFreeze

    Returns void

getCachedValue

  • getCachedValue(index: number): T | undefined

mute

  • mute(): void
  • Mute the Unit, to stop emitting values as well as events, so that the subscribers are not triggered.
    All other functionalities stay unaffected. ie: cache it still updated, value is still updated.

    Note: If you subscribe to the default Observable while the Unit is muted,
    it will replay the last value emitted before muting the Unit,
    because new values are not being emitted.

    Returns void

reset

resetValue

  • resetValue(): boolean

unfreeze

  • unfreeze(): void

unmute

  • unmute(): void
  • Unmute the Unit, to resume emitting values, and events.
    If a value was dispatched while the Unit was muted, the most recent value immediately gets emitted,
    so that subscribers can be in sync again.
    However, other events$ are lost, and they will only emit on the next event.

    It only works if the Unit is muted.
    Moreover, it works even if the Unit is frozen,
    but no value will be emitted because no values would have been dispatched while the Unit was frozen.

    triggers

    EventUnitUnmute

    Returns void

wouldDispatch

  • wouldDispatch(value: T, force?: boolean): boolean
  • Given a value, this function determines whether it should be dispatched or not.
    The dispatch is denied in following circumstances:

    If the Unit is not frozen, you can bypass other dispatch-checks by passing param force = true.

    This function is used internally, when a value is dispatched dispatch.
    Even initialValue UnitConfig.initialValue dispatch has to pass this check.

    You can also use it to check if the value will be dispatched or not before dispatching it.

    Parameters

    • value: T

      The value to be dispatched.

    • Default value force: boolean = false

      Whether dispatch-checks should be bypassed or not.

    Returns boolean

    A boolean indicating whether the param value would pass the dispatch-checks if dispatched.

Other Methods

pipe

  • pipe(): Observable<T>
  • pipe<A>(op1: OperatorFunction<T, A>): Observable<A>
  • pipe<A, B>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>): Observable<B>
  • pipe<A, B, C>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>): Observable<C>
  • pipe<A, B, C, D>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>): Observable<D>
  • pipe<A, B, C, D, E>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>): Observable<E>
  • pipe<A, B, C, D, E, F>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>, op6: OperatorFunction<E, F>): Observable<F>
  • pipe<A, B, C, D, E, F, G>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>, op6: OperatorFunction<E, F>, op7: OperatorFunction<F, G>): Observable<G>
  • pipe<A, B, C, D, E, F, G, H>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>, op6: OperatorFunction<E, F>, op7: OperatorFunction<F, G>, op8: OperatorFunction<G, H>): Observable<H>
  • pipe<A, B, C, D, E, F, G, H, I>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>, op6: OperatorFunction<E, F>, op7: OperatorFunction<F, G>, op8: OperatorFunction<G, H>, op9: OperatorFunction<H, I>): Observable<I>
  • pipe<A, B, C, D, E, F, G, H, I>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>, op6: OperatorFunction<E, F>, op7: OperatorFunction<F, G>, op8: OperatorFunction<G, H>, op9: OperatorFunction<H, I>, ...operations: OperatorFunction<any, any>[]): Observable<{}>
  • Returns Observable<T>

  • Type parameters

    • A

    Parameters

    • op1: OperatorFunction<T, A>

    Returns Observable<A>

  • Type parameters

    • A

    • B

    Parameters

    • op1: OperatorFunction<T, A>
    • op2: OperatorFunction<A, B>

    Returns Observable<B>

  • Type parameters

    • A

    • B

    • C

    Parameters

    • op1: OperatorFunction<T, A>
    • op2: OperatorFunction<A, B>
    • op3: OperatorFunction<B, C>

    Returns Observable<C>

  • Type parameters

    • A

    • B

    • C

    • D

    Parameters

    • op1: OperatorFunction<T, A>
    • op2: OperatorFunction<A, B>
    • op3: OperatorFunction<B, C>
    • op4: OperatorFunction<C, D>

    Returns Observable<D>

  • Type parameters

    • A

    • B

    • C

    • D

    • E

    Parameters

    • op1: OperatorFunction<T, A>
    • op2: OperatorFunction<A, B>
    • op3: OperatorFunction<B, C>
    • op4: OperatorFunction<C, D>
    • op5: OperatorFunction<D, E>

    Returns Observable<E>

  • Type parameters

    • A

    • B

    • C

    • D

    • E

    • F

    Parameters

    • op1: OperatorFunction<T, A>
    • op2: OperatorFunction<A, B>
    • op3: OperatorFunction<B, C>
    • op4: OperatorFunction<C, D>
    • op5: OperatorFunction<D, E>
    • op6: OperatorFunction<E, F>

    Returns Observable<F>

  • Type parameters

    • A

    • B

    • C

    • D

    • E

    • F

    • G

    Parameters

    • op1: OperatorFunction<T, A>
    • op2: OperatorFunction<A, B>
    • op3: OperatorFunction<B, C>
    • op4: OperatorFunction<C, D>
    • op5: OperatorFunction<D, E>
    • op6: OperatorFunction<E, F>
    • op7: OperatorFunction<F, G>

    Returns Observable<G>

  • Type parameters

    • A

    • B

    • C

    • D

    • E

    • F

    • G

    • H

    Parameters

    • op1: OperatorFunction<T, A>
    • op2: OperatorFunction<A, B>
    • op3: OperatorFunction<B, C>
    • op4: OperatorFunction<C, D>
    • op5: OperatorFunction<D, E>
    • op6: OperatorFunction<E, F>
    • op7: OperatorFunction<F, G>
    • op8: OperatorFunction<G, H>

    Returns Observable<H>

  • Type parameters

    • A

    • B

    • C

    • D

    • E

    • F

    • G

    • H

    • I

    Parameters

    • op1: OperatorFunction<T, A>
    • op2: OperatorFunction<A, B>
    • op3: OperatorFunction<B, C>
    • op4: OperatorFunction<C, D>
    • op5: OperatorFunction<D, E>
    • op6: OperatorFunction<E, F>
    • op7: OperatorFunction<F, G>
    • op8: OperatorFunction<G, H>
    • op9: OperatorFunction<H, I>

    Returns Observable<I>

  • Type parameters

    • A

    • B

    • C

    • D

    • E

    • F

    • G

    • H

    • I

    Parameters

    • op1: OperatorFunction<T, A>
    • op2: OperatorFunction<A, B>
    • op3: OperatorFunction<B, C>
    • op4: OperatorFunction<C, D>
    • op5: OperatorFunction<D, E>
    • op6: OperatorFunction<E, F>
    • op7: OperatorFunction<F, G>
    • op8: OperatorFunction<G, H>
    • op9: OperatorFunction<H, I>
    • Rest ...operations: OperatorFunction<any, any>[]

    Returns Observable<{}>

subscribe

  • subscribe(observer?: PartialObserver<T>): Subscription
  • subscribe(next: null | undefined, error: null | undefined, complete: () => void): Subscription
  • subscribe(next: null | undefined, error: (error: any) => void, complete?: () => void): Subscription
  • subscribe(next: (value: T) => void, error: null | undefined, complete: () => void): Subscription
  • subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): Subscription
  • Parameters

    • Optional observer: PartialObserver<T>

    Returns Subscription

  • deprecated

    Use an observer instead of a complete callback

    Parameters

    • next: null | undefined
    • error: null | undefined
    • complete: () => void
        • (): void
        • Returns void

    Returns Subscription

  • deprecated

    Use an observer instead of an error callback

    Parameters

    • next: null | undefined
    • error: (error: any) => void
        • (error: any): void
        • Parameters

          • error: any

          Returns void

    • Optional complete: () => void
        • (): void
        • Returns void

    Returns Subscription

  • deprecated

    Use an observer instead of a complete callback

    Parameters

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

          • value: T

          Returns void

    • error: null | undefined
    • complete: () => void
        • (): void
        • Returns void

    Returns Subscription

  • Parameters

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

          • value: T

          Returns void

    • Optional error: (error: any) => void
        • (error: any): void
        • Parameters

          • error: any

          Returns void

    • Optional complete: () => void
        • (): void
        • Returns void

    Returns Subscription

toPromise

  • toPromise<T>(this: Observable<T>): Promise<T>
  • toPromise<T>(this: Observable<T>, PromiseCtor: typeof Promise): Promise<T>
  • toPromise<T>(this: Observable<T>, PromiseCtor: PromiseConstructorLike): Promise<T>
  • Type parameters

    • T

    Parameters

    • this: Observable<T>

    Returns Promise<T>

  • Type parameters

    • T

    Parameters

    • this: Observable<T>
    • PromiseCtor: typeof Promise

    Returns Promise<T>

  • Type parameters

    • T

    Parameters

    • this: Observable<T>
    • PromiseCtor: PromiseConstructorLike

    Returns Promise<T>