turbodombuilder - v0.9.66
    Preparing search index...

    Class TurboModel<DataType, DataKeyType, IdType, ComponentType, DataEntryType>

    TurboModel

    The type of the data's keys.

    Wrapper around a plain JS container (object, Array, or Map) that exposes a consistent API for reads/writes, signals, and TurboObservers.

    Type Parameters

    • DataType = any

      The type of the data held in the model.

    • DataKeyType extends KeyType = any
    • IdType extends KeyType = any

      The type of the data's ID.

    • ComponentType extends object = any

      The type of instances managed by attached observers.

    • DataEntryType = any

      The type of data associated with each observer instance.

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    ALL: typeof ALL = ...

    Symbol used in nestAll, makeSignals, and generateObserver to target all entries at a certain level inside the data.

    modelConstructor: new (...args: any[]) => TurboModel = TurboModel

    Type Declaration

      • new (...args: any[]): TurboModel
      • Parameters

        • ...args: any[]

        Returns TurboModel

        The default constructor used to create nested TurboModel instances.

    The default constructor used to create nested TurboModel instances.

    observerConstructor: new (...args: any[]) => TurboObserver = TurboObserver

    Type Declaration

    The default constructor used to create TurboObserver instances via generateObserver.

    handlers: Map<string, TurboHandler<TurboModel<any, any, any, any, any>>> = ...

    Map of MVC handlers bound to this model.

    onKeyChanged: Delegate<(value: any, ...keys: KeyType[]) => void> = ...

    Delegate fired whenever a value changes at a key path. Receives the new value followed by the key path as spread arguments.

    isInitialized: boolean = false
    nestedModels: Map<DataKeyType, TurboModel<any, any, any, any, any>> = ...
    nestListeners: Set<(model: TurboModel, key: DataKeyType) => void> = ...
    id: IdType

    The ID of the data held by this model.

    Accessors

    enabledCallbacks: boolean

    Whether change callbacks and observer notifications are enabled.

    bubbleChanges: boolean

    Whether changes bubble up from nested models to their parent.

    • get data(): DataType

      Returns DataType

      The data held by this model. Setting it clears the current state and re-initializes the model.

    • set data(data: DataType): void

      Parameters

      Returns void

    • get keys(): DataKeyType[]

      Returns DataKeyType[]

      All keys currently present in the model.

    • get values(): any[]

      Returns any[]

      All values in the model, in the order of keys.

    • get size(): number

      Returns number

      Number of entries in the model.

    Methods

    • Protected Function

      setup

      Returns void

      Called in the constructor. Use for setup that should happen at instantiation, before this.initialize() is called.

    • Protected Function

      getAction

      Parameters

      • data: any

        The container to read from.

      • key: KeyType

        The key to read.

      Returns any

      The value at the key, or undefined if not found.

      Read a single key from a data container. Override this method to support other datatypes.

    • Function

      get

      Parameters

      Returns any

      The stored value, or undefined if not found.

      Retrieve the value at the given key.

    • Function

      get

      Parameters

      • ...keys: KeyType[]

        Ordered path from outermost to innermost key.

      Returns any

      The stored value, or undefined if not found.

      Retrieve the value at the given key path. Pass no keys to get the root data.

    • Function

      getFlat

      Parameters

      • flatKey: FlatKeyType

        A flat key produced by flattenKey.

      • Optionaldepth: number

        Required when flatKey is a numeric index. The depth of the key path.

      Returns any

      The stored value, or undefined if not found.

      Retrieve the value at the given flat key.

    • Function

      getKey

      Parameters

      • value: any

        The value to locate.

      Returns KeyType[]

      The key path, or undefined if not found.

      Find the key path of the first occurrence of the given value, searching depth-first.

    • Function

      getFlatKey

      Parameters

      • value: any

        The value to query.

      Returns FlatKeyType

      The flat key, or undefined if not found.

      Return the flat key of the first occurrence of the given value.

    • Function

      getKeys

      Parameters

      • value: any

        The value to locate.

      Returns KeyType[][]

      Array of key paths.

      Find the key paths of all occurrences of the given value, searching depth-first.

    • Function

      getFlatKeys

      Parameters

      • value: any

        The value to query.

      Returns FlatKeyType[]

      Array of flat keys.

      Return the flat keys of all occurrences of the given value.

    • Protected Function

      setAction

      Parameters

      • data: any

        The container to write to.

      • value: any

        The value to set.

      • key: KeyType

        The key to write.

      Returns void

      Write a single key to a data container. Override this method to support other datatypes.

    • Protected Function

      internalSet

      Parameters

      • model: TurboModel

        The owning model (used for nested model lookup and change notification), or undefined if operating on a non-root container.

      • data: any

        The container to write to.

      • value: any

        The value to set.

      • key: KeyType

        The key to write.

      Returns void

      Write a value at a key, propagating the change to a nested model if one exists, and firing keyChanged if the value actually changed.

    • Function

      set

      Parameters

      • value: unknown

        The value to set.

      • key: DataKeyType

        The key to write.

      Returns void

      Set a value at the given key and notify observers and signals if the value changed.

    • Function

      set

      Parameters

      • value: unknown

        The value to set.

      • ...keys: KeyType[]

        Ordered path from outermost to innermost key.

      Returns void

      Set a value at the given key path and notify observers and signals if the value changed.

    • Function

      setFlat

      Parameters

      • value: unknown

        The value to set.

      • flatKey: FlatKeyType

        A flat key produced by flattenKey.

      • Optionaldepth: number

        Required when flatKey is a numeric index. The depth of the key path.

      Returns void

      Set a value at the given flat key.

    • Protected Function

      internalAdd

      Parameters

      • model: TurboModel

        The owning model for change notification, or undefined for non-root containers.

      • data: any

        The container to insert into.

      • value: any

        The value to insert.

      • key: KeyType

        The target index or key.

      Returns KeyType

      The index or key where the value was stored.

      Insert a value into a container via addAction and fire keyChanged.

    • Protected Function

      addAction

      Parameters

      • model: TurboModel

        The owning model.

      • data: any

        The container to insert into.

      • value: any

        The value to insert.

      • key: KeyType

        The target index or key. Clamped to valid array bounds for array containers.

      Returns KeyType

      The index or key where the value was stored.

      Perform the raw insertion. Override this method to support other datatypes.

    • Function

      add

      Parameters

      • value: unknown

        The value to insert.

      Returns DataKeyType

      The index where the value was stored.

      Push a value to the end of an array-backed model. For non-array models, forwards to set.

    • Function

      add

      Parameters

      • value: unknown

        The value to insert.

      • Optionalkey: DataKeyType

        The index to insert at. If omitted, the value is pushed to the end.

      Returns DataKeyType

      The index where the value was stored.

      Insert a value into an array-backed model at the given index, or push it if no index is given. For non-array models, forwards to set.

    • Function

      add

      Parameters

      • value: unknown

        The value to insert.

      • ...keys: KeyType[]

        Key path to the target node, with the last key as the insertion index.

      Returns KeyType

      The index or key where the value was stored.

      Insert a value at the given key path. For array-backed nodes, the last key is the insertion index. For non-array models, forwards to set.

    • Function

      addFlat

      Parameters

      • value: unknown

        The value to insert.

      • flatKey: FlatKeyType

        A flat key produced by flattenKey.

      • Optionaldepth: number

        Required when flatKey is a numeric index. The depth of the key path.

      Returns KeyType

      The index or key where the value was stored.

      Insert a value at the position described by the given flat key.

    • Protected Function

      hasAction

      Parameters

      • data: any

        The container to check.

      • key: KeyType

        The key to check.

      Returns boolean

      true if the key is present.

      Check whether a key exists in a container. Override this method to support other datatypes.

    • Function

      has

      Parameters

      Returns boolean

      true if the entry exists.

      Check whether the given key exists in the model.

    • Function

      has

      Parameters

      • ...keys: KeyType[]

        Ordered path from outermost to innermost key.

      Returns boolean

      true if the entry exists.

      Check whether the given key path exists in the model.

    • Function

      hasFlat

      Parameters

      • flatKey: FlatKeyType

        A flat key produced by flattenKey.

      • Optionaldepth: number

        Required when flatKey is a numeric index. The depth of the key path.

      Returns boolean

      Check whether an entry exists at the given flat key.

    • Protected Function

      deleteAction

      Parameters

      • data: any

        The container to remove from.

      • key: KeyType

        The key to remove.

      Returns void

      Remove a single key from a container. Override this method to support other datatypes.

    • Protected Function

      internalDelete

      Parameters

      • model: TurboModel

        The owning model for nested model cleanup and change notification, or undefined for non-root containers.

      • data: any

        The container to remove from.

      • key: KeyType

        The key to remove.

      Returns void

      Remove a key from a container, clearing any associated nested model, and firing keyChanged. No-op if the key does not exist.

    • Function

      delete

      Parameters

      Returns void

      Remove the entry at the given key and notify observers.

    • Function

      delete

      Parameters

      • ...keys: KeyType[]

        Ordered path from outermost to innermost key.

      Returns void

      Remove the entry at the given key path and notify observers.

    • Function

      deleteFlat

      Parameters

      • flatKey: FlatKeyType

        A flat key produced by flattenKey.

      • Optionaldepth: number

        Required when flatKey is a numeric index. The depth of the key path.

      Returns void

      Remove the entry at the given flat key.

    • Function

      flatSize

      Parameters

      • depth: number

        How many levels deep to count.

      Returns number

      Return the total number of entries reachable from this model at the given depth.

    • Returns IterableIterator<[DataKeyType, any]>

      Iterate over [key, value] pairs.

    • Function

      entries

      Returns [DataKeyType, any][]

      Return all [key, value] pairs in the model.

    • Function

      forEach

      Parameters

      • callback: (value: any, key: DataKeyType, model: this) => void

        Called with the value, key, and model.

      • OptionalthisArg: any

        Value to use as this when calling the callback.

      Returns void

      Execute a callback for each entry in the model.

    • Function

      initialize

      Returns void

      Fire change notifications for all existing keys, marking the model as initialized. No-op if already initialized or if data is empty.

    • Function

      clear

      Parameters

      • OptionalclearData: boolean = true

        Whether to also clear the stored data.

      Returns void

      Reset the model, clearing nested models, observers, and signals.

    • Function

      toJSON

      Returns object | DataType

      Convert the model's data into a JSON-serializable form. Maps become plain objects. For non-object data types, the raw value is returned.

    • Function

      makeSignal

      Type Parameters

      • Type = any

        The type of the signal's value.

      Parameters

      Returns SignalBox<Type>

      Return an existing reactive SignalBox for the given key, or create one if absent. The signal reads via get and writes via set.

    • Function

      makeSignal

      Type Parameters

      • Type = any

        The type of the signal's value.

      Parameters

      • ...keys: KeyType[]

        Key path, with the last key as the signal target.

      Returns SignalBox<Type>

      Return an existing reactive SignalBox for the given key path, or create one if absent. The last key in the path is the signal's target; preceding keys navigate to the parent nested model. The signal reads via get and writes via set.

    • Function

      makeSignals

      Type Parameters

      • Type = any

        The type of the signals' values.

      Parameters

      • ...keys: KeyType[]

        Key path to the signal targets. Use ALL at any level to target all entries there.

      Returns SignalBox<Type>[]

      Return reactive SignalBox instances for multiple keys at the given path. Pass TurboModel.ALL at any level of the path to expand all entries at that level.

    • Function

      getSignal

      Parameters

      Returns any

      Retrieve an existing SignalBox for the given key, or undefined if none exists.

    • Function

      getSignal

      Parameters

      • ...keys: KeyType[]

        Key path, with the last key as the signal target.

      Returns any

      Retrieve an existing SignalBox for the given key path, or undefined if none exists. The last key in the path is the signal's target; preceding keys navigate to the parent nested model.

    • Function

      getNested

      Returns TurboModel

      Return this.

    • Function

      getNested

      Parameters

      Returns TurboModel

      Retrieve an already-created nested model at the given key, or undefined if none exists.

    • Function

      getNested

      Parameters

      • ...keys: KeyType[]

        Ordered path from outermost to innermost key.

      Returns TurboModel

      Retrieve an already-created nested model at the given key path, or undefined if none exists.

    • Protected Function

      keyChanged

      Parameters

      • keys: KeyType[]

        The key path that changed.

      • Optionalvalue: unknown = ...

        The new value. Defaults to the current value at the key.

      • Optionaldeleted: boolean = false

        Whether the entry was removed.

      Returns void

      Called internally whenever an entry is added, updated, or deleted. Emits signals, fires onKeyChanged, and notifies attached observers.

    • Function

      flattenKey

      Parameters

      • ...keys: KeyType[]

        The key path to serialize.

      Returns FlatKeyType

      Serialize a key path into a single flat key.

      • Fully numeric paths into array-backed data produce a numeric global leaf index.
      • All other paths produce a "k0|k1|k2|..." string, with symbols encoded as "@@description".
    • Function

      scopeKey

      Parameters

      • flatKey: string

        The flat string key to convert.

      Returns KeyType[]

      Convert a flat string key back into a key path. Reverses the string form of flattenKey. Segments starting with "@@" are decoded back to symbols.

    • Function

      scopeKey

      Parameters

      • flatKey: number

        The numeric index to convert.

      • depth: number

        The depth of the key path to reconstruct.

      Returns KeyType[]

      Convert a numeric global index back into a numeric key path. Reverses the numeric form of flattenKey.

    • Function

      getHandler

      Parameters

      • key: string

        The handler's key.

      Returns TurboHandler

      • The handler.

      Retrieves the attached MVC handler with the given key. By default, unless manually defined in the handler, if the element's class name is MyElement and the handler's class name is MyElementSomethingHandler, the key would be "something".

    • Function

      addHandler

      Parameters

      Returns void

      Registers a TurboHandler for the given key.

    • Parameters

      Returns void