Skip to content

refactor: use utils proxy insteads of Object.defineProperty #743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/apis/inject.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ComponentInstance } from '../component'
import { hasOwn, warn, currentVMInFn, isFunction } from '../utils'
import { hasOwn, warn, currentVMInFn, isFunction, proxy } from '../utils'
import { getCurrentInstance } from '../runtimeContext'

const NOT_FOUND = {}
Expand Down Expand Up @@ -28,9 +28,9 @@ export function provide<T>(key: InjectionKey<T> | string, value: T): void {

if (!vm._provided) {
const provideCache = {}
Object.defineProperty(vm, '_provided', {
proxy(vm, '_provided', {
get: () => provideCache,
set: (v) => Object.assign(provideCache, v),
set: (v: any) => Object.assign(provideCache, v),
})
}

Expand Down
13 changes: 5 additions & 8 deletions src/reactivity/reactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
hasOwn,
noopFn,
isObject,
proxy,
} from '../utils'
import { isComponentInstance, defineComponentInstance } from '../utils/helper'
import { RefKey } from '../utils/symbols'
Expand Down Expand Up @@ -80,9 +81,7 @@ export function defineAccessControl(target: AnyObject, key: any, val?: any) {
}

setupAccessControl(val)
Object.defineProperty(target, key, {
enumerable: true,
configurable: true,
proxy(target, key, {
get: function getterHandler() {
const value = getter ? getter.call(target) : val
// if the key is equal to RefKey, skip the unwrap logic
Expand All @@ -92,7 +91,7 @@ export function defineAccessControl(target: AnyObject, key: any, val?: any) {
return value
}
},
set: function setterHandler(newVal) {
set: function setterHandler(newVal: any) {
if (getter && !setter) return

const value = getter ? getter.call(target) : val
Expand Down Expand Up @@ -206,15 +205,13 @@ export function shallowReactive(obj: any): any {
setter = property.set
}

Object.defineProperty(observed, key, {
enumerable: true,
configurable: true,
proxy(observed, key, {
get: function getterHandler() {
const value = getter ? getter.call(obj) : val
ob.dep?.depend()
return value
},
set: function setterHandler(newVal) {
set: function setterHandler(newVal: any) {
if (getter && !setter) return
if (setter) {
setter.call(obj, newVal)
Expand Down
8 changes: 3 additions & 5 deletions src/reactivity/readonly.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { reactive, Ref, UnwrapRef } from '.'
import { isArray, isPlainObject, isObject, warn } from '../utils'
import { isArray, isPlainObject, isObject, warn, proxy } from '../utils'
import { readonlySet } from '../utils/sets'
import { isReactive, observe } from './reactive'
import { isRef, RefImpl } from './ref'
Expand Down Expand Up @@ -82,15 +82,13 @@ export function shallowReadonly(obj: any): any {
getter = property.get
}

Object.defineProperty(readonlyObj, key, {
enumerable: true,
configurable: true,
proxy(readonlyObj, key, {
get: function getterHandler() {
const value = getter ? getter.call(obj) : val
ob.dep.depend()
return value
},
set(v) {
set(v: any) {
if (__DEV__) {
warn(`Set operation on key "${key}" failed: target is readonly.`)
}
Expand Down