refactor: clean up #320
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Alec Larson · committed by Alec Larson · parent cb5707f · Browse files · 1 file·+21 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+21 added−24 removed
- other:+21 added−24 removed
Provenance
- Commit
fad74d3- Tree
b64dde6- Author
Alec Larson <alec.stanford.larson@gmail.com>- Committer
Alec Larson <alec.stanford.larson@gmail.com>- Parent
cb5707f
1 file changed+21 added−24 removed
modified:src/patches.js+21 added-24 removed
7 unmodified lines8910111212131415161718192020212223242526272829262728293031323334353637323334353637384139404142434644454647504852535455565758594950515253545556576258596061626364656667686970717264 unmodified lines7 unmodified lines
function generateArrayPatches(state, basePath, patches, inversePatches) { let {base, copy, assigned} = state
// Guarantee `base` is never longer than `copy` // Reduce complexity by ensuring `base` is never longer. if (copy.length < base.length) { ;[base, copy] = [copy, base] ;[patches, inversePatches] = [inversePatches, patches] }
const delta = copy.length - base.length
// Find the first changed index. // Find the first replaced index. let start = 0 while (base[start] === copy[start] && start < base.length) { ++start }
// Find the last changed index. let baseEnd = base.length while (baseEnd > start && base[baseEnd - 1] === copy[baseEnd + delta - 1]) { --baseEnd // Find the last replaced index. Search from the end to optimize splice patches. let end = base.length while (end > start && base[end - 1] === copy[end + delta - 1]) { --end }
// Look for replaced indices. const replaceCount = baseEnd - start for (let i = 0; i < replaceCount; ++i) { const index = start + i const path = basePath.concat([index]) if (assigned[index] && copy[index] !== base[index]) { // Process replaced indices. for (let i = start; i < end; ++i) { if (assigned[i] && copy[i] !== base[i]) { const path = basePath.concat([i]) patches.push({ op: "replace", path, value: copy[index] value: copy[i] }) inversePatches.push({ op: "replace", path, value: base[index] value: base[i] }) } } start += replaceCount
// For "add" patches that extend the array, we can use a single "replace" // patch (on the `length` property) as the inverse patch, instead of multiple // "remove" patches. const useRemove = start != base.length const replacePatchesCount = patches.length for (let i = delta - 1; i >= 0; --i) { const path = basePath.concat([start + i]) patches[i + replacePatchesCount] = { const useRemove = end != base.length const replaceCount = patches.length
// Process added indices. for (let i = end + delta - 1; i >= end; --i) { const path = basePath.concat([i]) patches[replaceCount + i - end] = { op: "add", path, value: copy[start + i] value: copy[i] } if (useRemove) { inversePatches.push({ op: "remove", path }) } }
// One "replace" patch reverses all non-splicing "add" patches. if (!useRemove) { inversePatches.push({ op: "replace", path: basePath.concat(["length"]),64 unmodified lines