Skip to content
Myr

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

3 files+14 added−5 removed

modified:__tests__/patch.js+2 added-2 removed
modified:src/es5.js+11 added-2 removed
16 unmodified lines
1718function 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 lines
47}4849function 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 lines
145 // For all other cases, we don't have to compare, as they would have been picked up by the index setters146 return false147}148147export function produceEs5(baseState, producer) {149export 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 thunk
11 unmodified lines
174 // 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 lines
modified:src/proxy.js+1 added-1 removed
41 unmodified lines
4243function 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