gradum-kit - v0.1.0
    Preparing search index...

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

    GradumYModel

    A GradumModel whose data lives in a Y.js structure, so edits propagate to every other client sharing the document. Reads and writes go through the same API as a plain model; changes arriving from Y.js — local or remote — are turned into the usual signal and observer notifications.

    Type Parameters

    • DataType = any

      The type of the data held in the model.

    • DataKeyType extends KeyType = any

      The type of the data's keys.

    • 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
    ALL: typeof ALL = ...

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

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

    Type Declaration

    The default constructor used to create GradumObserver instances via generateObserver.

    handlers: Map<string, GradumHandler<GradumModel<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.

    onDataChanged: Delegate<(oldData: any, newData: any) => void> = ...

    Delegate fired when this model is pointed at different data. Receives the previous data followed by the new data. Use it to set up watchers that depend on this.data.

    fireCallbackHook: (key: string, ...values: any[]) => void

    Hook invoked by GradumModel.fireCallback. Assign it to route named callbacks from the model out to whatever owns it.

    isInitialized: boolean = false

    Whether GradumModel.initialize has already run on this model.

    changeObservers: Set<ObserverData<DataEntryType, ComponentType, DataKeyType>> = ...

    Every observer attached to this model, with the key path each one watches.

    nestedModels: Map<DataKeyType, GradumModel<any, any, any, any, any>> = ...

    Child models created for nested keys, one per key that has been nested.

    nestedListeners: Set<ListenerData> = ...

    Listeners relaying changes from nested models up to this one.

    id: IdType

    The ID of the data held by this model.

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

    Type Declaration

    The default constructor used to create nested GradumModel instances.

    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 GradumModel.keys.

    • get dataSize(): number

      Returns number

      Number of entries in the model.

    • set enabledCallbacks(value: boolean): void

      Parameters

      • value: boolean

      Returns void

    • Function

      from

      Type Parameters

      • DataType extends object = any

        The type of the data to wrap.

      • IdType extends KeyType = any

        The type of the data's ID.

      Parameters

      • Optionaldata: DataType = ...

        The data to wrap.

      • Optionalid: IdType

        The ID to give the backing model.

      Returns GradumModelProxy<DataType, IdType>

      The proxied data.

      Wrap plain data in a proxy that reads and writes through a model, so the data can be used directly while still producing signals. Reach the underlying model through the proxy's $model key. Assigning an unknown key creates a signal for it.

    • Function

      create

      Type Parameters

      • This extends { prototype: GradumModel }

        The class create was called on.

      Parameters

      Returns This["prototype"]

      The created model, typed as the class this was called on.

      Instantiate a model, then optionally initialize it and make its signals. The return type follows the class it is called on, so GradumYModel.create(...) yields a GradumYModel with its Y-specific members intact.

      Note: the callee is read through this["prototype"] rather than InstanceType<this>. The latter instantiates this class' generics with their constraints (object, KeyType, unknown) instead of their any defaults, which breaks inference at every call site.

    • Protected Function

      setup

      Returns void

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

    • 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

      internalSet

      Parameters

      • model: GradumModel

        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 boolean

      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 boolean

      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 boolean

      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 boolean

      Set a value at the given flat key.

    • Protected Function

      internalAdd

      Parameters

      • model: GradumModel

        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.

    • 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.

    • 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

      true if an entry exists at that flat key.

      Check whether an entry exists at the given flat key.

    • Protected Function

      internalDelete

      Parameters

      • model: GradumModel

        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

      The number of entries at that depth, counting every branch.

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

    • Protected Function

      diffAction

      Parameters

      • oldData: DataType

        The data being replaced.

      • newData: DataType

        The data to adopt.

      Returns void

      Swap in new data while keeping existing nested models and signals alive, re-pointing each child at its counterpart in the new data instead of tearing the tree down. Only called when GradumModel.diffCheck accepts the pair.

    • Returns IterableIterator<[DataKeyType, any]>

      Iterate over [key, value] pairs.

    • 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

      toJSON

      Returns object | DataType

      A plain copy of the data, safe to pass to JSON.stringify.

      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>

      The signal for that key. Reading or writing it keeps the model's data in sync.

      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>

      The signal for that key path. Reading or writing it keeps the model's data in sync.

      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>[]

      One signal per key at that path, in the order the keys appear.

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

    • Function

      getSignal

      Parameters

      Returns any

      The existing signal, or undefined if the key has none.

      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

      The existing signal, or undefined if the key path has none.

      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 GradumModel

      This model, so an empty path resolves to the root.

      Return this.

    • Function

      getNested

      Parameters

      Returns GradumModel

      The nested model, or undefined if that key was never nested.

      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 GradumModel

      The nested model, or undefined if that path was never nested.

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

    • Protected Function

      initializeObserverOnPath

      Parameters

      • data: any

        The data to walk.

      • observer: GradumObserver

        The observer to notify.

      • keys: KeyType[]

        The remaining key path to walk.

      • prefixKeys: KeyType[]

        The path already walked, passed back to the observer.

      • deep: boolean = false

      Returns void

      Walk the data along an observer's key path and report every existing entry to it, so an observer attached to already-populated data still sees what is there. Paths containing GradumModel.ALL fan out across every entry at that level.

    • 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

      The flat key: a number for a fully numeric path, otherwise a "k0|k1" string.

      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[]

      The key path the flat key was built from.

      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[]

      The numeric key path that global index maps to at the given depth.

      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 GradumHandler

      The handler registered under that key, or undefined if there is none.

      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 GradumHandler for the given key.

    • Function

      setDataWithoutInitializing

      Parameters

      Returns void

      Point the model at new data without running GradumModel.initialize on it, so observers and signals are not re-created. Use it when the caller will initialize at a moment of its own choosing; prefer assigning data otherwise.

    • Function

      fireCallback

      Parameters

      • key: string

        The name of the callback to fire.

      • ...values: any[]

        Arguments forwarded to the hook.

      Returns void

      Fire a named callback through GradumModel.fireCallbackHook. Does nothing if no hook has been assigned.

    • 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

      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.

    • Function

      addAction

      Parameters

      • model: GradumModel

        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

      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

      å

      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.

    • 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

      • clearData: boolean = true

        Whether to also clear the stored data.

      Returns void

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

    • Function

      diffCheck

      Parameters

      • oldData: DataType

        The data being replaced.

      • newData: DataType

        The data to adopt.

      Returns boolean

      true if the swap can be done in place.

      Whether two data containers are similar enough to be swapped in place by GradumModel.diffAction rather than triggering a full clear and re-initialize. True for two plain objects, two arrays, or two Maps.

    • Parameters

      • event: YEvent
      • transaction: any

      Returns void

    • Protected Function

      attachNestedObservers

      Parameters

      • value: any

        The Y.js type to observe. Non-Y values are ignored.

      Returns void

      Start observing a Y.js type and everything nested inside it, so changes anywhere in the subtree reach this model. Types already being observed are skipped, so repeated calls are cheap.

    • Protected Function

      detachNestedObservers

      Parameters

      • value: any

        The Y.js type to stop observing. Non-Y values are ignored.

      Returns void

      Stop observing a Y.js type and everything nested inside it, releasing the observers attached by GradumYModel.attachNestedObservers.