Skip to content
Myr

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

4 files+75 added−17 removed

modified:__tests__/patch.js+33 added-1 removed
modified:src/common.js+33 added-12 removed
82 unmodified lines
83 return Object.prototype.hasOwnProperty.call(thing, prop)84}8586// given a base object, returns it if unmodified, or return the changed cloned if modified87export function finalize(base, path, patches) {87export 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.copy
1 unmodified line
93 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 line
113 finalizeNonProxiedObject(base)114 return base115}116109function generatePatches(state, path, patches, result) {117function 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}147128function finalizeObject(copy, state, path, patches) {148function 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 generate
1 unmodified line
154 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 lines
modified:src/proxy.js+8 added-4 removed
139 unmodified lines
140 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 thunk
9 unmodified lines
159 // 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 line
modified:readme.md+1 added
333 unmodified lines
334* [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)340341## How does Immer work?342
111 unmodified lines