/** * Created by luosong on 16/7/20. */ export abstract class Collection extends Array { /** * Appends new elements to an array, and returns the new length of the array. * @param items * @returns {number} */ push(...items):number { let index = this.length; const count = super.push.apply(this, items); if (count > 0) { for (let item of items) { this.onItemAdded(index, item); index++; } } return count; } /** * Inserts new elements at the start of an array. * @param items * @returns {number} */ unshift(...items):number { let index = 0; const count = super.unshift.apply(this, items); if (count > 0) { for (let item of items) { this.onItemAdded(index, item); index++; } } return count; } /** * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. * @param start The zero-based location in the array from which to start removing elements. * @param deleteCount The number of elements to remove. * @param items Elements to insert into the array in place of the deleted elements. */ splice(start: number, deleteCount?: number, ...items: T[]): T[] { const args = []; args.push(start); if (deleteCount >= 0) { args.push(deleteCount); } if (items) { for (let item of items) { args.push(item); } } const removedItems = super.splice.apply(this, args); if (removedItems && removedItems.length > 0) { removedItems.forEach((item, index) => { this.onItemRemoved(start + index, item); }) } return removedItems; } protected onItemAdded(index:number, item:T):void { } protected onItemRemoved(index:number, item:T):void { } }