All files / mframejs/binding/property classPropertyObserverCreator.ts

100% Statements 33/33
100% Branches 12/12
100% Functions 6/6
100% Lines 33/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 159 160 16129x 29x                           29x                             4696x 4696x   4696x 4696x     4696x 4696x                       2021x 2021x 2021x 2021x   2021x 2021x                     6717x 6717x 6717x 6717x                   353x 353x                           1098x                 3076x 3076x   1891x 1891x               271x     271x   271x                                 2103x                 82x     82x   82x                
import { ClassPropertyObserver } from './classPropertyObserver';
import { Cache } from '../../utils/exported';
import { IBindingContext } from '../../interface/exported';
 
// helper class (its faster this way)
const emptyObject = class {
    // nothing
};
 
 
/*
 * creates a observer for property on class/object
 * classRef.__observer[this.keyBlock]
 *
 */
export class ClassPropertyObserverCreator {
    // private static curClass: any;
    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];
 
 
        ClassPropertyObserverCreator.processKeys(caller);
        ClassPropertyObserverCreator.clear();
 
    }
 
 
 
    /**
     * remove observer
     *
     */
    public static remove(_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.keyParts[this.keyNo];
 
        ClassPropertyObserverCreator.removeKeys(caller);
        ClassPropertyObserverCreator.clear();
 
    }
 
 
 
    /**
     * clear internal
     *
     */
    private static clear() {
        this.classRef = null;
        this.keyParts = null;
        this.keyNo = null;
        this.keyBlock = null;
    }
 
 
 
    /**
     * set next key
     *
     */
    private static nextKey() {
        this.keyNo++;
        this.keyBlock = this.keyParts[this.keyNo];
 
    }
 
 
 
    /**
     * process key
     *
     */
    private static processKeys(caller: any) {
 
        // add "__obeserver" prop if it does not exist
        if (!this.classRef.__observer) {
            Object.defineProperty(this.classRef, '__observer', {
                writable: true,
                configurable: true,
                value: new emptyObject()
            });
        }
 
        // create observer
        if (!this.classRef.__observer[this.keyBlock]) {
            this.classRef.__observer[this.keyBlock] = new ClassPropertyObserver(this.classRef, this.keyBlock);
            this.classRef.__observer[this.keyBlock].subscribe(caller);
        } else {
            this.classRef.__observer[this.keyBlock].subscribe(caller);
            this.classRef.__observer[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);
            }
        }
    }
 
 
 
    /**
     * remove key
     *
     */
    private static removeKeys(caller: any) {
 
 
        // unsubscribe
        if (this.classRef && this.classRef.__observer) {
            if (this.classRef.__observer[this.keyBlock]) {
                this.classRef.__observer[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);
            }
        }
    }
 
 
 
}