All files / mframejs/binding/property propertyObserverHandler.ts

94.29% Statements 33/35
100% Branches 0/0
88.89% Functions 8/9
94.29% Lines 33/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 13829x 29x   29x                 29x           3392x 3392x           3392x 3392x                   4613x 4613x 4613x                   3392x   3392x                         3392x 3392x 3392x 3392x 3392x 3392x                       3392x   3392x                                     1221x 1221x 1221x 1221x 1221x           4613x                       2015x       2015x     2015x 2015x 2015x 2015x        
import { traverseAST, getBehavior } from '../ast/traverseAst';
import { createBindingExpression, removeBindingExpression } from '../createBindingExpression';
import { IListener, IBindingContext } from '../../interface/exported';
import { ContainerBehavior } from '../../container/exported';
 
 
 
/**
 * Register/creates a property observer with expression
 * It be called on changes to value of expression
 *
 */
export class PropertyObserverHandler {
    private expression: string;
    private ast: any;
    private context: IBindingContext;
    private observing: boolean;
    private listener: IListener;
    private value: any = undefined;
    private isNew = true;
    public curBehavior: any;
    public attributesValues: string[];
 
 
    constructor(expression: string, listener: IListener) {
        this.expression = expression;
        this.listener = listener;
    }
 
 
 
    /**
     * binds observer handler to passed in context
     *
     */
    public bind(context: IBindingContext) {
        this.observing = true;
        this.context = context;
        createBindingExpression(this.expression, this.context, this);
    }
 
 
    /**
     * gets new AST "createNewBindingExpression"
     *
     */
    public setAst(ast: Object) {
 
        this.ast = ast;
        if (!this.curBehavior) {
            this.connectBehavior();
        }
 
    }
 
 
 
    /**
     * Sets init value
     *
     */
    public 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
     * only used for signal
     *
     */
    public connectBehavior() {
        const behaviors = getBehavior(this.ast);
        if (behaviors) {
            behaviors.forEach((behavior: { name: string, args: any[] }) => {
                if (behavior.name === 'signal') {
                    const x = ContainerBehavior.findBehavior(behavior.name);
                    if (x) {
                        this.curBehavior = new x(this, behavior.args);
                    }
                }
            });
        }
    }
 
 
 
    /**
     * Gets called by observer, it then calls the listener
     *
     */
    public update() {
 
        const newValue = traverseAST(this.ast, this.context);
        const oldValue = this.value;
        this.value = newValue;
        this.listenerCall(newValue, oldValue);
        this.bind(this.context);
 
    }
 
    public listenerCall(newValue: any, oldValue: any) {
        if (this.listener) {
            this.listener.call(newValue, oldValue);
        }
    }
 
 
    /**
     * Unbinds and clears all internals
     *
     */
    public unbind() {
 
        if (this.observing) {
            removeBindingExpression(this.expression, this.context, this);
        }
 
        // remove this from caller
        this.listener.caller = null;
 
        // remove rest of internals
        this.observing = false;
        this.context = null;
        this.listener = null;
        this.value = null;
    }
 
}