Class ArrayTransformer<S, T>

Object for synchronization and transformation a source array into a target array.

It observes changes in the source array, applies a transformation function to each item, and updates the target array accordingly. The class also handles item initialization and cleanup if provided by the user.

Type Parameters

  • S

    Type of the items in the source array.

  • T

    Type of the items in the target array.

Implements

Constructors

  • Creates an instance of a class ArrayTransformer.

    Type Parameters

    • S
    • T

    Parameters

    • params: {
          changed?: (t: T[]) => void;
          clean?: (t: T) => void;
          init?: (t: T) => Promise<void>;
          isItemsEquals?: (a: S, b: S) => boolean;
          source: ObjectOrFunction<readonly S[]>;
          target: ObjectOrFunction<T[]>;
          trackingMode?: TrackingMode;
          transform: (s: S) => T;
      }

      Configuration parameters for the transformer.

      • Optionalchanged?: (t: T[]) => void

        Optional callback function that is called when the target array changes.

      • Optionalclean?: (t: T) => void

        Optional cleanup function for each item in the target array before it is replaced or removed.

      • Optionalinit?: (t: T) => Promise<void>

        Optional asynchronous initialization function for each transformed item in the target array.

      • OptionalisItemsEquals?: (a: S, b: S) => boolean

        Optional function to compare items in the source array for equality. Defaults to Object.is.

      • source: ObjectOrFunction<readonly S[]>

        The source array or a function that returns the source array.

      • target: ObjectOrFunction<T[]>

        The target array or a function that returns the target array.

      • OptionaltrackingMode?: TrackingMode
      • transform: (s: S) => T

        Function that transforms each item from the source array into the target array.

    Returns ArrayTransformer<S, T>

Accessors

  • get hasItemInitializationStage(): boolean

    Indicates whether an initialization function is provided and needs to be called for each transformed item.

    Returns boolean

Methods

  • Returns void

  • Initiates the transformation process by starting to observe the source array. If the source is a function, it sets up a reaction to monitor changes.

    Returns void

    An error if the transformer has already been started.

  • Starts the transformation process and waits for all initialization promises to resolve before completing. This method ensures that all items in the target array are fully initialized before the method returns.

    Returns Promise<void>

    A promise that resolves when all initialization promises have been completed.

  • Synchronizes the target array with the current state of the source array. This method updates the target array based on the current contents of the source array.

    Returns void

  • Asynchronously synchronizes the target array with the source array and waits for all initialization promises to resolve before completing. This ensures that the target array is updated and initialized properly before the method returns.

    Returns Promise<void>

    A promise that resolves when the synchronization and all initialization promises have been completed.