WIP on ES5 impl
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 160038b · Browse files · 3 files·+14 added−5 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+14 added−5 removed
- tests:+2 added−2 removed
- other:+12 added−3 removed
Provenance
- Commit
11a2d24- Tree
88c4b2a- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
160038b
3 files changed+14 added−5 removed
modified:__tests__/patch.js+2 added-2 removed
18 unmodified lines19202122232324252627282930313233333435363781 unmodified lines18 unmodified lines test("patches are replayable", () => { expect(applyPatches(base, recordedPatches)).toEqual(res) })
test.skip("patches can be revered", () => { test("patches can be revered", () => { expect(applyPatches(res, inversePatches)).toEqual(base) }) }
describe(`patches - ${name} - proxy`, () => { setUseProxies(true) runPatchTestHelper() })
describe.skip(`patches - ${name} - es5`, () => { describe(`patches - ${name} - es5`, () => { setUseProxies(false) runPatchTestHelper() })}81 unmodified linesmodified:src/es5.js+11 added-2 removed
16 unmodified lines17 18 function createState(parent, proxy, base) {19 return {20 modified: false,21+ assigned: {}, // true: value was assigned to these props, false: was removed22 hasCopy: false,23 parent,24 base,25 proxy,21 unmodified lines47 }48 49 function set(state, prop, value) {50 assertUnfinished(state)51+ state.assigned[prop] = true // optimization; skip this if there is no listener52 if (!state.modified) {53 if (is(source(state)[prop], value)) return54 markChanged(state)55 prepareCopy(state)89 unmodified lines145 // For all other cases, we don't have to compare, as they would have been picked up by the index setters146 return false147 }148 147-export function produceEs5(baseState, producer) {149+export function produceEs5(baseState, producer, patchListener) {150 if (isProxy(baseState)) {151 // See #100, don't nest producers152 const returnValue = producer.call(baseState, baseState)153 return returnValue === undefined ? baseState : returnValue154 }155 const prevStates = states156 states = []157+ const patches = patchListener && []158+ const inversePatches = patchListener && []159 try {160 // create proxy for root161 const rootProxy = createProxy(undefined, baseState)162 // execute the thunk11 unmodified lines174 // something was returned, and it wasn't the proxy itself175 if (rootProxy[PROXY_STATE].modified)176 throw new Error(RETURNED_AND_MODIFIED_ERROR)177 result = finalize(returnValue)178+ if (patches) {179+ patches.push({op: "replace", path: [], value: result})180+ inversePatches.push({op: "replace", path: [], value: baseState})181+ }174- } else result = finalize(rootProxy)182+ } else result = finalize(rootProxy, [], patches, inversePatches)183 // make sure all proxies become unusable184 each(states, (_, state) => {185 state.finished = true186 })187+ patchListener && patchListener(patches, inversePatches)188 return result189 } finally {190 states = prevStates191 }34 unmodified linesmodified:src/proxy.js+1 added-1 removed
41 unmodified lines42 43 function createState(parent, base) {44 return {45 modified: false, // this tree is modified (either this object or one of it's children)46- assigned: {}, // true: value was assigned to these props, false: was removed // TODO: optimize if not recording46+ assigned: {}, // true: value was assigned to these props, false: was removed47 finalized: false,48 parent,49 base,50 copy: undefined,127 unmodified lines