All files / src/custom-element/mixins UpdateTrackerMixin.ts

62.96% Statements 17/27
50% Branches 12/24
66.67% Functions 8/12
62.96% Lines 17/27

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    11x   11x       34x                         47x       13x     2x                                                                                 13x         13x   11x           2x   2x   2x           13x   13x   13x   2x         11x
import { LifecycleHooks } from "../../virtual-dom/interfaces";
 
const UpdateTrackerMixin = Base =>
 
    class UpdateTracker extends Base implements LifecycleHooks {
 
        didMountCallback () {
 
            console.log('Mounted callback: ', this.constructor.name);
        }
 
        willUpdateCallback?: () => void;
 
        didUpdateCallback?: () => void;
 
        willUnmountCallback?: () => void;
 
        /**
         * The set of children nodes that are removed every time of one them gets mounted/updated to
         * allow to call the respective callback after in the parent after the children have been mounted/updated
         */
        private _childrenToUpdate: Set<Node> = undefined;
 
        private _initializeChildrenToUpdate() {
 
            if (this.adoptedChildren.size > 0 &&
                this._childrenToUpdate === undefined) {
 
                this._childrenToUpdate = new Set<Node>(this.adoptedChildren);
            }
        }
 
        protected willMount() {
 
            this._initializeChildrenToUpdate();
        }
 
        protected async didMount() {
 
            if (this.adoptedChildren.size == 0) { // It is a leaf node
 
                this._notifyDidMount();       
            }
        }
 
        protected childDidMount(child: Node, callback: Function) {
 
            this._childrenToUpdate.delete(child);
 
            if (this._childrenToUpdate.size === 0) { // Copy the children that need to be removed when updated
 
                this._notifyDidMount();
            }
        }
 
        private _notifyDidMount() {
 
            this.callAttributesChange();
 
            this.didMountCallback?.();
 
            if (this.adoptingParent !== null) { // Let the adopting parent know that the child was mounted/updated 
 
                this.adoptingParent.childDidMount(this);
            }
        }
 
        protected willUpdate() {
 
            this._initializeChildrenToUpdate();
        }
 
        protected async didUpdate() {
 
            if (this.adoptedChildren.size == 0) { // It is a leaf node
 
                this._notifyDidUpdate();
            }
        }
 
        protected childDidUpdate(child: Node, callback: Function) {
 
            this._childrenToUpdate.delete(child);
 
            Eif (this._childrenToUpdate.size === 0) { // Copy the children that need to be removed when updated
 
                this._notifyDidUpdate();
            }
        }
 
        private _notifyDidUpdate() {
 
            this.callAttributesChange();
 
            this.didUpdateCallback?.();
 
            if (this.adoptingParent !== null) { // Let the adopting parent know that the child was mounted/updated 
 
                this.adoptingParent.childDidUpdate(this);
            }
        }
    }
 
export default UpdateTrackerMixin;