# All files / lib/lifecycle component-did-mount.js

95.34% Statements 82/86
81.81% Branches 18/22
100% Functions 3/3
95.34% Lines 82/86

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
    872x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    415x
    415x
    415x
    415x
    415x
    344x
    344x
    344x
    415x
    415x
    72x
    72x
    415x

    415x

    2x
    2x
    2x
    390x
    415x
    415x
    415x
    415x
    415x
    415x
    415x
    415x
    83x
    83x
    415x
    415x
    205x
    205x
    205x
    415x
    415x
    415x
    415x
    415x
    390x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    307x
    307x
    307x
    307x
    307x
    307x
    751x
    537x
    537x
    307x
    307x
    307x
    307x
    307x
    307x
    200x
    200x
    307x
    307x
    307x
    17x
    17x
    107x
    107x
    307x
    307x
     import diff from '../util/binding';
    import { EMPTY, ComponentTreeCache, InstanceCache, VTree } from '../util/types';

    const { Internals } = diff;

    /**
     * @param {any} target
     * @param {VTree} vTree
     */
    const invokeRef = (target = EMPTY.OBJ, vTree) => {
      let { ref } = target.props || target;

      // Allow refs to be passed to HTML elements. When in a DOM environment
      // a Node will be passed to the ref function and assigned.
      if (!ref) {
        target = Internals.NodeCache.get(vTree);
        ref = vTree.attributes.ref;
      }

      if (typeof ref === 'function') {
        ref(target);
      }
      else if (typeof ref === 'object' && ref) {
        ref.current = target;
      }
      else if (typeof ref === 'string') {
        target.refs = { ...target.refs, [ref]: target };
      }
    };

    const invokeRefsForVTrees = (/** @type {(VTree | null)[]} */ ...vTrees) => {
      for (let i = 0; i < vTrees.length; i++) {
        const vTree = vTrees[i];

        if (!vTree) continue;

        const componentTree = ComponentTreeCache.get(vTree);
        const instance = InstanceCache.get(componentTree || vTree);

        if (vTree.childNodes.length) {
          invokeRefsForVTrees(...vTree.childNodes);
        }

        if (!instance) {
          invokeRef(Internals.NodeCache.get(vTree), vTree);
          continue;
        }

        // If any instances exist, loop through them and invoke the respective `ref`
        // logic.
        invokeRef(instance, vTree);
      }
    }

    /**
     * @param {VTree} vTree
     *
     * @returns {void}
     */
    export default function componentDidMount(vTree) {
      const componentTree = ComponentTreeCache.get(vTree);

      /** @type {VTree[]} */
      const childTrees = [];

      ComponentTreeCache.forEach((parentTree, childTree) => {
        if (parentTree === componentTree) {
          childTrees.push(childTree);
        }
      });

      invokeRefsForVTrees(vTree);

      // Only trigger a mount for the first element.
      if (childTrees.length === 0 || childTrees[0] !== vTree) {
        return;
      }

      // If there is a parent, ensure it is called recursively.
      if (ComponentTreeCache.has(componentTree)) {
        componentDidMount(componentTree);
      }

      const instance = InstanceCache.get(componentTree);
      instance && instance.componentDidMount && instance.componentDidMount();
    }