UNPKG

mframejs

Version:
68 lines 2.25 kB
import { traverseAST, getBehavior } from '../ast/traverseAst'; import { createBindingExpression, removeBindingExpression } from '../createBindingExpression'; import { ContainerBehavior } from '../../container/exported'; export class PropertyObserverHandler { constructor(expression, listener) { this.value = undefined; this.isNew = true; this.expression = expression; this.listener = listener; } bind(context) { this.observing = true; this.context = context; createBindingExpression(this.expression, this.context, this); } setAst(ast) { this.ast = ast; if (!this.curBehavior) { this.connectBehavior(); } } init() { if (this.isNew) { this.isNew = false; const oldValue = this.value; const newValue = traverseAST(this.ast, this.context); this.value = newValue; this.listenerCall(newValue, oldValue); this.isNew = false; } } connectBehavior() { const behaviors = getBehavior(this.ast); if (behaviors) { behaviors.forEach((behavior) => { if (behavior.name === 'signal') { const x = ContainerBehavior.findBehavior(behavior.name); if (x) { this.curBehavior = new x(this, behavior.args); } } }); } } update() { const newValue = traverseAST(this.ast, this.context); const oldValue = this.value; this.value = newValue; this.listenerCall(newValue, oldValue); this.bind(this.context); } listenerCall(newValue, oldValue) { if (this.listener) { this.listener.call(newValue, oldValue); } } unbind() { if (this.observing) { removeBindingExpression(this.expression, this.context, this); } this.listener.caller = null; this.observing = false; this.context = null; this.listener = null; this.value = null; } } //# sourceMappingURL=propertyObserverHandler.js.map