diff --git a/src/proxy.js b/src/proxy.js index aead3291a..ecc861c03 100644 --- a/src/proxy.js +++ b/src/proxy.js @@ -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 }