UNPKG

@akala/core

Version:
201 lines (200 loc) 8.28 kB
import { Event } from "../events/shared.js"; import { type Formatter } from "../formatters/index.js"; import { type Subscription } from "../teardown-manager.js"; import { watcher } from "./shared.js"; export type ObservableArrayPopEvent<T> = { action: 'pop'; oldItems: T[]; }; export type ObservableArrayPushEvent<T> = { action: 'push'; newItems: T[]; }; export type ObservableArrayShiftEvent<T> = { action: 'shift'; oldItems: T[]; }; export type ObservableArrayUnshiftEvent<T> = { action: 'unshift'; newItems: T[]; }; export type ObservableArrayReplaceEvent<T> = { action: 'replace'; replacedItems: { index: number; oldItem: T; newItem: T; }[]; }; export type ObservableArrayInitEvent<T> = { action: 'init'; newItems: T[]; }; export type ObservableArrayEventMap<T> = ObservableArrayPopEvent<T> | ObservableArrayPushEvent<T> | ObservableArrayShiftEvent<T> | ObservableArrayUnshiftEvent<T> | ObservableArrayReplaceEvent<T> | ObservableArrayInitEvent<T>; export interface ObservableArrayEventArgs<T> { action: 'init' | 'push' | 'shift' | 'pop' | 'unshift' | 'replace'; newItems?: T[]; oldItems?: T[]; } /** * ObservableArray class that extends Event. * @template T */ export declare class ObservableArray<T> extends Event<[ObservableArrayEventMap<T>], void, { triggerAtRegistration?: boolean; once?: boolean; }> { readonly array: Array<T>; /** * Constructor for ObservableArray. * @param {Array<T> | ObservableArray<T>} array - The array to observe. */ constructor(array: Array<T> | ObservableArray<T>); [watcher]: this; get length(): number; set length(value: number); /** * Pushes items to the array. * @param {...T[]} items - The items to push. * @returns {number} The new length of the array. */ push(...items: T[]): number; /** * Shifts items from the array. * @param {number} [count=1] - The number of items to shift. * @returns {void} */ shift(count?: number): void; /** * Pops items from the array. * @param {number} [count=1] - The number of items to pop. * @returns {T[]} The popped items. */ pop(count?: number): T[]; /** * Unshifts items to the array. * @param {...T[]} items - The items to unshift. * @returns {number} The new length of the array. */ unshift(...items: T[]): number; /** * Replaces an item in the array. * @param {number} index - The index of the item to replace. * @param {T} item - The new item. * @returns {void} */ replace(index: number, item: T): void; /** * Replaces multiple items in the array. * @param {{ index: number, item: T }[]} x - The items to replace. * @returns {void} */ replaceN(x: { index: number; item: T; }[]): void; /** * Sorts the array. * @param {(a: T, b: T) => number} [comparer] - The comparer function. * @returns {ObservableArray<T>} The sorted array. */ sort(comparer?: (a: T, b: T) => number): this; /** * Adds a listener to the array. * @param {(args_0: ObservableArrayEventMap<T>) => void} listener - The listener function. * @param {{ triggerAtRegistration?: boolean, once?: boolean }} [options] - The options for the listener. * @returns {Subscription} The subscription. */ addListener(listener: (args_0: ObservableArrayEventMap<T>) => void, options?: { triggerAtRegistration?: boolean; once?: boolean; }): Subscription; private subcription?; /** * Splices the array. * @param {number} start - The start index. * @param {number} [deleteCount] - The number of items to delete. * @param {...T[]} replaceItems - The items to replace. * @returns {T[]} The deleted items. */ splice(start: number, deleteCount?: number, ...replaceItems: T[]): T[]; /** * Replaces the array with a new array. * @param {T[] | ObservableArray<T>} values - The new array. * @returns {void} */ replaceArray(values: T[] | ObservableArray<T>): void; /** * Finds the index of an element in the array. * @param {T} searchElement - The element to search for. * @param {number} [fromIndex] - The index to start the search at. * @returns {number} The index of the element. */ indexOf(searchElement: T, fromIndex?: number): number; /** * Maps the array. * @param {...Parameters<Array<T>['map']<U>>} args - The arguments for the map function. * @returns {ReturnType<Array<T>['map']<U>>} The mapped array. */ map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[]; /** * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; /** * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; /** * Filters the array. * @param {...Parameters<Array<T>['filter']>} args - The arguments for the filter function. * @returns {ReturnType<Array<T>['filter']>} The filtered array. */ filter(...args: Parameters<Array<T>['filter']>): ReturnType<Array<T>['filter']>; /** * Finds an element in the array. * @param {...Parameters<Array<T>['find']>} args - The arguments for the find function. * @returns {ReturnType<Array<T>['find']>} The found element. */ find(...args: Parameters<Array<T>['find']>): ReturnType<Array<T>['find']>; /** * Finds an element index in the array. * @param {...Parameters<Array<T>['findIndex']>} args - The arguments for the find function. * @returns {ReturnType<Array<T>['findIndex']>} The found element index. */ findIndex(...args: Parameters<Array<T>['findIndex']>): ReturnType<Array<T>['findIndex']>; /** * Iterates over the array. * @param {...Parameters<Array<T>['forEach']>} args - The arguments for the forEach function. * @returns {ReturnType<Array<T>['forEach']>} The result of the iteration. */ forEach(...args: Parameters<Array<T>['forEach']>): ReturnType<Array<T>['forEach']>; [Symbol.iterator](): ArrayIterator<T>; /** * Converts the array to a string. * @returns {string} The string representation of the array. */ toString(): string; [Symbol.dispose](): void; } /** * AsyncArrayFormatter class that implements Formatter. * @template T */ export declare class AsyncArrayFormatter<T> implements Formatter<ObservableArray<T>> { private promise; private value; private readonly result; /** * Formats the value. * @param {T[] | ObservableArray<T>} value - The value to format. * @returns {ObservableArray<T>} The formatted value. */ format(value: T[] | ObservableArray<T>): ObservableArray<T>; constructor(); }