Skip to content

Commit

Permalink
refactor: makeTrapsForGetters function
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Apr 22, 2019
1 parent 4164f0d commit 5e6dcf8
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions src/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,40 +393,30 @@ function makeIterable(next) {
})
}

/** These traps all use the `Reflect` API on the `source(state)` */
const basicTraps = [
"ownKeys",
"has",
"set",
"deleteProperty",
"defineProperty",
"getOwnPropertyDescriptor",
"preventExtensions",
"isExtensible",
"getPrototypeOf"
].reduce((traps, name) => {
traps[name] = (state, ...args) => Reflect[name](source(state), ...args)
return traps
}, {})

function makeTrapsForGetters(getters) {
return {
...basicTraps,
get(state, prop, receiver) {
return getters.hasOwnProperty(prop)
? getters[prop](state, prop, receiver)
: Reflect.get(state, prop, receiver)
},
ownKeys(state) {
return Reflect.ownKeys(source(state))
},
set(state, ...args) {
return Reflect.set(source(state), ...args)
},
has(state, ...args) {
return Reflect.has(source(state), ...args)
},
deleteProperty(state, ...args) {
return Reflect.deleteProperty(source(state), ...args)
},
defineProperty(state, ...args) {
return Reflect.defineProperty(source(state), ...args)
},
getOwnPropertyDescriptor(state, ...args) {
return Reflect.getOwnPropertyDescriptor(source(state), ...args)
},
preventExtensions(state) {
return Reflect.preventExtensions(source(state))
},
isExtensible(state) {
return Reflect.isExtensible(source(state))
},
getPrototypeOf(state) {
return Reflect.getPrototypeOf(source(state))
},
setPrototypeOf(state) {
throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignore
}
Expand Down

0 comments on commit 5e6dcf8

Please sign in to comment.