declare var Proxy: any; import Observable from './Observable'; import { IArray } from './IObservable'; export default class ObservableArray extends Observable> { private _innerValue: Array; constructor(value?: Array) { super(); this._innerValue = value; this.value = this.wrapArray((value instanceof Array) ? value : []); } wrapArray(value: Array) { return new ProxyArray( (value instanceof ProxyArray) ? value.slice(0) : value, this ); } async setValue(value?: Array) { if (this._innerValue !== value || this.alwaysNotify) { this._innerValue = value; var oldValue = this.value; value = this.wrapArray((value instanceof Array) ? value : []); this.value = value; this.promise = this.publish(value, oldValue); await this.promise; } } } export class ProxyArray extends Array implements IArray { constructor(value?: Array, containingObservable?: Observable>) { super(); let inner = new Proxy(value, { set: (target: Array, property: string, value: T, receiver: ProxyArray) => { let result = true; let oldValue = target[property]; if (oldValue !== value || containingObservable.alwaysNotify) { result = (target[property] = value) === value; if (result && isFinite(Number(property)) || property === 'length') { containingObservable.publish(target, target); } } return result; }, deleteProperty: (target: Array, property: string) => { let result = delete target[property]; if (result && isFinite(Number(property))) { containingObservable.publish(target, target); } return result; } }) as ProxyArray; return inner; } }