Skip to content
Myr

refactor: rename "source" helper to "latest"

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Alec Larson · committed by Alec Larson · parent b6b542d · Browse files · 2 files+27 added−33 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+27 added−33 removed

  • other:+27 added−33 removed

2 files+27 added−33 removed

modified:src/es5.js+4 added-4 removed
62 unmodified lines
63function revoke() {64 this.revoked = true65}6667function source(state) {67function latest(state) {68 return state.copy || state.base69}7071// Access a property without creating an Immer draft.
9 unmodified lines
81}8283function get(state, prop) {84 assertUnrevoked(state)85 const value = peek(source(state), prop)85 const value = peek(latest(state), prop)86 if (state.finalizing) return value87 // Create a draft if the value is unmodified.88 if (value === peek(state.base, prop) && isDraftable(value)) {89 prepareCopy(state)
5 unmodified lines
95function set(state, prop, value) {96 assertUnrevoked(state)97 state.assigned[prop] = true98 if (!state.modified) {99 if (is(value, peek(source(state), prop))) return99 if (is(value, peek(latest(state), prop))) return100 markChanged(state)101 prepareCopy(state)102 }103 state.copy[prop] = value
43 unmodified lines
147function assertUnrevoked(state) {148 if (state.revoked === true)149 throw new Error(150 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +151 JSON.stringify(source(state))151 JSON.stringify(latest(state))152 )153}154155// This looks expensive, but only proxies are visited, and only objects without known changes are scanned.
108 unmodified lines
modified:src/proxy.js+23 added-29 removed
87 unmodified lines
88 if (!state.modified && has(drafts, prop)) {89 return drafts[prop]90 }9192 const value = source(state)[prop]92 const value = latest(state)[prop]93 if (state.finalized || !isDraftable(value)) {94 return value95 }96
7 unmodified lines
104105 return (drafts[prop] = createProxy(value, state))106 },107 has(state, prop) {108 return prop in source(state)108 return prop in latest(state)109 },110 ownKeys(state) {111 return Reflect.ownKeys(source(state))111 return Reflect.ownKeys(latest(state))112 },113 set(state, prop, value) {114 if (!state.modified) {115 const baseValue = peek(state.base, prop)
21 unmodified lines
137 },138 // Note: We never coerce `desc.value` into an Immer draft, because we can't make139 // the same guarantee in ES5 mode.140 getOwnPropertyDescriptor(state, prop) {141 const owner = source(state)141 const owner = latest(state)142 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)143 if (desc) {144 desc.writable = true145 desc.configurable = !Array.isArray(owner) || prop !== "length"
53 unmodified lines
199 */200201const mapTraps = makeTrapsForGetters({202 [DRAFT_STATE]: state => state,203 size: state => source(state).size,204 has(state, prop) {205 state = source(state)206 return state.has.bind(state)207 },203 size: state => latest(state).size,204 has: state => key => latest(state).has(key),205 set: state => (key, value) => {209 const stateSource = source(state)210 if (!stateSource.has(key) || stateSource.get(key) !== value) {206 const values = latest(state)207 if (!values.has(key) || values.get(key) !== value) {208 markChanged(state)209 state.assigned.set(key, true)210 state.copy.set(key, value)211 }212 return state.draft213 },214 delete: state => key => {218 if (source(state).has(key)) {215 if (latest(state).has(key)) {216 markChanged(state)217 state.assigned.set(key, false)218 return state.copy.delete(key)219 }
1 unmodified line
221 },222 clear: state => () => {223 markChanged(state)224 state.assigned = new Map()228 for (const key of source(state).keys()) {225 for (const key of latest(state).keys()) {226 state.assigned.set(key, false)227 }228 return state.copy.clear()229 },230 forEach: (state, _, receiver) => (cb, thisArg) =>234 source(state).forEach((_, key, map) => {231 latest(state).forEach((_, key, map) => {232 const value = receiver.get(key)233 cb.call(thisArg, value, key, map)234 }),235 get: state => key => {
4 unmodified lines
240 if (state.modified && state.copy.has(key)) {241 return state.copy.get(key)242 }243247 const value = source(state).get(key)244 const value = latest(state).get(key)245246 if (state.finalized || !isDraftable(value)) {247 return value248 }
7 unmodified lines
256 }257258 return draft259 },263 keys: state => () => source(state).keys(),260 keys: state => () => latest(state).keys(),261 values: iterateMapValues,262 entries: iterateMapValues,263 [Symbol.iterator]: iterateMapValues264})
3 unmodified lines
268 const getYieldable =269 prop === "values" ? (key, value) => value : (key, value) => [key, value]270271 return () => {275 const iterator = source(state)[Symbol.iterator]()272 const iterator = latest(state)[Symbol.iterator]()273 return makeIterable(() => {274 const result = iterator.next()275 if (!result.done) {276 const [key] = result.value
10 unmodified lines
287 */288289const setTraps = makeTrapsForGetters({290 [DRAFT_STATE]: state => state,294 size: state => source(state).size,295 has(state, prop) {296 state = source(state)297 return state.has.bind(state)298 },291 size: state => latest(state).size,292 has: state => key => latest(state).has(key),293 add: state => value => {300 if (!source(state).has(value)) {294 if (!latest(state).has(value)) {295 markChanged(state)296 state.copy.add(value)297 }298 return state.draft
21 unmodified lines
320})321322function iterateSetValues(state, prop) {323 return () => {330 const iterator = source(state)[Symbol.iterator]()324 const iterator = latest(state)[Symbol.iterator]()325326 return makeIterable(() => {327 const result = iterator.next()328 if (!result.done) {
27 unmodified lines
356/**357 * Helpers358 */359366// returns the object we should be reading the current value from, which is base, until some change has been made367function source(state) {360// Retrieve the latest values of the draft.361function latest(state) {362 return state.copy || state.base363}364365// Access a property without creating an Immer draft.366function peek(draft, prop) {367 const state = draft[DRAFT_STATE]368 const desc = Reflect.getOwnPropertyDescriptor(375 state ? source(state) : draft,369 state ? latest(state) : draft,370 prop371 )372 return desc && desc.value373}
30 unmodified lines
404 next405 })406}407414/** Create traps that all use the `Reflect` API on the `source(state)` */408/** Create traps that all use the `Reflect` API on the `latest(state)` */409function makeReflectTraps(names) {410 return names.reduce((traps, name) => {417 traps[name] = (state, ...args) => Reflect[name](source(state), ...args)411 traps[name] = (state, ...args) => Reflect[name](latest(state), ...args)412 return traps413 }, {})414}415
13 unmodified lines