forked from mystiq/hydrogen-web
Add type annotations to ObservableArray
This commit is contained in:
parent
348a9c83f5
commit
b148f3ca9e
1 changed files with 12 additions and 10 deletions
|
@ -16,52 +16,54 @@ limitations under the License.
|
||||||
|
|
||||||
import {BaseObservableList} from "./BaseObservableList";
|
import {BaseObservableList} from "./BaseObservableList";
|
||||||
|
|
||||||
export class ObservableArray extends BaseObservableList {
|
export class ObservableArray<T> extends BaseObservableList<T> {
|
||||||
constructor(initialValues = []) {
|
private _items: T[];
|
||||||
|
|
||||||
|
constructor(initialValues: T[] = []) {
|
||||||
super();
|
super();
|
||||||
this._items = initialValues;
|
this._items = initialValues;
|
||||||
}
|
}
|
||||||
|
|
||||||
append(item) {
|
append(item: T): void {
|
||||||
this._items.push(item);
|
this._items.push(item);
|
||||||
this.emitAdd(this._items.length - 1, item);
|
this.emitAdd(this._items.length - 1, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
remove(idx) {
|
remove(idx: number): void {
|
||||||
const [item] = this._items.splice(idx, 1);
|
const [item] = this._items.splice(idx, 1);
|
||||||
this.emitRemove(idx, item);
|
this.emitRemove(idx, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
insertMany(idx, items) {
|
insertMany(idx: number, items: T[]): void {
|
||||||
for(let item of items) {
|
for(let item of items) {
|
||||||
this.insert(idx, item);
|
this.insert(idx, item);
|
||||||
idx += 1;
|
idx += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
insert(idx, item) {
|
insert(idx: number, item: T): void {
|
||||||
this._items.splice(idx, 0, item);
|
this._items.splice(idx, 0, item);
|
||||||
this.emitAdd(idx, item);
|
this.emitAdd(idx, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
update(idx, item, params = null) {
|
update(idx: number, item: T, params: any = null): void {
|
||||||
if (idx < this._items.length) {
|
if (idx < this._items.length) {
|
||||||
this._items[idx] = item;
|
this._items[idx] = item;
|
||||||
this.emitUpdate(idx, item, params);
|
this.emitUpdate(idx, item, params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get array() {
|
get array(): Readonly<T[]> {
|
||||||
return this._items;
|
return this._items;
|
||||||
}
|
}
|
||||||
|
|
||||||
at(idx) {
|
at(idx: number): T | undefined {
|
||||||
if (this._items && idx >= 0 && idx < this._items.length) {
|
if (this._items && idx >= 0 && idx < this._items.length) {
|
||||||
return this._items[idx];
|
return this._items[idx];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get length() {
|
get length(): number {
|
||||||
return this._items.length;
|
return this._items.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue