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
Provenance
- Commit
160038b- Tree
2eb5a66- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
d203f56
3 files changed+72 added−71 removed
modified:src/common.js+2 added-31 removed
123456108 unmodified linesExpand all11511611711811711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614711912012112246 unmodified linesimport {generatePatches} from "./patches"
export const PROXY_STATE = typeof Symbol !== "undefined" ? Symbol("immer-proxy-state") : "__$immer_state"108 unmodified linesExpand all finalizeNonProxiedObject(base) return base}
function generatePatches( state, basepath, patches, inversePatches, baseValue, resultValue) { if (patches) each(state.assigned, (key, added) => { const path = basepath.concat(key) patches.push( added ? { op: "replace", path, value: resultValue[key] } : { op: "remove", path } ) inversePatches.push( added && !key in baseValue ? {op: "remove", path} : {op: "replace", path, value: baseValue[key]} ) })}
function finalizeObject(copy, state, path, patches, inversePatches) { const base = state.base each(copy, (prop, value) => { if (value !== base[prop]) {46 unmodified linesmodified:src/immer.js+2 added-40 removed
1 export {setAutoFreeze, setUseProxies} from "./common"2 3+import {applyPatches as applyPatchesImpl} from "./patches"4 import {isProxyable, getUseProxies} from "./common"5 import {produceProxy} from "./proxy"6 import {produceEs5} from "./es5"7 53 unmodified lines61 ? produceProxy(baseState, producer, patchListener)62 : produceEs5(baseState, producer, patchListener)63 }64 64-export 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-}65+export const applyPatches = produce(applyPatchesImpl)added:src/patches.js+68 added
1+import {each} from "./common"2+3+export 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+}33+34+export 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+}