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
Provenance
- Commit
5093fd0- Tree
a1053ee- Author
Alec Larson <alec.stanford.larson@gmail.com>- Committer
Alec Larson <alec.stanford.larson@gmail.com>- Parent
4164f0d
1 file changed+26 added−33 removed
modified:src/proxy.js+26 added-33 removed
175 unmodified lines176 }177 return objectTraps.set.call(this, state[0], prop, value)178 }179 180+// Used by Map and Set drafts181+const reflectTraps = makeReflectTraps([182+ "ownKeys",183+ "has",184+ "set",185+ "deleteProperty",186+ "defineProperty",187+ "getOwnPropertyDescriptor",188+ "preventExtensions",189+ "isExtensible",190+ "getPrototypeOf"191+])192+193 /**194 * Map drafts195 */196 184-const mapGetters = {197+const mapTraps = makeTrapsForGetters({198 [DRAFT_STATE]: state => state,199 size: state => source(state).size,200 has(state, prop) {201 state = source(state)54 unmodified lines256 keys: state => () => source(state).keys(),257 values: iterateMapValues,258 entries: iterateMapValues,259 [Symbol.iterator]: iterateMapValues247-}248-const mapTraps = makeTrapsForGetters(mapGetters)260+})261 262 /** Map.prototype.values _-or-_ Map.prototype.entries */263 function iterateMapValues(state, prop, receiver) {264 const getYieldable =16 unmodified lines281 /**282 * Set drafts283 */284 273-const setGetters = {285+const setTraps = makeTrapsForGetters({286 [DRAFT_STATE]: state => state,287 size: state => source(state).size,288 has(state, prop) {289 state = source(state)25 unmodified lines315 keys: iterateSetValues,316 values: iterateSetValues,317 entries: iterateSetValues,318 [Symbol.iterator]: iterateSetValues307-}308-const setTraps = makeTrapsForGetters(setGetters)319+})320 321 function iterateSetValues(state, prop) {322 return () => {323 const iterator = source(state)[Symbol.iterator]()79 unmodified lines403 next404 })405 }406 407+/** Create traps that all use the `Reflect` API on the `source(state)` */408+function makeReflectTraps(names) {409+ return names.reduce((traps, name) => {410+ traps[name] = (state, ...args) => Reflect[name](source(state), ...args)411+ return traps412+ }, {})413+}414+415 function 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