UNPKG

11.1 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> with specified length.
43 */
44 constructor(arrayLength?: number);
45 /**
46 * Create ObservableArray<T> from source Array<T>.
47 */
48 constructor(items: T[]);
49 /**
50 * Create ObservableArray<T> from T items.
51 */
52 constructor(...items: T[]);
53 [Symbol.iterator](): Generator<T, void, unknown>;
54 /**
55 * Returns item at specified position.
56 * Supports relative indexing from the end of the array when passed a negative index.
57 */
58 getItem(pos: number): T;
59 /**
60 * Sets item at specified position.
61 * Supports relative indexing from the end of the array when passed a negative index.
62 */
63 setItem(pos: number, value: T): void;
64 /**
65 * Gets or sets the length of the array. This is a number one higher than the highest element defined in an array.
66 */
67 get length(): number;
68 set length(value: number);
69 toJSON(): Array<any>;
70 /**
71 * Returns a string representation of an array.
72 */
73 toString(): string;
74 toLocaleString(): string;
75 /**
76 * Combines two or more arrays.
77 * @param items Additional items to add to the end of array1.
78 */
79 concat(...args: any[]): ObservableArray<T>;
80 /**
81 * Adds all the elements of an array separated by the specified separator string.
82 * @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.
83 */
84 join(separator?: string): string;
85 /**
86 * Removes the last element from an array and returns it.
87 */
88 pop(): T;
89 /**
90 * Appends new elements to an array, and returns the new length of the array.
91 * @param item New element of the Array.
92 */
93 push(...args: T[]): number;
94 _notifyLengthChange(): void;
95 /**
96 * Reverses the elements in an Array.
97 * This method uses 'in place' algorithm.
98 */
99 reverse(): ObservableArray<T>;
100 /**
101 * Removes the first element from an array and returns it.
102 */
103 shift(): T;
104 /**
105 * Returns a section of an array.
106 * @param start The beginning of the specified portion of the array.
107 * @param end The end of the specified portion of the array.
108 */
109 slice(start?: number, end?: number): ObservableArray<T>;
110 /**
111 * Sorts an array.
112 * This method uses 'in place' algorithm.
113 * @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.
114 */
115 sort(compareFn?: (a: T, b: T) => number): ObservableArray<T>;
116 /**
117 * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
118 * This method uses 'in place' algorithm.
119 * @param start The zero-based location in the array from which to start removing elements.
120 * @param deleteCount The number of elements to remove.
121 * @param items Elements to insert into the array in place of the deleted elements.
122 */
123 splice(start: number, deleteCount?: number, ...items: T[]): ObservableArray<T>;
124 /**
125 * Inserts new elements at the start of an array.
126 * @param items Elements to insert at the start of the Array.
127 */
128 unshift(...args: T[]): number;
129 /**
130 * Returns the first element in the array where predicate is true, and null otherwise.
131 * @param callbackfn
132 * @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.
133 */
134 find(callbackfn: (value: T, index: number, array: ObservableArray<T>) => any, thisArg?: any): T;
135 /**
136 * Returns the index of the first element in the array where predicate is true, and -1 otherwise.
137 * @param callbackfn
138 * @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.
139 */
140 findIndex(callbackfn: (value: T, index: number, array: ObservableArray<T>) => any, thisArg?: any): number;
141 /**
142 * Determines whether the specified element exists inside the array.
143 * @param searchElement The value to locate in the array.
144 * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
145 */
146 includes(searchElement: T, fromIndex?: number): boolean;
147 /**
148 * Returns the index of the first occurrence of a value in an array.
149 * @param searchElement The value to locate in the array.
150 * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
151 */
152 indexOf(searchElement: T, fromIndex?: number): number;
153 /**
154 * Returns the index of the last occurrence of a specified value in an array.
155 * @param searchElement The value to locate in the array.
156 * @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.
157 */
158 lastIndexOf(searchElement: T, fromIndex?: number): number;
159 /**
160 * Determines whether all the members of an array satisfy the specified test.
161 * @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.
162 * @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.
163 */
164 every(callbackfn: (value: T, index: number, array: ObservableArray<T>) => boolean, thisArg?: any): boolean;
165 /**
166 * Determines whether the specified callback function returns true for any element of an array.
167 * @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.
168 * @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.
169 */
170 some(callbackfn: (value: T, index: number, array: ObservableArray<T>) => boolean, thisArg?: any): boolean;
171 /**
172 * Performs the specified action for each element in an array.
173 * @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
174 * @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.
175 */
176 forEach(callbackfn: (value: T, index: number, array: ObservableArray<T>) => void, thisArg?: any): void;
177 /**
178 * Calls a defined callback function on each element of an array, and returns an array that contains the results.
179 * @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.
180 * @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.
181 */
182 map<U>(callbackfn: (value: T, index: number, array: ObservableArray<T>) => U, thisArg?: any): ObservableArray<U>;
183 /**
184 * Returns the elements of an array that meet the condition specified in a callback function.
185 * @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.
186 * @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.
187 */
188 filter(callbackfn: (value: T, index: number, array: ObservableArray<T>) => boolean, thisArg?: any): ObservableArray<T>;
189 /**
190 * 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.
191 * @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.
192 * @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.
193 */
194 reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: ObservableArray<T>) => T, initialValue?: T): T;
195 /**
196 * 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.
197 * @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.
198 * @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.
199 */
200 reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: ObservableArray<T>) => T, initialValue?: T): T;
201}
202export interface ObservableArray<T> {
203 /**
204 * A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
205 * @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change").
206 * @param callback - Callback function which will be executed when event is raised.
207 * @param thisArg - An optional parameter which will be used as `this` context for callback execution.
208 */
209 on(eventNames: string, callback: (data: EventData) => void, thisArg?: any): void;
210 on(event: 'change', callback: (args: ChangedData<T>) => void, thisArg?: any): void;
211}