All files / mframejs/binding/array arrayObserverHandler.ts

100% Statements 17/17
100% Branches 0/0
100% Functions 4/4
100% Lines 17/17

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 7129x               29x           55x 55x   55x 55x                   91x 91x 91x                     36x   36x                       27x       27x       27x 27x 27x 27x        
import { ClassArrayObserverCreator } from './classArrayObserverCreator';
import { IListener, IBindingContext } from '../../interface/exported';
 
/**
 * Register/creates a array observer with expression
 * It be called on changes and then calls listener
 *
 */
export class ArrayObserverHandler {
    private context: IBindingContext;
    private observing: boolean;
 
 
    constructor(
        private expression: string,
        private 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;
        ClassArrayObserverCreator.create(this.context, this.expression, this);
    }
 
 
 
    /**
     * Gets called by observer, it then calls the listener
     *
     */
    public update(data: any) {
        if (this.listener) {
            this.listener.call(data);
        }
        this.bind(this.context);
    }
 
 
    /**
     * Unbinds and clears all internals
     *
     */
    public unbind() {
 
        // if observing remove observer
        if (this.observing) {
            ClassArrayObserverCreator.remove(this.context, this.expression, this);
        }
 
        // remove this from caller
        this.listener.caller = null;
 
        // remove rest of internals
        // todo: Do I need to do this, Im I making grabage collection worse?
        this.listener = null;
        this.observing = false;
        this.context = null;
        this.expression = null;
    }
 
}