diff --git a/src/observable/list/BaseMappedList.ts b/src/observable/list/BaseMappedList.ts index 314c083e..f339d113 100644 --- a/src/observable/list/BaseMappedList.ts +++ b/src/observable/list/BaseMappedList.ts @@ -18,60 +18,68 @@ limitations under the License. import {BaseObservableList} from "./BaseObservableList"; import {findAndUpdateInArray} from "./common"; -export class BaseMappedList extends BaseObservableList { - constructor(sourceList, mapper, updater, removeCallback) { +export type Mapper = (value: F) => T +export type Updater = (mappedValue: T, params: any, value: F) => void; + +export class BaseMappedList extends BaseObservableList { + protected _sourceList: BaseObservableList; + protected _sourceUnsubscribe: (() => void) | null = null; + _mapper: Mapper; + _updater: Updater; + _removeCallback: ((value: T) => void) | null; + _mappedValues: T[] | null = null; + + constructor(sourceList: BaseObservableList, mapper: Mapper, updater: Updater, removeCallback: ((value: T) => void) | null = null) { super(); this._sourceList = sourceList; this._mapper = mapper; this._updater = updater; this._removeCallback = removeCallback; - this._mappedValues = null; - this._sourceUnsubscribe = null; } - findAndUpdate(predicate, updater) { - return findAndUpdateInArray(predicate, this._mappedValues, this, updater); + findAndUpdate(predicate: (value: T) => boolean, updater: (value: T) => any | false) { + return findAndUpdateInArray(predicate, this._mappedValues!, this, updater); } get length() { - return this._mappedValues.length; + return this._mappedValues!.length; } [Symbol.iterator]() { - return this._mappedValues.values(); + return this._mappedValues!.values(); } } -export function runAdd(list, index, mappedValue) { - list._mappedValues.splice(index, 0, mappedValue); +export function runAdd(list: BaseMappedList, index: number, mappedValue: T): void { + list._mappedValues!.splice(index, 0, mappedValue); list.emitAdd(index, mappedValue); } -export function runUpdate(list, index, value, params) { - const mappedValue = list._mappedValues[index]; +export function runUpdate(list: BaseMappedList, index: number, value: F, params: any): void { + const mappedValue = list._mappedValues![index]; if (list._updater) { list._updater(mappedValue, params, value); } list.emitUpdate(index, mappedValue, params); } -export function runRemove(list, index) { - const mappedValue = list._mappedValues[index]; - list._mappedValues.splice(index, 1); +export function runRemove(list: BaseMappedList, index: number): void { + const mappedValue = list._mappedValues![index]; + list._mappedValues!.splice(index, 1); if (list._removeCallback) { list._removeCallback(mappedValue); } list.emitRemove(index, mappedValue); } -export function runMove(list, fromIdx, toIdx) { - const mappedValue = list._mappedValues[fromIdx]; - list._mappedValues.splice(fromIdx, 1); - list._mappedValues.splice(toIdx, 0, mappedValue); +export function runMove(list: BaseMappedList, fromIdx: number, toIdx: number): void { + const mappedValue = list._mappedValues![fromIdx]; + list._mappedValues!.splice(fromIdx, 1); + list._mappedValues!.splice(toIdx, 0, mappedValue); list.emitMove(fromIdx, toIdx, mappedValue); } -export function runReset(list) { +export function runReset(list: BaseMappedList): void { list._mappedValues = []; list.emitReset(); }