definitions fixed

This commit is contained in:
Erjan Gavalji
2015-03-03 10:34:40 +02:00
parent a03ce4ca1d
commit cc829e0152
705 changed files with 321431 additions and 148812 deletions

View File

@@ -0,0 +1,241 @@
// Test: http://jsperf.com/array-vs-observable-array-vs-array-observe
/**
* Contains the ObservableArray class, which is capable of detecting and responding to changes of a collection of objects.
*/
declare module "data/observable-array" {
import observable = require("data/observable");
/**
* Known event names.
*/
module knownEvents {
export var change: string;
}
/**
* Event args for "changed" event.
*/
interface ChangedData<T> extends observable.EventData {
/**
* Change type.
*/
action: string;
/**
* Start index.
*/
index: number;
/**
* Removed items.
*/
removed: Array<T>;
/**
* Number of added items.
*/
addedCount: number;
}
/**
* Change types.
*/
class ChangeType {
static Add: string;
static Delete: string;
static Update: string;
static Splice: string;
}
/**
* Advanced array like class used when you want to be notified when a change occurs.
*/
class ObservableArray<T> extends observable.Observable {
on(event: string, callback: (data: observable.EventData) => void);
/**
* Raised when a change occurs.
*/
on(event: "change", callback: (args: ChangedData<T>) => void);
/**
* Create ObservableArray<T> with specified length.
*/
constructor(arrayLength?: number);
/**
* Create ObservableArray<T> from source Array<T>.
*/
constructor(items: T[]);
/**
* Create ObservableArray<T> from T items.
*/
constructor(...items: T[]);
/**
* Returns item at specified index.
*/
getItem(index: number): T;
/**
* Sets item at specified index.
*/
setItem(index: number, value: T): void;
/**
* Returns a string representation of an array.
*/
toString(): string;
toLocaleString(): string;
/**
* Combines two or more arrays.
* @param items Additional items to add to the end of array1.
*/
concat<U extends T[]>(...items: U[]): T[];
/**
* Combines two or more arrays.
* @param items Additional items to add to the end of array1.
*/
concat(...items: T[]): T[];
/**
* Adds all the elements of an array separated by the specified separator string.
* @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.
*/
join(separator?: string): string;
/**
* Removes the last element from an array and returns it.
*/
pop(): T;
/**
* Appends new elements to an array, and returns the new length of the array.
* @param items New elements of the Array.
*/
push(items: T[]): number;
/**
* Appends new elements to an array, and returns the new length of the array.
* @param items New elements of the Array.
*/
push(...items: T[]): number;
/**
* Reverses the elements in an Array.
*/
reverse(): T[];
/**
* Removes the first element from an array and returns it.
*/
shift(): T;
/**
* Returns a section of an array.
* @param start The beginning of the specified portion of the array.
* @param end The end of the specified portion of the array.
*/
slice(start?: number, end?: number): T[];
/**
* Sorts an array.
* @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.
*/
sort(compareFn?: (a: T, b: T) => number): T[];
/**
* 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.
*/
splice(start: number): T[];
/**
* 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[];
/**
* Inserts new elements at the start of an array.
* @param items Elements to insert at the start of the Array.
*/
unshift(...items: T[]): number;
/**
* Returns the index of the first occurrence of a value in an array.
* @param searchElement The value to locate in the array.
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
*/
indexOf(searchElement: T, fromIndex?: number): number;
/**
* Returns the index of the last occurrence of a specified value in an array.
* @param searchElement The value to locate in the array.
* @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.
*/
lastIndexOf(searchElement: T, fromIndex?: number): number;
/**
* Determines whether all the members of an array satisfy the specified test.
* @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.
* @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.
*/
every(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean;
/**
* Determines whether the specified callback function returns true for any element of an array.
* @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.
* @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.
*/
some(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean;
/**
* Performs the specified action for each element in an array.
* @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
* @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.
*/
forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void;
/**
* Calls a defined callback function on each element of an array, and returns an array that contains the results.
* @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.
* @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.
*/
map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[];
/**
* Returns the elements of an array that meet the condition specified in a callback function.
* @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.
* @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.
*/
filter(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): T[];
/**
* 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.
* @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.
* @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.
*/
reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T;
/**
* 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.
* @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.
* @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.
*/
reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U;
/**
* 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.
* @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.
* @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.
*/
reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T;
/**
* 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.
* @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.
* @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.
*/
reduceRight<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U;
/**
* Gets or sets the length of the array. This is a number one higher than the highest element defined in an array.
*/
length: number;
}
}

View File

@@ -0,0 +1,318 @@
import observable = require("data/observable");
import observableArrayDef = require("data/observable-array");
export class ChangeType implements observableArrayDef.ChangeType {
static Add = "add";
static Delete = "delete";
static Update = "update";
static Splice = "splice";
}
var CHANGE = "change";
export module knownEvents {
export var change = CHANGE;
}
export class ObservableArray<T> extends observable.Observable implements observableArrayDef.ObservableArray<T> { // implements Array<T> {
//[n: number]: T;
private _array: Array<any>;
private _addArgs: observableArrayDef.ChangedData<T>;
private _deleteArgs: observableArrayDef.ChangedData<T>;
constructor() {
super();
if (arguments.length === 1 && Array.isArray(arguments[0])) {
this._array = arguments[0].slice();
}
else {
this._array = Array.apply(null, arguments);
}
this._addArgs = {
eventName: CHANGE, object: this,
action: ChangeType.Add,
index: null,
removed: new Array(),
addedCount: 1
};
this._deleteArgs = {
eventName: CHANGE, object: this,
action: ChangeType.Delete,
index: null,
removed: null,
addedCount: 0
};
}
getItem(index: number): T {
return this._array[index];
}
setItem(index: number, value: T) {
this._array[index] = value;
this.notify(<observableArrayDef.ChangedData<T>>{
eventName: CHANGE, object: this,
action: ChangeType.Update,
index: index,
removed: new Array(1),
addedCount: 1
});
}
/**
* Gets or sets the length of the array. This is a number one higher than the highest element defined in an array.
*/
get length(): number {
return this._array.length;
}
/**
* Returns a string representation of an array.
*/
toString(): string {
return this._array.toString();
}
toLocaleString(): string {
return this._array.toLocaleString();
}
/**
* Combines two or more arrays.
* @param items Additional items to add to the end of array1.
*/
concat(): T[] {
this._addArgs.index = this._array.length;
var result = this._array.concat.apply(this._array, arguments);
this._addArgs.addedCount = result.length - this._array.length;
this.notify(this._addArgs);
return result;
}
/**
* Adds all the elements of an array separated by the specified separator string.
* @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.
*/
join(separator?: string): string {
return this._array.join(separator);
}
/**
* Removes the last element from an array and returns it.
*/
pop(): T {
this._deleteArgs.index = this._array.length - 1;
var result = this._array.pop();
this._deleteArgs.removed = [result];
this.notify(this._deleteArgs);
return result;
}
/**
* Appends new elements to an array, and returns the new length of the array.
* @param item New element of the Array.
*/
push(): number {
this._addArgs.index = this._array.length;
if (arguments.length === 1 && Array.isArray(arguments[0])) {
var source = <Array<T>>arguments[0];
for (var i = 0, l = source.length; i < l; i++) {
this._array.push(source[i]);
}
}
else {
this._array.push.apply(this._array, arguments);
}
this._addArgs.addedCount = this._array.length - this._addArgs.index;
this.notify(this._addArgs);
return this._array.length;
}
/**
* Reverses the elements in an Array.
*/
reverse(): T[] {
return this._array.reverse();
}
/**
* Removes the first element from an array and returns it.
*/
shift(): T {
var result = this._array.shift();
this._deleteArgs.index = 0;
this._deleteArgs.removed = [result];
this.notify(this._deleteArgs);
return result;
}
/**
* Returns a section of an array.
* @param start The beginning of the specified portion of the array.
* @param end The end of the specified portion of the array.
*/
slice(start?: number, end?: number): T[] {
return this._array.slice(start, end);
}
/**
* Sorts an array.
* @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.
*/
sort(compareFn?: (a: T, b: T) => number): T[] {
return this._array.sort(compareFn);
}
/**
* 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): T[] {
var length = this._array.length;
var result = this._array.splice.apply(this._array, arguments);
this.notify(<observableArrayDef.ChangedData<T>>{
eventName: CHANGE, object: this,
action: ChangeType.Splice,
index: start,
removed: result,
addedCount: this._array.length > length ? this._array.length - length : 0
});
return result;
}
/**
* Inserts new elements at the start of an array.
* @param items Elements to insert at the start of the Array.
*/
unshift(): number {
var length = this._array.length;
var result = this._array.unshift.apply(this._array, arguments);
this._addArgs.index = 0;
this._addArgs.addedCount = result - length;
this.notify(this._addArgs);
return result;
}
/**
* Returns the index of the first occurrence of a value in an array.
* @param searchElement The value to locate in the array.
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
*/
indexOf(searchElement: T, fromIndex?: number): number {
var index = fromIndex ? fromIndex : 0;
for (var i = index, l = this._array.length; i < l; i++) {
if (this._array[i] === searchElement) {
return i;
}
}
return -1;
}
/**
* Returns the index of the last occurrence of a specified value in an array.
* @param searchElement The value to locate in the array.
* @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.
*/
lastIndexOf(searchElement: T, fromIndex?: number): number {
var index = fromIndex ? fromIndex : this._array.length - 1;
for (var i = index; i >= 0; i--) {
if (this._array[i] === searchElement) {
return i;
}
}
return -1;
}
/**
* Determines whether all the members of an array satisfy the specified test.
* @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.
* @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.
*/
every(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean {
return this._array.every(callbackfn, thisArg);
}
/**
* Determines whether the specified callback function returns true for any element of an array.
* @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.
* @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.
*/
some(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean {
return this._array.some(callbackfn, thisArg);
}
/**
* Performs the specified action for each element in an array.
* @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
* @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.
*/
forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void {
this._array.forEach(callbackfn, thisArg);
}
/**
* Calls a defined callback function on each element of an array, and returns an array that contains the results.
* @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.
* @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.
*/
map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[] {
return this._array.map(callbackfn, thisArg);
}
/**
* Returns the elements of an array that meet the condition specified in a callback function.
* @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.
* @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.
*/
filter(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): T[] {
return this._array.filter(callbackfn, thisArg);
}
/**
* 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.
* @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.
* @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.
*/
reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T {
return this._array.reduce(callbackfn, initialValue);
}
/**
* 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.
* @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.
* @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.
*/
reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T {
return this._array.reduceRight(callbackfn, initialValue);
}
}

View File

@@ -0,0 +1,4 @@
{
"name" : "observable-array",
"main" : "observable-array.js"
}

109
data/observable/observable.d.ts vendored Normal file
View File

@@ -0,0 +1,109 @@
/**
* Contains the Observable class, which represents an observable object, or "data" in the model-view paradigm.
*/
declare module "data/observable" {
/**
* Base event data.
*/
interface EventData {
/**
* The name of the event.
*/
eventName: string;
/**
* The Observable instance that has raised the event.
*/
object: Observable;
}
/**
* Data for the "propertyChange" event.
*/
interface PropertyChangeData extends EventData {
/**
* The name of the property that has changed.
*/
propertyName: string;
/**
* The new value of the property.
*/
value: any;
}
/**
* Known event names.
*/
module knownEvents {
/**
* The name of the property changed event.
*/
export var propertyChange: string;
}
/**
* Observable is used when you want to be notified when a change occurs. Use on/off methods to add/remove listener.
*/
class Observable {
/**
* Gets the name of the constructor function for this instance. E.g. for a Button class this will return "Button".
*/
typeName: string;
/**
* Shortcut alias to the addEventListener method.
*/
on(eventNames: string, callback: (data: EventData) => void, thisArg?: any);
/**
* Shortcut alias to the removeEventListener method.
*/
off(eventNames: string, callback?: any);
/**
* Adds a listener for the specified event name.
* @param eventNames Comma delimited names of the events to attach the listener to.
* @param callback A function to be called when some of the specified event(s) is raised.
* @param thisArg An optional parameter which when set will be used as "this" in callback method call.
*/
addEventListener(eventNames: string, callback: (data: EventData) => void, thisArg?: any);
/**
* Removes listener(s) for the specified event name.
* @param eventNames Comma delimited names of the events the specified listener is associated with.
* @param callback An optional parameter pointing to a specific listener. If not defined, all listeners for the event names will be removed.
* @param thisArg An optional parameter which when set will be used to refine search of the correct callback which will be removed as event listener.
*/
removeEventListener(eventNames: string, callback?: any, thisArg?: any);
/**
* Updates the specified property with the provided value.
*/
set(name: string, value: any): void;
/**
* Gets the value of the specified property.
*/
get(name: string): any;
/**
* Notifies all the registered listeners for the event provided in the data.eventName.
* @param data The data associated with the event.
*/
notify(data: EventData): void;
/**
* Checks whether a listener is registered for the specified event name.
* @param eventName The name of the event to check for.
*/
hasListeners(eventName: string): boolean;
//@private
/**
* This method is intended to be overriden by inheritors to provide additional implementation.
*/
_setCore(data: PropertyChangeData);
_createPropertyChangeData(name: string, value: any): PropertyChangeData;
_emit(eventNames: string);
//@endprivate
}
}

View File

@@ -0,0 +1,169 @@
import types = require("utils/types");
import definition = require("data/observable");
interface ListenerEntry {
callback: (data: definition.EventData) => void;
thisArg: any;
}
export module knownEvents {
export var propertyChange = "propertyChange";
}
export class Observable implements definition.Observable {
private _observers = {};
get typeName(): string {
return types.getClass(this);
}
public on(eventNames: string, callback: (data: definition.EventData) => void, thisArg?: any) {
this.addEventListener(eventNames, callback, thisArg);
}
public off(eventNames: string, callback?: any) {
this.removeEventListener(eventNames, callback);
}
public addEventListener(eventNames: string, callback: (data: definition.EventData) => void, thisArg?: any) {
types.verifyCallback(callback);
var events: Array<string> = eventNames.split(",");
for (var i = 0, l = events.length; i < l; i++) {
var event = events[i].trim();
var list = this._getEventList(event, true);
// TODO: Performance optimization - if we do not have the thisArg specified, do not wrap the callback in additional object (ObserveEntry)
list.push({
callback: callback,
thisArg: thisArg
});
}
}
public removeEventListener(eventNames: string, callback?: any, thisArg?: any) {
var events: Array<string> = eventNames.split(",");
for (var i = 0, l = events.length; i < l; i++) {
var event = events[i].trim();
if (callback) {
var list = this._getEventList(event, false);
if (list) {
var index = this._indexOfListener(list, callback, thisArg);
if (index >= 0) {
list.splice(index, 1);
}
if (list.length === 0) {
delete this._observers[event];
}
}
}
else {
this._observers[event] = undefined;
delete this._observers[event];
}
}
}
public set(name: string, value: any) {
// TODO: Parameter validation
if (this[name] === value) {
return;
}
// create data for the change
var data = this._createPropertyChangeData(name, value);
this._setCore(data);
this.notify(data);
// TODO: Maybe we need to update source object used in the constructor as well?
}
public get(name: string): any {
return this[name];
}
public _setCore(data: definition.PropertyChangeData) {
this[data.propertyName] = data.value;
}
public notify(data: definition.EventData) {
var observers = this._getEventList(data.eventName);
if (!observers) {
return;
}
var i;
var entry: ListenerEntry;
for (i = 0; i < observers.length; i++) {
entry = observers[i];
if (entry.thisArg) {
entry.callback.apply(entry.thisArg, [data]);
} else {
entry.callback(data);
}
}
}
public hasListeners(eventName: string) {
return eventName in this._observers;
}
public _createPropertyChangeData(name: string, value: any): definition.PropertyChangeData {
return {
eventName: knownEvents.propertyChange,
propertyName: name,
object: this,
value: value
};
}
public _emit(eventNames: string) {
var events: Array<string> = eventNames.split(",");
for (var i = 0, l = events.length; i < l; i++) {
var event = events[i].trim();
this.notify({ eventName: event, object: this });
}
}
private _getEventList(eventName: string, createIfNeeded?: boolean): Array<ListenerEntry> {
if (!eventName) {
throw new TypeError("EventName must be valid string.");
}
var list = <Array<ListenerEntry>>this._observers[eventName];
if (!list && createIfNeeded) {
list = [];
this._observers[eventName] = list;
}
return list;
}
private _indexOfListener(list: Array<ListenerEntry>, callback: (data: definition.EventData) => void, thisArg?: any): number {
var i;
var entry: ListenerEntry;
for (i = 0; i < list.length; i++) {
entry = list[i];
if (thisArg) {
if (entry.callback === callback && entry.thisArg === thisArg) {
return i;
}
}
else {
if (entry.callback === callback) {
return i;
}
}
}
return -1;
}
public toString(): string {
return this.typeName;
}
}

View File

@@ -0,0 +1,4 @@
{
"name" : "observable",
"main" : "observable.js"
}

View File

@@ -0,0 +1,4 @@
{
"name" : "virtual-array",
"main" : "virtual-array.js"
}

88
data/virtual-array/virtual-array.d.ts vendored Normal file
View File

@@ -0,0 +1,88 @@
/**
* Contains the VirtualArray class, which is an advanced array like class that helps loading items on demand.
*/
declare module "data/virtual-array" {
import observable = require("data/observable");
import observableArray = require("data/observable-array");
/**
* Known event names.
*/
module knownEvents {
export var itemsLoading: string;
export var change: string;
}
/**
* Provides event args for "changed" event.
*/
export interface ChangedData<T> extends observableArray.ChangedData<T> {
//
}
/**
* Change types (Add, Delete, Update, Splice).
*/
export class ChangeType extends observableArray.ChangeType {
//
}
/**
* Advanced array like class that helps loading items on demand.
*/
export class VirtualArray<T> extends observable.Observable {
constructor(arrayLength?: number);
/**
* Gets or sets length for the virtual array.
*/
length: number;
/**
* Gets or sets load size for the virtual array.
*/
loadSize: number;
/**
* Returns item at specified index.
*/
getItem(index: number): T;
/**
* Sets item at specified index.
*/
setItem(index: number, value: T): void;
/**
* Loads items from an array starting at index.
*/
load(index: number, items: T[]): void;
on(event: string, callback: (data: observable.EventData) => void);
/**
* Raised when still not loaded items are requested.
*/
on(event: "itemsLoading", callback: (args: ItemsLoading) => void);
/**
* Raised when a change occurs.
*/
on(event: "change", callback: (args: ChangedData<T>) => void);
}
/**
* Event args for "itemsLoading" event.
*/
export interface ItemsLoading extends observable.EventData {
/**
* Start index.
*/
index: number;
/**
* Number of items to load.
*/
count: number;
}
}

View File

@@ -0,0 +1,161 @@
import observable = require("data/observable");
import types = require("utils/types");
import virtualArrayDef = require("data/virtual-array");
var CHANGE = "change", UPDATE = "update", DELETE = "delete", ADD = "add";
export module knownEvents {
export var itemsLoading = "itemsLoading";
export var change = "change";
}
export class ChangeType implements virtualArrayDef.ChangeType {
static Add = "add";
static Delete = "delete";
static Update = "update";
static Splice = "splice";
}
export class VirtualArray<T> extends observable.Observable {
private _requestedIndexes: Array<number>;
private _loadedIndexes: Array<number>;
private _length: number;
private _cache: {};
constructor(length = 0) {
super();
this._length = length;
this._cache = {};
this._requestedIndexes = [];
this._loadedIndexes = [];
}
get length(): number {
return this._length;
}
set length(value: number) {
if (this._length !== value) {
var index = this._length;
var count = value - this._length;
this._length = value;
this.notify({
eventName: CHANGE, object: this,
action: count > 0 ? ADD : DELETE,
index: index,
removed: new Array(count < 0 ? Math.abs(count) : 0),
addedCount: count > 0 ? count : 0
});
}
}
private _loadSize: number;
get loadSize(): number {
return this._loadSize;
}
set loadSize(value: number) {
this._loadSize = value;
}
getItem(index: number): T {
var item = this._cache[index];
if (types.isUndefined(item)) {
if (index >= 0 && index < this.length && this._requestedIndexes.indexOf(index) < 0 && this._loadedIndexes.indexOf(index) < 0) {
this.requestItems(index);
}
}
return item;
}
setItem(index: number, value: T) {
if (this._cache[index] !== value) {
this.load(index, [value]);
}
}
load(index: number, items: T[]): void {
var i: number;
for (i = 0; i < items.length; i++) {
var itemIndex = index + i;
this._cache[itemIndex] = items[i];
this._requestedIndexes.splice(this._requestedIndexes.indexOf(itemIndex), 1);
if (this._loadedIndexes.indexOf(itemIndex) < 0) {
this._loadedIndexes.push(itemIndex);
}
}
// Remove requested but never loaded indexes.
if (this._requestedIndexes.length > 0) {
for (i = 0; i < this.loadSize - items.length; i++) {
this._requestedIndexes.splice(this._requestedIndexes.indexOf(index + i), 1);
}
}
this.notify({
eventName: CHANGE, object: this,
action: UPDATE,
index: index,
removed: new Array(items.length),
addedCount: items.length
});
}
private requestItems(index: number): void {
var indexesToLoad = [];
var pageIndex = this._loadSize > 0 ? this._loadSize * Math.floor(index / this._loadSize) : index;
var count = 0;
var start = -1;
for (var i = 0; i < this.loadSize; i++) {
var itemIndex = pageIndex + i;
if (itemIndex >= this._length) {
break;
}
if (this._loadedIndexes.indexOf(itemIndex) < 0) {
if (start < 0) {
start = itemIndex;
}
indexesToLoad.push(itemIndex);
if (this._requestedIndexes.indexOf(itemIndex) < 0) {
this._requestedIndexes.push(itemIndex);
}
count++;
} else {
if (count > 0) {
this.notify({
eventName: knownEvents.itemsLoading, object: this,
index: start,
count: count
});
}
start = -1;
count = 0;
}
}
if (start >= 0 && count > 0) {
this.notify({
eventName: knownEvents.itemsLoading, object: this,
index: start,
count: count
});
}
}
}