More tests, inverse patches
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 d821dda · Browse files · 4 files·+75 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+75 added−17 removed
- tests:+33 added−1 removed
- other:+42 added−16 removed
Provenance
- Commit
8aa82bb- Tree
b82b99a- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
d821dda
4 files changed+75 added−17 removed
modified:__tests__/patch.js+33 added-1 removed
81 unmodified lines8283848586868788899091929394959697989910010110210310410510610710810911011111211311411511611711881 unmodified lines }, [{op: "replace", path: ["z"], value: {y: 5}}, {op: "remove", path: ["x"]}])
// runPatchTest("simple replacement", { x: 3 }, _d => 4, [{ op: "replace", path: [], value: 4 }])runPatchTest( "minimum amount of changes", {x: 3, y: {a: 4}, z: 3}, d => { d.y.a = 4 d.y.b = 5 Object.assign(d, {x: 4, y: {a: 2}}) }, [ {op: "replace", path: ["x"], value: 4}, {op: "replace", path: ["y"], value: {a: 2}} ])
runPatchTest( "arrays", {x: [1, 2, 3]}, d => { d.x.unshift(4) d.x.splice(2, 1) }, [ {op: "replace", path: ["x", "0"], value: 4}, {op: "replace", path: ["x", "1"], value: 1}, {op: "replace", path: ["x", "2"], value: 3}, {op: "remove", path: ["x", "3"]}, {op: "replace", path: ["x", "length"], value: 3} ])
runPatchTest("simple replacement", {x: 3}, _d => 4, [ {op: "replace", path: [], value: 4}])modified:src/common.js+33 added-12 removed
82 unmodified lines83 return Object.prototype.hasOwnProperty.call(thing, prop)84 }85 86 // given a base object, returns it if unmodified, or return the changed cloned if modified87-export function finalize(base, path, patches) {87+export function finalize(base, path, patches, inversePatches) {88 if (isProxy(base)) {89 const state = base[PROXY_STATE]90 if (state.modified === true) {91 if (state.finalized === true) return state.copy1 unmodified line93 const result = finalizeObject(94 useProxies ? state.copy : (state.copy = shallowCopy(base)),95 state,96 path,97- patches97+ patches,98+ inversePatches99 )99- generatePatches(state, path, patches, result)100+ generatePatches(101+ state,102+ path,103+ patches,104+ inversePatches,105+ state.base,106+ result107+ )108 return result109 } else {110 return state.base111 }1 unmodified line113 finalizeNonProxiedObject(base)114 return base115 }116 109-function generatePatches(state, path, patches, result) {117+function generatePatches(118+ state,119+ basepath,120+ patches,121+ inversePatches,122+ baseValue,123+ resultValue124+) {125 if (patches)111- each(state.assigned, (key, value) => {126+ each(state.assigned, (key, added) => {127+ const path = basepath.concat(key)128 patches.push(113- value129+ added130 ? {131 op: "replace",116- path: path.concat(key),117- value: result[key]132+ path,133+ value: resultValue[key]134 }135 : {136 op: "remove",121- path: path.concat(key)137+ path138 }139 )124- // TODO: also inverse patches140+ inversePatches.push(141+ added && !key in baseValue142+ ? {op: "remove", path}143+ : {op: "replace", path, value: baseValue[key]}144+ )145 })146 }147 128-function finalizeObject(copy, state, path, patches) {148+function finalizeObject(copy, state, path, patches, inversePatches) {149 const base = state.base150 each(copy, (prop, value) => {151 if (value !== base[prop]) {152 // if there was an assignment on this property, we don't need to generate1 unmodified line154 const generatePatches = patches && !has(state.assigned, prop)155 copy[prop] = finalize(156 value,157 generatePatches && path.concat(prop),138- generatePatches && patches158+ generatePatches && patches,159+ inversePatches160 )161 }162 })163 return freeze(copy)34 unmodified linesmodified:src/proxy.js+8 added-4 removed
139 unmodified lines140 return returnValue === undefined ? baseState : returnValue141 }142 const previousProxies = proxies143 proxies = []144- const patches = [] // TODO: optimize, leave empty if no listener144+ const patches = patchListener && []145+ const inversePatches = patchListener && []146 try {147 // create proxy for root148 const rootProxy = createProxy(undefined, baseState)149 // execute the thunk9 unmodified lines159 // See #117160 // Should we just throw when returning a proxy which is not the root, but a subset of the original state?161 // Looks like a wrongly modeled reducer162 result = finalize(returnValue)162- // TODO: emit patch163+ if (patches) {164+ patches.push({op: "replace", path: [], value: result})165+ inversePatches.push({op: "replace", path: [], value: baseState})166+ }167 } else {164- result = finalize(rootProxy, [], patches)168+ result = finalize(rootProxy, [], patches, inversePatches)169 }170 // revoke all proxies171 each(proxies, (_, p) => p.revoke())168- patchListener && patchListener(patches)172+ patchListener && patchListener(patches, inversePatches)173 return result174 } finally {175 proxies = previousProxies176 }1 unmodified linemodified:readme.md+1 added
333 unmodified lines334 * [immer based handleActions](https://gist.github.com/kitze/fb65f527803a93fb2803ce79a792fff8) _Boilerplate free actions for Redux_335 * [redux-box](https://github.com/anish000kumar/redux-box) _Modular and easy-to-grasp redux based state management, with least boilerplate_336 * [quick-redux](https://github.com/jeffreyyoung/quick-redux) _tools to make redux developement quicker and easier_337 * [bey](https://github.com/jamiebuilds/bey) _Simple immutable state for React using Immer_338+* [immer-wieder](https://github.com/drcmda/immer-wieder#readme) _State management lib that combines React 16 Context and immer for Redux semantics_339 * ... and [many more](https://www.npmjs.com/browse/depended/immer)340 341 ## How does Immer work?342 111 unmodified lines