2020-07-28 09:04:04 -07:00
|
|
|
class IgniteProperty {
|
|
|
|
constructor() {
|
2020-07-28 22:23:49 -07:00
|
|
|
this.callbacks = [];
|
2020-07-28 09:04:04 -07:00
|
|
|
this._value = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
get value() {
|
|
|
|
return this._value;
|
|
|
|
}
|
|
|
|
|
|
|
|
set value(val) {
|
|
|
|
var old = this._value;
|
|
|
|
this._value = val;
|
|
|
|
|
2020-07-28 22:23:49 -07:00
|
|
|
//Invoke any callbacks letting them know the value changed.
|
|
|
|
for (var i = 0; i < this.callbacks.length; i++) {
|
|
|
|
this.callbacks[i].invoke(old, val);
|
2020-07-28 09:04:04 -07:00
|
|
|
}
|
|
|
|
}
|
2020-07-28 22:23:49 -07:00
|
|
|
|
|
|
|
attach(onChange) {
|
|
|
|
var callback = new IgnitePropertyCallback(this, onChange);
|
|
|
|
this.callbacks.push(callback);
|
|
|
|
return callback;
|
|
|
|
}
|
2020-07-28 09:04:04 -07:00
|
|
|
}
|
|
|
|
|
2020-07-28 22:23:49 -07:00
|
|
|
class IgnitePropertyCallback {
|
|
|
|
constructor(property, callback) {
|
|
|
|
this.callback = callback;
|
|
|
|
this.property = property;
|
|
|
|
}
|
2020-07-28 09:04:04 -07:00
|
|
|
|
2020-07-28 22:23:49 -07:00
|
|
|
invoke(oldValue, newValue) {
|
|
|
|
if (this.callback) {
|
|
|
|
this.callback(oldValue, newValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
|
|
|
this.callback = null;
|
|
|
|
|
|
|
|
if (this.property) {
|
|
|
|
this.property.callbacks = this.property.callbacks.filter(callback => callback != this);
|
|
|
|
this.property = null;
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 09:04:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
2020-07-28 22:23:49 -07:00
|
|
|
IgniteProperty
|
2020-07-28 09:04:04 -07:00
|
|
|
};
|