All files / src/renderer patchNode.ts

30.88% Statements 21/68
20% Branches 9/45
50% Functions 2/4
31.75% Lines 20/63

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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 18616x 16x     16x         8x   8x           8x   8x   10x   10x   10x   2x     8x                         8x             8x                             8x             8x   8x   8x   8x                             16x                                                                                                                                                                                        
import areEquivalentValues from "./areEquivalentValues";
import { createNode } from "./createNode";
import { NodePatchingData } from "./NodePatcher";
 
export function patchNode(node: Node, patchingData: NodePatchingData = null): void {
 
    const {
        patcher,
        values
    } = patchingData;
 
    Eif ((node as any)._$patchingData !== undefined) { // Patch this node
 
        const {
            patcher: oldPatcher,
            values: oldValues,
            rules
        } = (node as any)._$patchingData;
 
        for (let i = 0; i < oldValues.length; ++i) {
 
            const oldValue = oldValues[i];
 
            const newValue = values[i];
 
            if (areEquivalentValues(oldValue, newValue)) {
 
                continue;
            }
 
            Iif (Array.isArray(oldValue)) {
 
                const nonPatchingDataValues = oldValue.filter(v => v.patcher === undefined);
 
                if (nonPatchingDataValues.length > 0) { // The array has non patching data
 
                    patch(oldPatcher, rules, oldValues);
                }
                else {
 
                    patchChildren(node, oldValue, newValue);
                }
            }
            else Iif (oldValue === undefined ||
                oldValue === null) {
 
                oldPatcher.patchNode(node, rules, oldValues, values);
 
                ((node as any)._$patchingData).values = values; // Replace the old values with the new ones
            }
            else Iif (oldValue.patcher !== undefined) { // The old value is a patching data
 
                if (newValue === null) {
 
                    oldPatcher.patchNode(node, rules, oldValues, values);
 
                    ((node as any)._$patchingData).values = values; // Replace the old values with the new ones
                }
                else {
 
                    patchNode(oldValue.node, newValue);
                }
            }
            else { // The old value is NOT a patching data
 
                patch(oldPatcher, rules, oldValues);
            }
        }
    }
 
    function patch(oldPatcher: any, rules: any, oldValues: any) {
 
        Eif (oldPatcher === patcher) {
 
            oldPatcher.patchNode(node, rules, oldValues, values);
 
            ((node as any)._$patchingData).values = values; // Replace the old values with the new ones
 
            patchingData.node = node; // Set the node in the new patching data
        }
        else { // Different type of node, replace it with a new one
 
            const {
                parentNode
            } = node;
 
            const newNode = createNode(parentNode, patchingData);
 
            parentNode.replaceChild(newNode, node);
        }
    }
}
 
export function patchChildren(parentNode: Node, oldChildren: NodePatchingData[], newChildren: NodePatchingData[]) {
 
    let { length: oldChildrenCount } = oldChildren;
 
    // Map the keyed nodes from the old children nodes
    const keyedNodes = new Map<any, Node>();
 
    for (let i = 0; i < oldChildrenCount; ++i) {
 
        const {
            node: oldChild
        } = oldChildren[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: patchingDataCount } = newChildren;
 
    for (let i = 0; i < patchingDataCount; ++i) {
 
        const {
            node: oldChild
        } = oldChildren[i];
 
        if (oldChild === undefined) {
 
            const node = createNode(parentNode, newChildren[i]);
 
            if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) { // Nested children of a child
 
                throw new Error('Child cannot have nested children');
            }
 
            //container.appendChild(node);
 
            throw new Error('kuku 1');
        }
        else { // oldChild !== undefined
 
            const newChild = newChildren[i];
 
            const {
                patcher,
                values
            } = newChild;
 
            // 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    
 
                patchNode(oldChild, newChild);
            }
            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);
 
                    patchNode(oldChild, (keyedNode as any)._$patchingData);
                }
                else { // No keyed node found, set the new child
 
                    patchNode(oldChild, newChild);
                }
            }
        }
    }
 
    // Remove the extra nodes
    for (let i = oldChildrenCount - 1; i >= patchingDataCount; --i) {
 
        (oldChildren[i].node as HTMLElement).remove();
    }
}