UNPKG

9.55 kBTypeScriptView Raw
1import { Observable, EventData } from '../observable';
2export declare class ChangeType {
3 static Add: string;
4 static Delete: string;
5 static Update: string;
6 static Splice: string;
7 static Change: string;
8}
9/**
10 * Event args for "changed" event.
11 */
12export interface ChangedData<T> extends EventData {
13 /**
14 * Change type.
15 */
16 action: string;
17 /**
18 * Start index.
19 */
20 index: number;
21 /**
22 * Removed items.
23 */
24 removed: Array<T>;
25 /**
26 * Number of added items.
27 */
28 addedCount: number;
29}
30/**
31 * Advanced array like class used when you want to be notified when a change occurs.
32 */
33export declare class ObservableArray<T> extends Observable {
34 /**
35 * String value used when hooking to change event.
36 */
37 static changeEvent: string;
38 private _array;
39 private _addArgs;
40 private _deleteArgs;
41 /**
42 * Create ObservableArray<T> from source Array<T>.
43 */
44 constructor(args?: T[] | number);
45 /**
46 * Returns item at specified index.
47 */
48 getItem(index: number): T;
49 /**
50 * Sets item at specified index.
51 */
52 setItem(index: number, value: T): void;
53 /**
54 * Gets or sets the length of the array. This is a number one higher than the highest element defined in an array.
55 */
56 get length(): number;
57 set length(value: number);
58 /**
59 * Returns a string representation of an array.
60 */
61 toString(): string;
62 toLocaleString(): string;
63 /**
64 * Combines two or more arrays.
65 * @param items Additional items to add to the end of array1.
66 */
67 concat(...args: any[]): T[];
68 /**
69 * Adds all the elements of an array separated by the specified separator string.
70 * @param separator A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.
71 */
72 join(separator?: string): string;
73 /**
74 * Removes the last element from an array and returns it.
75 */
76 pop(): T;
77 /**
78 * Appends new elements to an array, and returns the new length of the array.
79 * @param item New element of the Array.
80 */
81 push(...args: any): number;
82 _notifyLengthChange(): void;
83 /**
84 * Reverses the elements in an Array.
85 */
86 reverse(): T[];
87 /**
88 * Removes the first element from an array and returns it.
89 */
90 shift(): T;
91 /**
92 * Returns a section of an array.
93 * @param start The beginning of the specified portion of the array.
94 * @param end The end of the specified portion of the array.
95 */
96 slice(start?: number, end?: number): T[];
97 /**
98 * Sorts an array.
99 * @param compareFn The name of the function used to determine the order of the elements. If omitted, the elements are sorted in ascending, ASCII character order.
100 */
101 sort(compareFn?: (a: T, b: T) => number): T[];
102 /**
103 * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
104 * @param start The zero-based location in the array from which to start removing elements.
105 * @param deleteCount The number of elements to remove.
106 * @param items Elements to insert into the array in place of the deleted elements.
107 */
108 splice(start: number, deleteCount?: number, ...items: any): T[];
109 /**
110 * Inserts new elements at the start of an array.
111 * @param items Elements to insert at the start of the Array.
112 */
113 unshift(...args: any): number;
114 /**
115 * Returns the index of the first element in the array where predicate is true, and -1 otherwise.
116 * @param predicate
117 * @param thisArg If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.
118 */
119 findIndex(predicate: (value: any, index: number, obj: any[]) => unknown, thisArg?: any): number;
120 /**
121 * Returns the index of the first occurrence of a value in an array.
122 * @param searchElement The value to locate in the array.
123 * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
124 */
125 indexOf(searchElement: T, fromIndex?: number): number;
126 /**
127 * Returns the index of the last occurrence of a specified value in an array.
128 * @param searchElement The value to locate in the array.
129 * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at the last index in the array.
130 */
131 lastIndexOf(searchElement: T, fromIndex?: number): number;
132 /**
133 * Determines whether all the members of an array satisfy the specified test.
134 * @param callbackfn A function that accepts up to three arguments. The every method calls the callbackfn function for each element in array1 until the callbackfn returns false, or until the end of the array.
135 * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
136 */
137 every(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean;
138 /**
139 * Determines whether the specified callback function returns true for any element of an array.
140 * @param callbackfn A function that accepts up to three arguments. The some method calls the callbackfn function for each element in array1 until the callbackfn returns true, or until the end of the array.
141 * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
142 */
143 some(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean;
144 /**
145 * Performs the specified action for each element in an array.
146 * @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
147 * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
148 */
149 forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void;
150 /**
151 * Calls a defined callback function on each element of an array, and returns an array that contains the results.
152 * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
153 * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
154 */
155 map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[];
156 /**
157 * Returns the elements of an array that meet the condition specified in a callback function.
158 * @param callbackfn A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array.
159 * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
160 */
161 filter(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): T[];
162 /**
163 * 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.
164 * @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.
165 * @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.
166 */
167 reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T;
168 /**
169 * Calls the specified callback function for all the elements in an array, in descending order. 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.
170 * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
171 * @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.
172 */
173 reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T;
174}
175export interface ObservableArray<T> {
176 /**
177 * A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
178 * @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change").
179 * @param callback - Callback function which will be executed when event is raised.
180 * @param thisArg - An optional parameter which will be used as `this` context for callback execution.
181 */
182 on(eventNames: string, callback: (data: EventData) => void, thisArg?: any): void;
183 on(event: 'change', callback: (args: ChangedData<T>) => void, thisArg?: any): void;
184}