Skip to content
Myr

Moved to single file setup

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Michel Weststrate · committed by Michel Weststrate · parent d203f56 · Browse files · 3 files+72 added−71 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+72 added−71 removed

  • other:+72 added−71 removed

3 files+72 added−71 removed

modified:src/common.js+2 added-31 removed
modified:src/immer.js+2 added-40 removed
1export {setAutoFreeze, setUseProxies} from "./common"23import {applyPatches as applyPatchesImpl} from "./patches"4import {isProxyable, getUseProxies} from "./common"5import {produceProxy} from "./proxy"6import {produceEs5} from "./es5"7
53 unmodified lines
61 ? produceProxy(baseState, producer, patchListener)62 : produceEs5(baseState, producer, patchListener)63}6464export function applyPatches(base, patches) {65 return produce(base, draft => {66 debugger67 for (let i = 0; i < patches.length; i++) {68 const patch = patches[i]69 if (patch.path.length === 0 && patch.op === "replace") {70 draft = patch.value71 } else {72 const path = patch.path.slice()73 const key = path.pop()74 const base = path.reduce((current, part) => {75 if (!current)76 throw new Error(77 "Cannot apply patch, path doesn't resolve: " +78 patch.path.join("/")79 )80 return current[part]81 }, draft)82 if (!base)83 throw new Error(84 "Cannot apply patch, path doesn't resolve: " +85 patch.path.join("/")86 )87 switch (patch.op) {88 case "replace":89 base[key] = patch.value90 break91 case "remove":92 delete base[key]93 break94 default:95 throw new Error(96 "Unsupported patch operation: " + patch.op97 )98 }99 }100 }101 return draft102 })103}65export const applyPatches = produce(applyPatchesImpl)
added:src/patches.js+68 added
1import {each} from "./common"23export function generatePatches(4 state,5 basepath,6 patches,7 inversePatches,8 baseValue,9 resultValue10) {11 if (patches)12 each(state.assigned, (key, added) => {13 const path = basepath.concat(key)14 patches.push(15 added16 ? {17 op: "replace",18 path,19 value: resultValue[key]20 }21 : {22 op: "remove",23 path24 }25 )26 inversePatches.push(27 added && !key in baseValue28 ? {op: "remove", path}29 : {op: "replace", path, value: baseValue[key]}30 )31 })32}3334export function applyPatches(draft, patches) {35 for (let i = 0; i < patches.length; i++) {36 const patch = patches[i]37 if (patch.path.length === 0 && patch.op === "replace") {38 draft = patch.value39 } else {40 const path = patch.path.slice()41 const key = path.pop()42 const base = path.reduce((current, part) => {43 if (!current)44 throw new Error(45 "Cannot apply patch, path doesn't resolve: " +46 patch.path.join("/")47 )48 return current[part]49 }, draft)50 if (!base)51 throw new Error(52 "Cannot apply patch, path doesn't resolve: " +53 patch.path.join("/")54 )55 switch (patch.op) {56 case "replace":57 base[key] = patch.value58 break59 case "remove":60 delete base[key]61 break62 default:63 throw new Error("Unsupported patch operation: " + patch.op)64 }65 }66 }67 return draft68}