vasille
Version:
The first Developer eXperience Orientated front-end framework (core library).
47 lines (46 loc) • 997 B
JavaScript
import { IValue } from "../core/ivalue.js";
import { reportError } from "../functional/safety.js";
/**
* Declares a notifiable value
* @class Reference
* @extends IValue
*/
export class Reference extends IValue {
/**
* @param value {any} the initial value
*/
constructor(value) {
super();
this.state = value;
this.onChange = new Set();
}
get $() {
return this.state;
}
set $(value) {
if (this.state !== value) {
this.state = value;
this.updateDeps(value);
}
}
on(handler) {
this.onChange.add(handler);
}
off(handler) {
this.onChange.delete(handler);
}
destroy() {
super.destroy();
this.onChange.clear();
}
updateDeps(value) {
this.onChange.forEach(handler => {
try {
handler(value);
}
catch (e) {
reportError(e);
}
});
}
}