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 | 19x 19x 19x 19x 19x 104x 21x 83x 83x 83x 83x 83x 81x 81x 81x 9x 72x 72x 2x 2x 1x 2x 21x 21x 21x 42x 42x 42x 42x 31x 21x 21x 42x 42x 7x 35x 35x 35x 35x 35x 35x 23x 23x 1x 12x 11x 11x 11x 4x 7x 7x 11x 11x 11x 11x 1x 21x 6x | import areEquivalentValues from "./areEquivalentValues";
import createNodes from "./createNodes";
import { beginMarker } from "./createTemplate";
import mountNodes from "./mountNodes";
import { NodePatchingData } from "./NodePatcher";
export default function updateNodes(container: Node, oldPatchingData: NodePatchingData | NodePatchingData[], newPatchingData: NodePatchingData | NodePatchingData[]) {
if (Array.isArray(newPatchingData)) {
updateArrayNodes(container, oldPatchingData as NodePatchingData[], newPatchingData);
}
else {
let {
node
} = oldPatchingData as NodePatchingData;
Iif (node === undefined) {
throw new Error('There must be an existing node');
}
const {
patcher: oldPatcher,
values: oldValues,
rules
} = oldPatchingData as NodePatchingData;
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 = createNodes(newPatchingData);
if ((node as Comment).data === beginMarker) {
node.nextSibling.remove(); // Remove the end marker as well
}
container.replaceChild(newNode, node); // Replace the end marker with the node
}
}
}
function updateArrayNodes(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];
Iif (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
mountNodes(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
updateNodes(oldChild, oldPatchingData[i], newChildPatchingData);
if (i >= container.childNodes.length) { // The child was removed when replacing the nodes
container.appendChild(oldChild);
}
}
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
Eif (areEquivalentValues(newChildPatchingData.values, (keyedNode as any)._$patchingData.values)) {
if (i >= container.childNodes.length) {
container.appendChild(keyedNode);
}
else { // Replace the node
container.childNodes[i].replaceWith(keyedNode);
--oldCount; // It removes the child from the existing children
}
newChildPatchingData.node = keyedNode; // Set the node of the new patching data
const {
rules,
values
} = (keyedNode as any)._$patchingData;
newChildPatchingData.rules = rules;
newChildPatchingData.values = values; // Ensure we pass the child values with the attached nodes if any
}
else { // Some value has changed, patch the existing node
updateNodes(oldChild, oldPatchingData[i], (keyedNode as any)._$patchingData);
}
}
else { // No keyed node found, set the new child
updateNodes(oldChild, oldPatchingData[i], newChildPatchingData);
}
}
}
}
// Remove the extra nodes
for (let i = oldCount - 1; i >= newCount; --i) {
(oldPatchingData[i].node as HTMLElement).remove();
}
}
|