declare var Proxy: any; import Observable from './Observable'; import { IHash } from './IObservable'; export default class ObservableHash extends Observable> { private _innerValue: IHash; constructor(value?: IHash) { super(); this._innerValue = value; this.value = this.wrapHash((value instanceof Object) ? value : {}); } wrapHash(value: IHash) { return new ProxyHash( (value instanceof ProxyHash) ? Object.assign({}, value) : value, this ); } async setValue(value?: IHash) { if (this._innerValue !== value || this.alwaysNotify) { this._innerValue = value; var oldValue = this.value; value = this.wrapHash((value instanceof Object) ? value : {}); this.value = value; this.promise = this.publish(value, oldValue); await this.promise; } } } export class ProxyHash implements IHash { [index: string]: T; constructor(value?: IHash, containingObservable?: Observable>) { let inner = new Proxy(value, { set: (target: IHash, property: string, value: T, receiver: ProxyHash) => { let result = true; let oldValue = target[property]; if (oldValue !== value || containingObservable.alwaysNotify) { result = (target[property] = value) === value; if (result) { containingObservable.publish(target, target); } } return result; }, deleteProperty: (target: IHash, property: string) => { let result = delete target[property]; if (result) { containingObservable.publish(target, target); } return result; } }) as ProxyHash; return inner; } }