Skip to content
Myr

refactor: makeTrapsForGetters function

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 4164f0d · Browse files · 1 file+26 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

1 file+26 added−33 removed

  • other:+26 added−33 removed

1 file+26 added−33 removed

modified:src/proxy.js+26 added-33 removed
175 unmodified lines
176 }177 return objectTraps.set.call(this, state[0], prop, value)178}179180// Used by Map and Set drafts181const reflectTraps = makeReflectTraps([182 "ownKeys",183 "has",184 "set",185 "deleteProperty",186 "defineProperty",187 "getOwnPropertyDescriptor",188 "preventExtensions",189 "isExtensible",190 "getPrototypeOf"191])192193/**194 * Map drafts195 */196184const mapGetters = {197const mapTraps = makeTrapsForGetters({198 [DRAFT_STATE]: state => state,199 size: state => source(state).size,200 has(state, prop) {201 state = source(state)
54 unmodified lines
256 keys: state => () => source(state).keys(),257 values: iterateMapValues,258 entries: iterateMapValues,259 [Symbol.iterator]: iterateMapValues247}248const mapTraps = makeTrapsForGetters(mapGetters)260})261262/** Map.prototype.values _-or-_ Map.prototype.entries */263function iterateMapValues(state, prop, receiver) {264 const getYieldable =
16 unmodified lines
281/**282 * Set drafts283 */284273const setGetters = {285const setTraps = makeTrapsForGetters({286 [DRAFT_STATE]: state => state,287 size: state => source(state).size,288 has(state, prop) {289 state = source(state)
25 unmodified lines
315 keys: iterateSetValues,316 values: iterateSetValues,317 entries: iterateSetValues,318 [Symbol.iterator]: iterateSetValues307}308const setTraps = makeTrapsForGetters(setGetters)319})320321function iterateSetValues(state, prop) {322 return () => {323 const iterator = source(state)[Symbol.iterator]()
79 unmodified lines
403 next404 })405}406407/** Create traps that all use the `Reflect` API on the `source(state)` */408function makeReflectTraps(names) {409 return names.reduce((traps, name) => {410 traps[name] = (state, ...args) => Reflect[name](source(state), ...args)411 return traps412 }, {})413}414415function makeTrapsForGetters(getters) {416 return {417 ...reflectTraps,418 get(state, prop, receiver) {419 return getters.hasOwnProperty(prop)420 ? getters[prop](state, prop, receiver)421 : Reflect.get(state, prop, receiver)422 },403 ownKeys(state) {404 return Reflect.ownKeys(source(state))405 },406 set(state, ...args) {407 return Reflect.set(source(state), ...args)408 },409 has(state, ...args) {410 return Reflect.has(source(state), ...args)411 },412 deleteProperty(state, ...args) {413 return Reflect.deleteProperty(source(state), ...args)414 },415 defineProperty(state, ...args) {416 return Reflect.defineProperty(source(state), ...args)417 },418 getOwnPropertyDescriptor(state, ...args) {419 return Reflect.getOwnPropertyDescriptor(source(state), ...args)420 },421 preventExtensions(state) {422 return Reflect.preventExtensions(source(state))423 },424 isExtensible(state) {425 return Reflect.isExtensible(source(state))426 },427 getPrototypeOf(state) {428 return Reflect.getPrototypeOf(source(state))429 },423 setPrototypeOf(state) {424 throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignore425 }426 }
1 unmodified line