Added support for property change with same object instance (via WrappedValue).

This commit is contained in:
Nedyalko Nikolov
2016-01-22 08:27:31 +02:00
parent 8159929ff7
commit 5c35f33441
4 changed files with 139 additions and 18 deletions

View File

@@ -29,6 +29,37 @@ declare module "data/observable" {
*/
value: any;
}
/**
* Helper class that is used to fire property change even when real object is the same.
* By default property change will not be fired for a same object.
* By wrapping object into a WrappedValue instance `same object restriction` will be passed.
*/
class WrappedValue {
/**
* Property which holds the real value.
*/
wrapped: any;
/**
* Creates an instance of WrappedValue object.
* @param value - the real value which should be wrapped.
*/
constructor(value: any);
/**
* Gets the real value of previously wrappedValue.
* @param value - Value that should be unwraped. If there is no wrappedValue property of the value object then value will be returned.
*/
static unwrap(value: any): any;
/**
* Returns an instance of WrappedValue. The actual instance is get from a WrappedValues pool.
* @param value - Value that should be wrapped.
*/
static wrap(value: any): WrappedValue
}
/**
* Observable is used when you want to be notified when a change occurs. Use on/off methods to add/remove listener.

View File

@@ -6,6 +6,45 @@ interface ListenerEntry {
thisArg: any;
}
var _wrappedIndex = 0;
export class WrappedValue implements definition.WrappedValue {
private _wrapped: any;
public get wrapped(): any {
return this._wrapped;
}
public set wrapped(value) {
this._wrapped = value;
}
constructor(value: any) {
this._wrapped = value;
}
public static unwrap(value: any) {
if (value && value.wrapped) {
return value.wrapped;
}
return value;
}
public static wrap(value: any) {
var w = _wrappedValues[_wrappedIndex++ % 5];
w.wrapped = value;
return w;
}
}
var _wrappedValues = [
new WrappedValue(null),
new WrappedValue(null),
new WrappedValue(null),
new WrappedValue(null),
new WrappedValue(null)
]
export class Observable implements definition.Observable {
public static propertyChangeEvent = "propertyChange";
private _map: Map<string, Object>;
@@ -126,7 +165,8 @@ export class Observable implements definition.Observable {
public _setCore(data: definition.PropertyChangeData) {
this.disableNotifications[data.propertyName] = true;
this[data.propertyName] = data.value;
let newValue = WrappedValue.unwrap(data.value);
this[data.propertyName] = newValue;
delete this.disableNotifications[data.propertyName];
}