All files / renderer update.ts

9.26% Statements 5/54
0% Branches 0/36
0% Functions 0/2
9.8% Lines 5/51

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 1502x 2x 2x     2x                                                                                                                                                                                                         2x                                                                                      
import areEquivalentValues from "./areEquivalentValues";
import { createNode } from "./createNode";
import { mountNode } from "./mount";
import { NodePatchingData } from "./NodePatcher"
 
export function updateChildren(container: Node, oldPatchingData: NodePatchingData[], newPatchingData: NodePatchingData[]) {
 
    let { length: oldCount } = oldPatchingData;
 
    // Map the keyed nodes from the old children nodes
    const keyedNodes = new Map<any, Node>();
 
    for (let i = 0; i < oldCount; ++i) {
 
        const {
            node: oldChild
        } = oldPatchingData[i];
 
        if (oldChild === undefined) { // Not a patching data
 
            continue;
        }
 
        let key = (oldChild as HTMLElement).getAttribute?.('key') || null;
 
        if (key !== null) {
 
            keyedNodes.set(key, oldChild);
        }
    }
 
    const { length: newCount } = newPatchingData;
 
    for (let i = 0; i < newCount; ++i) {
 
        const oldChild = i < oldPatchingData.length ?
            oldPatchingData[i].node :
            undefined;
 
        if (oldChild === undefined) { // Mount the child
 
            mountNode(container, newPatchingData[i]);
        }
        else { // oldChild !== undefined
 
            const newChildPatchingData = newPatchingData[i];
 
            const {
                patcher,
                values
            } = newChildPatchingData;
 
            // Check for any keyed patching data
            const {
                keyIndex
            } = patcher;
 
            const valueKey = keyIndex !== undefined ? values[keyIndex].toString() : null;
 
            // Compare against a keyed node
            const oldChildKey = (oldChild as HTMLElement).getAttribute?.('key') || null;
 
            if (oldChildKey === valueKey) { // If the keys are the same patch the node with that patching data    
 
                updateNode(oldChild, oldPatchingData[i], newChildPatchingData);
            }
            else { // oldChildKey !== valueKey - Find the node that corresponds with the keyed patching data
 
                if (keyedNodes.has(valueKey)) { // Find an existing keyed node
 
                    const keyedNode = keyedNodes.get(valueKey);
 
                    // If the values of the keyed node match the ones of the oldChild then just swap them
                    if (areEquivalentValues(newChildPatchingData.values, (keyedNode as any)._$patchingData.values)) {
 
                        if (i >= container.childNodes.length) {
 
                            container.appendChild(keyedNode);
                        }
                        else {
 
                            container.insertBefore(keyedNode, container.childNodes[i]); // Notice oldNode is not being used since its position might have changed
                        }
 
                        newChildPatchingData.node = keyedNode; // Set the node of the new patching data
                    }
                    else { // Some value has changed, patch the existing node
 
                        updateNode(oldChild, oldPatchingData[i], (keyedNode as any)._$patchingData);
                    }
                }
                else { // No keyed node found, set the new child
 
                    updateNode(oldChild, oldPatchingData[i], newChildPatchingData);
                }
            }
        }
    }
 
    // Remove the extra nodes
    for (let i = oldCount - 1; i >= newCount; --i) {
 
        (oldPatchingData[i].node as HTMLElement).remove();
    }
}
 
export function updateNode(container: Node, oldPatchingData: NodePatchingData, newPatchingData: NodePatchingData) {
 
    let {
        node
    } = oldPatchingData;
 
    if (node === undefined) {
 
        throw new Error('There must be an existing node');
    }
 
    const {
        patcher: oldPatcher,
        values: oldValues,
        rules
    } = oldPatchingData;
 
    const {
        patcher,
        values
    } = newPatchingData;
 
    if (oldPatcher === patcher) {
 
        newPatchingData.rules = rules; // Set the compiled rules in the new patched data
 
        newPatchingData.node = node; // Set the node in the new patching data
 
        if (areEquivalentValues(oldPatchingData.values, newPatchingData.values)) {
 
            return; // Same patcher and same vales mean no changes to apply
        }
 
        oldPatcher.patchNode(node, rules, oldValues, values);
 
        (node as any)._$patchingData = newPatchingData;
    }
    else { // Different type of node, replace it with a new one
 
        const newNode = createNode(container, newPatchingData);
 
        container.replaceChild(newNode, node);
    }
}