Class Collection<T>

MVVM Array wrapper. Emmits change after push/pop/change of element

Type Parameters

  • T extends IEntity = any

Hierarchy (view full)

Implements

  • ICollectionAdapter<T>

Constructors

  • Constructor for the Collection class. Initializes a new instance of Collection with the given entities and optional debounce value and prevData function.

    Type Parameters

    • T extends IEntity = any

    Parameters

    • entities:
          | Collection<T>
          | T[]
          | (() => T[])
          | Entity<T>[] = []

      The initial entities for the Collection. It can be an array of entities, a function that returns an array of entities, an array of Entity objects, or an instance of Collection.

    • Optional_debounce: number = CHANGE_DEBOUNCE

      Optional debounce value for entity changes.

    • Optional_prevData: (() => Entity<T>[]) = ...

      Optional function that returns the previous data of the Collection items.

    Returns Collection<T>

Properties

_debounce: number = CHANGE_DEBOUNCE

Optional debounce value for entity changes.

_prevData: (() => Entity<T>[]) = ...

Optional function that returns the previous data of the Collection items.

Accessors

  • get hasListeners(): boolean
  • Check if the object has any listeners attached to it.

    Returns boolean

    True if the object has listeners, false otherwise.

  • get ids(): (string | number)[]
  • Returns an array of all the IDs stored in the object.

    Returns (string | number)[]

    An array containing all the IDs stored in the object.

  • get isEmpty(): boolean
  • Checks if the collection is empty.

    Returns boolean

    • True if the collection is empty, false otherwise.
  • get items(): Entity<T>[]
  • Retrieves the items stored in the current instance.

    Returns Entity<T>[]

    The array of items stored in the current instance.

  • get lastIdx(): number
  • Returns the last index of the items in the object.

    Returns number

    The last index of the items.

Methods

  • Clear function that performs the following operations:

    1. Disposes of any resources held by this class.
    2. Reorders any elements as necessary.

    Returns void

    clear

    global

  • Emits the given event with the specified arguments.

    Parameters

    • eventName: EventKey

      The name of the event to emit.

    • Rest...args: any[]

      The arguments to pass to the event listeners.

    Returns Promise<void>

    • A promise that resolves when all event listeners have completed.
  • Filter method for an array of items.

    Parameters

    • predicate: ((value: Entity<T>, idx: number) => boolean)

      The function used to test each item in the array.

        • (value, idx): boolean
        • Parameters

          Returns boolean

    Returns Entity<T>[]

    • The filtered array of items.
  • Finds an entity in the list of items based on the given predicate.

    Parameters

    • predicate: ((value: Entity<T>, idx: number) => boolean)

      The predicate function used to determine if an entity matches the condition. The predicate should accept two parameters: value and idx, representing the current entity and its index respectively. It should return a boolean value indicating whether the entity matches the condition.

        • (value, idx): boolean
        • Parameters

          Returns boolean

    Returns undefined | Entity<T>

    • The entity that matches the condition specified by the predicate. If no entity matches the condition, undefined is returned.
  • Finds an entity by its ID.

    Parameters

    • id: string | number

      The ID of the entity to find.

    Returns Entity<T>

    • The found entity.
    • If the entity with the given ID does not exist.
  • Calls a function for each element in the array and passes the value and index as arguments.

    Parameters

    • callbackfn: ((value: Entity<T>, idx: number) => void)

      The function to call for each element, accepts the value and index as arguments.

        • (value, idx): void
        • Parameters

          Returns void

    Returns void

  • Retrieves the listeners associated with the given event key.

    Parameters

    • key: EventKey

      The event key to retrieve the listeners for.

    Returns Function[]

    An array of listeners associated with the given event key.

  • Attaches a change handler to the given collection.

    Parameters

    • change: ((collection: Collection<T>, target: null | Entity<T>) => void)

      The function to be called when a change occurs in the collection. It should accept two parameters: - collection: The collection that has changed. - target: The entity that has been modified, or null if the entire collection has changed.

        • (collection, target): void
        • Parameters

          Returns void

    Returns (() => void)

    • A function that can be called to detach the change handler from the collection.
      • (): void
      • Returns void

  • Handles drop changes for all entities in the collection. Triggers the drop changes event after processing.

    Returns void

    ClassName

    handleDropChanges

  • Applies a callback function to each value in the map and returns a new array of the results.

    Type Parameters

    • V = any

      The type of the elements in the resulting array.

    Parameters

    • callbackfn: ((value: Entity<T>, idx: number) => V)

      A function that accepts a value and its index, and returns a new value.

        • (value, idx): V
        • Parameters

          Returns V

    Returns V[]

    An array containing the results of applying the callback function to each value in the map.

  • Subscribes a callback function to the given event name. The callback function will be triggered only once when the event is emitted.

    Parameters

    • eventName: EventKey

      The name of the event to subscribe to.

    • callback: Function

      The callback function to be executed when the event is emitted.

    Returns (() => void)

    • A function that can be called to unsubscribe the callback function from the event.
      • (): void
      • Returns void

  • Adds items to the collection and performs necessary operations.

    Parameters

    • Rest...items: T[] | T[][]

      The items to be added to the collection.

    Returns void

  • Executes a refresh action triggering an event.

    Returns Promise<void>

    refresh

    REFRESH_SYMBOL

  • Removes an item from the collection by its ID.

    Parameters

    • item: IEntity

      The item to be removed.

    Returns void

  • Removes all items from the collection and performs necessary cleanup.

    Returns void

    Collection

    removeAll

  • Removes an item from the collection by its id.

    Parameters

    • id: string | number

      The id of the item to be removed.

    Returns void

    If the item with the given id is not found in the collection.

  • Sets the data for the software and performs necessary operations.

    Parameters

    • items: T[]

      The array of items to set as data.

    Returns void

  • Checks if at least one element in the array passes the provided test.

    Parameters

    • predicate: ((value: Entity<T>, idx: number) => boolean)

      The test function used to check each element.

        • (value, idx): boolean
        • Parameters

          Returns boolean

    Returns boolean

    • True if any element passes the test, false otherwise.
  • Subscribes a callback function to the specified event name.

    Parameters

    • eventName: EventKey

      The key of the event.

    • callback: Function

      The callback function to be executed when the event is triggered.

    Returns void

  • Converts an array of objects to an array of plain objects using the "toObject" method of each object.

    Returns T[]

    The new array with each object converted to a plain object.

  • Removes a callback function from the list of event listeners for the specified event.

    Parameters

    • eventName: EventKey

      The key of the event to unsubscribe from.

    • callback: Function

      The callback function to remove from the event listeners.

    Returns void

  • Clears all event handlers registered for the current object.

    Returns void

  • Upsert function for adding or updating items in a collection.

    Parameters

    • Rest...items: T[] | T[][]

      An array of items to be added or updated.

    Returns void