Skip to main content
Version: 2.4

BrickContext

Softcoded application context

Contexts have parents and children, they represent the lifecycle of bricks. They contain callbacks executed at specific time of the brick lifespan.

Each time a brick is updated with new inputs, Context.clear is called, executing all callbacks registered with Context.onClear.

When a brick is destroyed, Context.destroy is called, executing all callbacks registered with Context.onClear and Context.onDestroy

Hierarchy

Index

Constructors

constructor

Methods

clear

  • clear(): void
  • Clear the current context: detach and destroys all children context. The context can be reused.


    Returns void

createChild

  • Create and return a new context, child of the current one. Mainly used to run other bricks within the current one (e.g.: the iterator of the brick For Each).


    Parameters

    • optionaldebugName: string

      a name to give to the context for debugging purpose.

    Returns BrickContext

destroy

  • destroy(): void
  • Destroy the current context. It destroys all children context attached to this one and clear their values. The context cannot be reused after calling this function.


    Returns void

get

  • get<T>(key: string | Property<T>, global?: boolean): T
  • Return the current value of the specified property. If there is currently no value, return null.


    Type parameters

    • T

    Parameters

    • key: string | Property<T>

      the property or key string

    • optionalglobal: boolean

      [=false] whether the method checks parent contexts

    Returns T

    the current value

getClosest

  • Return the closest parent of a given type


    Parameters

    • modelTag: Tag

      tag of the “type” of context you want to find in the parent tree

    Returns BrickContext

getOtherContext

  • Return the context with the specified id if accessible (in the hierarchy) from this context. Otherwise, return null.


    Parameters

    • id: string

      the context id we want to retrieve

    Returns BrickContext

getParent

has

  • has<T>(key: string | Property<T>, global?: boolean): boolean
  • Returns a boolean indicating whether a property has a value or not.


    Type parameters

    • T

    Parameters

    • key: string | Property<T>

      the property or key string

    • optionalglobal: boolean

      [=false] whether the method checks parent contexts

    Returns boolean

    whether property has a value or not

isDestroyed

  • isDestroyed(): boolean
  • Whether the context is destroyed.


    Returns boolean

observe

  • observe<T>(key: string | Property<T>, waitForValue?: boolean, global?: boolean): Observable<T>
  • Return an observable to subscribe to value updates of the specified property. If waitForValue is set to FALSE (TRUE by default), the first value received by the observable is null if there is no value at subscription time. If global is set to TRUE (FALSE by default), it observes values coming from other contexts accessible from the current one.


    Type parameters

    • T

    Parameters

    • key: string | Property<T>

      the property or key string

    • optionalwaitForValue: boolean

      [=true] whether the observable should wait for a first value to get a value.

    • optionalglobal: boolean

      [=false] whether to listen to a value coming from other contexts.

    Returns Observable<T>

    the observable

offClear

  • offClear(id: string): void
  • Remove a previously registered callback with Context.onClear method using its id.


    Parameters

    • id: string

      the id of the callback to unregister

    Returns void

offDestroy

  • offDestroy(callbackId: string): boolean
  • Remove a previously registered callback with Context.onDestroy method using its id.


    Parameters

    • callbackId: string

      the id of the callback to unregister

    Returns boolean

onClear

  • onClear(callback: () => void): string
  • Register a callback to execute every time the context is cleared. This happens every time the brick is refreshed with new inputs and during the brick destruction. Return the callback id.


    Parameters

    • callback: () => void

      the callback function

    Returns string

    the callback id

onContext

  • onContext(id: string, callback: (context: BrickContext) => void): () => void
  • Listen to the creation of the context with the specified id. Executes the callback when that context becomes accessible by this current context. Return a function to unregister the listener.


    Parameters

    • id: string

      the context id, which corresponds to the tag of the brick to be retrieved.

    • callback: (context: BrickContext) => void

      the function to execute when the specified brick context is available

    Returns () => void

      • (): void
      • Listen to the creation of the context with the specified id. Executes the callback when that context becomes accessible by this current context. Return a function to unregister the listener.


        Returns void

onDestroy

  • onDestroy(callback: () => void): string
  • Register a callback to execute when the context is destroyed. Return the callback id.


    Parameters

    • callback: () => void

      the callback function

    Returns string

    the callback id

remove

  • remove<T>(key: string | Property<T>): boolean
  • Clear the value of the specified property and propagate that event.


    Type parameters

    • T

    Parameters

    • key: string | Property<T>

      the property or key string

    Returns boolean

repeat

  • Subscribe to the specified observable and set its values to the context with the provided key.


    Type parameters

    • T

    Parameters

    • key: string | Property<T>

      the key used to set the values coming from the observable

    • observable: Observable<T>

      the observable providing values

    Returns BrickContext

    this context

runner

  • Run a runnable property and returns its context.


    Type parameters

    • T

    Parameters

    • key: string | Brick | Property<T>

      the runnable property or the runnable itself

    Returns BrickContext

    the child context or null if the runner was not found

set

  • Set the specified property value and propagate the update to anyone observing it.


    Type parameters

    • T

    Parameters

    • key: string | Property<T>

      the property or key string

    • value: T

      the new value to set

    Returns BrickContext

    this

setParentElement

  • Set the parent element for visual brick to be rendered.


    Parameters

    • parent: Element

      the parent container

    Returns BrickContext

    this

trigger

  • Trigger the specified event and propagate the update to anyone observing it.


    Type parameters

    • T

    Parameters

    • key: string | Property<T>

      the event or key event

    Returns BrickContext

    this

waitFor

  • waitFor<T>(key: string | Property<T>, global?: boolean): Promise<T>
  • Wait for the property to get a new value, wrapped in a promise.


    Type parameters

    • T

    Parameters

    • key: string | Property<T>

      the property

    • optionalglobal: boolean

      [=false] whether the method checks parent contexts

    Returns Promise<T>

    a promise of the next value of property