Skip to content
Myr

patches.js

This file

Path

src/patches.js

109 lines · 3.8 KiB · JavaScript

Last changed by

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

History of this file

Take it

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 1–2why, one step back
1import {each} from "./common"
2

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 3–6why, one step back
3export function generatePatches(state, basePath, patches, inversePatches) {
4 Array.isArray(state.base)5 ? generateArrayPatches(state, basePath, patches, inversePatches)6 : generateObjectPatches(state, basePath, patches, inversePatches)

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 7–8why, one step back
7}
8

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 9–18why, one step back
9export function generateArrayPatches(state, basePath, patches, inversePatches) {
10 const {base, copy, assigned} = state11 const minLength = Math.min(base.length, copy.length)1213 // Look for replaced indices.14 for (let i = 0; i < minLength; i++) {15 if (assigned[i] && base[i] !== copy[i]) {16 const path = basePath.concat(i)17 patches.push({op: "replace", path, value: copy[i]})18 inversePatches.push({op: "replace", path, value: base[i]})

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 19–20why, one step back
19 }
20 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 21–29why, one step back
21
22 // Did the array expand?23 if (minLength < copy.length) {24 for (let i = minLength; i < copy.length; i++) {25 patches.push({26 op: "add",27 path: basePath.concat(i),28 value: copy[i]29 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 30–32why, one step back
30 }
31 inversePatches.push({32 op: "replace",

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 33–34why, one step back
33 path: basePath.concat("length"),
34 value: base.length

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 35why, one step back
35 })

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 36–39why, one step back
36 }
3738 // ...or did it shrink?39 else if (minLength < base.length) {

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 40–41why, one step back
40 patches.push({
41 op: "replace",

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 42–43why, one step back
42 path: basePath.concat("length"),
43 value: copy.length

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 44why, one step back
44 })

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 45–50why, one step back
45 for (let i = minLength; i < base.length; i++) {
46 inversePatches.push({47 op: "add",48 path: basePath.concat(i),49 value: base[i]50 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 51–54why, one step back
51 }
52 }53}54

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 55–56why, one step back
55function generateObjectPatches(state, basePath, patches, inversePatches) {
56 const {base, copy} = state

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 57why, one step back
57 each(state.assigned, (key, assignedValue) => {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 58–62why, one step back
58 const origValue = base[key]
59 const value = copy[key]60 const op = !assignedValue ? "remove" : key in base ? "replace" : "add"61 if (origValue === base && op === "replace") return62 const path = basePath.concat(key)

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 63–67why, one step back
63 patches.push(op === "remove" ? {op, path} : {op, path, value})
64 inversePatches.push(65 op === "add"66 ? {op: "remove", path}67 : op === "remove"

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 68–69why, one step back
68 ? {op: "add", path, value: origValue}
69 : {op: "replace", path, value: origValue}

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 70–76why, one step back
70 )
71 })72}7374export function applyPatches(draft, patches) {75 for (let i = 0; i < patches.length; i++) {76 const patch = patches[i]

improve applyPatches perfaleclarsonbefore adoptionfe82ed9

lines 77–78why, one step back
77 const {path} = patch
78 if (path.length === 0 && patch.op === "replace") {

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 79–80why, one step back
79 draft = patch.value
80 } else {

improve applyPatches perfaleclarsonbefore adoptionfe82ed9

lines 81–84why, one step back
81 let base = draft
82 for (let i = 0; i < path.length - 1; i++) {83 base = base[path[i]]84 if (!base || typeof base !== "object")

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 85why, one step back
85 throw new Error("Cannot apply patch, path doesn't resolve: " + path.join("/")) // prettier-ignore

improve applyPatches perfaleclarsonbefore adoptionfe82ed9

lines 86–87why, one step back
86 }
87 const key = path[path.length - 1]

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 88–95why, one step back
88 switch (patch.op) {
89 case "replace":90 case "add":91 // TODO: add support is not extensive, it does not support insertion or `-` atm!92 base[key] = patch.value93 break94 case "remove":95 if (Array.isArray(base)) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 96–101why, one step back
96 if (key !== base.length - 1)
97 throw new Error(`Only the last index of an array can be removed, index: ${key}, length: ${base.length}`) // prettier-ignore98 base.length -= 199 } else {100 delete base[key]101 }

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 102–109why, one step back
102 break
103 default:104 throw new Error("Unsupported patch operation: " + patch.op)105 }106 }107 }108 return draft109}