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

Type Parameters

Hierarchy (View Summary)

Implements

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

    Parameters

    • Optionalentities: 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

      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
_prevData: () => Entity<T>[]
clear: () => void

Clear function that performs the following operations:

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

Type declaration

    • (): void
    • Returns void

clear

global

emit: (eventName: EventKey, ...args: any[]) => Promise<void>

Emits the given event with the specified arguments.

Type declaration

    • (eventName: EventKey, ...args: any[]): Promise<void>
    • Parameters

      • eventName: EventKey

        The name of the event to emit.

      • ...args: any[]

        The arguments to pass to the event listeners.

      Returns Promise<void>

      • A promise that resolves when all event listeners have completed.
filter: (predicate: (value: Entity<T>, idx: number) => boolean) => Entity<T>[]

Filter method for an array of items.

Type declaration

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

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

        The function used to test each item in the array.

      Returns Entity<T>[]

      • The filtered array of items.
find: (
    predicate: (value: Entity<T>, idx: number) => boolean,
) => undefined | Entity<T>

Finds an entity in the list of items based on the given predicate.

Type declaration

    • (predicate: (value: Entity<T>, idx: number) => boolean): undefined | Entity<T>
    • 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.

      Returns undefined | Entity<T>

      • The entity that matches the condition specified by the predicate. If no entity matches the condition, undefined is returned.
findById: (id: string | number) => Entity<T>

Finds an entity by its ID.

Type declaration

    • (id: string | number): Entity<T>
    • 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.
forEach: (callbackfn: (value: Entity<T>, idx: number) => void) => void

Calls a function for each element in the array and passes the value and index as arguments.

Type declaration

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

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

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

      Returns void

getListeners: (key: EventKey) => Function[]

Retrieves the listeners associated with the given event key.

Type declaration

    • (key: EventKey): Function[]
    • Parameters

      • key: EventKey

        The event key to retrieve the listeners for.

      Returns Function[]

      An array of listeners associated with the given event key.

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

Attaches a change handler to the given collection.

Type declaration

    • (
          change: (collection: Collection<T>, target: null | Entity<T>) => void,
      ): () => void
    • 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.

      Returns () => void

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

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

Type declaration

    • (): void
    • Returns void

ClassName

handleDropChanges

map: <V = any>(callbackfn: (value: Entity<T>, idx: number) => V) => V[]

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

Type declaration

    • <V = any>(callbackfn: (value: Entity<T>, idx: number) => V): V[]
    • 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.

      Returns V[]

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

once: (eventName: EventKey, callback: Function) => () => void

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

Type declaration

    • (eventName: EventKey, callback: Function): () => void
    • 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.
push: (...items: T[] | T[][]) => void

Adds items to the collection and performs necessary operations.

Type declaration

    • (...items: T[] | T[][]): void
    • Parameters

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

        The items to be added to the collection.

      Returns void

refresh: () => Promise<void>

Executes a refresh action triggering an event.

refresh

REFRESH_SYMBOL

remove: (item: IEntity) => void

Removes an item from the collection by its ID.

Type declaration

    • (item: IEntity): void
    • Parameters

      • item: IEntity

        The item to be removed.

      Returns void

removeAll: () => void

Removes all items from the collection and performs necessary cleanup.

Type declaration

    • (): void
    • Returns void

Collection

removeAll

removeById: (id: string | number) => void

Removes an item from the collection by its id.

Type declaration

    • (id: string | number): void
    • 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.

setData: (items: T[]) => void

Sets the data for the software and performs necessary operations.

Type declaration

    • (items: T[]): void
    • Parameters

      • items: T[]

        The array of items to set as data.

      Returns void

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

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

Type declaration

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

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

        The test function used to check each element.

      Returns boolean

      • True if any element passes the test, false otherwise.
subscribe: (eventName: EventKey, callback: Function) => void

Subscribes a callback function to the specified event name.

Type declaration

    • (eventName: EventKey, callback: Function): void
    • Parameters

      • eventName: EventKey

        The key of the event.

      • callback: Function

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

      Returns void

toArray: () => T[]

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

Type declaration

    • (): T[]
    • Returns T[]

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

unsubscribe: (eventName: EventKey, callback: Function) => void

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

Type declaration

    • (eventName: EventKey, callback: Function): void
    • Parameters

      • eventName: EventKey

        The key of the event to unsubscribe from.

      • callback: Function

        The callback function to remove from the event listeners.

      Returns void

unsubscribeAll: () => void

Clears all event handlers registered for the current object.

Type declaration

    • (): void
    • Returns void

upsert: (...items: T[] | T[][]) => void

Upsert function for adding or updating items in a collection.

Type declaration

    • (...items: T[] | T[][]): void
    • Parameters

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

        An array of items to be added or updated.

      Returns void

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.