UNPKG

vasille

Version:

The same framework which is designed to build bulletproof frontends (core library).

51 lines (50 loc) 1.05 kB
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 { /** * The encapsulated value * @type {*} */ state; /** * Array of handlers * @type {Set} * @readonly */ onChange; /** * @param value {any} the initial value */ constructor(value) { super(); this.state = value; this.onChange = new Set(); } get V() { return this.state; } set V(value) { if (this.state !== value) { this.state = value; this.onChange.forEach(handler => { try { handler(value); } catch (e) { reportError(e); } }); } } on(handler) { this.onChange.add(handler); } off(handler) { this.onChange.delete(handler); } }