All files / mframejs/binding/array classArrayObserverCreator.ts

90.91% Statements 30/33
83.33% Branches 5/6
100% Functions 6/6
90.91% Lines 30/33

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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 15929x 29x                             29x                           91x   91x   91x 91x   91x 91x                       27x 27x 27x 27x   27x 27x                     118x 118x 118x 118x                   10x 10x                         12x                 13x 13x   88x 88x             10x     10x   10x                                 27x                                            
import { ClassArrayObserver } from './classArrayObserver';
import { Cache } from '../../utils/exported';
import { IBindingContext } from '../../interface/exported';
 
// helper class (its faster this way)
const emptyObject = class {
    // nothing
};
 
 
 
/*
 * creates a observer for array on class/object
 * classRef.__observerArray[this.keyBlock]
 *
 */
export class ClassArrayObserverCreator {
    private static classRef: any;
    private static keyParts: any;
    private static keyNo: any;
    private static keyBlock: any;
 
 
 
    /**
      * create
      *
      */
    public static create(_class: IBindingContext, observerKey: string, caller: any) {
 
        this.classRef = _class && _class.$context;
 
        this.keyParts = Cache.keyMaps.getCreate(observerKey);
 
        this.keyNo = 0;
        this.keyBlock = this.keyParts[this.keyNo];
 
        ClassArrayObserverCreator.processKeys(caller);
        ClassArrayObserverCreator.clear();
 
    }
 
 
 
    /**
     * removes the observer
     *
     */
    public static remove(_class: IBindingContext, observerKey: string, caller: any) {
 
        this.classRef = _class.$context;
        this.keyParts = Cache.keyMaps.getCreate(observerKey);
        this.keyNo = 0;
        this.keyBlock = this.keyParts[this.keyNo];
 
        ClassArrayObserverCreator.removeKeys(caller);
        ClassArrayObserverCreator.clear();
 
    }
 
 
 
    /**
     * clear class
     *
     */
    private static clear() {
        this.classRef = null;
        this.keyParts = null;
        this.keyNo = null;
        this.keyBlock = null;
    }
 
 
 
    /**
     * sets next key
     *
     */
    private static nextKey() {
        this.keyNo++;
        this.keyBlock = this.keyParts[this.keyNo];
    }
 
 
 
    /**
     * added oberserver properties to object/class
     *
     */
    private static processKeys(caller: any) {
 
        // add "__obeserver" prop if it does not exist
        if (!this.classRef.__observerArray) {
            Object.defineProperty(this.classRef, '__observerArray', {
                writable: true,
                configurable: true,
                value: new emptyObject()
            });
        }
 
        // create observer
        if (!this.classRef.__observerArray[this.keyBlock]) {
            this.classRef.__observerArray[this.keyBlock] = new ClassArrayObserver(this.classRef, this.keyBlock);
            this.classRef.__observerArray[this.keyBlock].subscribe(caller);
        } else {
            this.classRef.__observerArray[this.keyBlock].subscribe(caller);
            this.classRef.__observerArray[this.keyBlock].observe();
        }
 
        // if not next
        if (this.keyNo !== this.keyParts.length - 1 && this.keyParts.length > 1) {
 
            if (this.classRef) {
                this.classRef = this.classRef[this.keyBlock];
            }
 
            this.nextKey();
            if (this.classRef) {
                this.processKeys(caller);
            }
        }
    }
 
 
 
    /**
     * removes the caller
     *
     */
    private static removeKeys(caller: any) {
 
 
        // unsubescribe
        if (this.classRef.__observerArray) {
            if (this.classRef.__observerArray[this.keyBlock]) {
                this.classRef.__observerArray[this.keyBlock].unsubscribe(caller);
            }
        }
 
 
        // if not next
        if (this.keyNo !== this.keyParts.length - 1 && this.keyParts.length > 1) {
 
            if (this.classRef) {
                this.classRef = this.classRef[this.keyBlock];
            }
 
            this.nextKey();
            if (this.classRef) {
                this.removeKeys(caller);
            }
        }
    }
 
 
 
}