Use proxy instead of defineProperty
This commit is contained in:
@@ -85,22 +85,17 @@ export const reactive = (data, watchers, methods, onChange) => {
|
|||||||
const { subscribe, notify } = useSubscription()
|
const { subscribe, notify } = useSubscription()
|
||||||
const { watch, getTarget } = useWatcher()
|
const { watch, getTarget } = useWatcher()
|
||||||
|
|
||||||
const _data = {}
|
const _data = new Proxy(data, {
|
||||||
|
get(target, key) {
|
||||||
Object.keys(data).forEach((key) => {
|
subscribe(getTarget(), { key, value: target[key] })
|
||||||
let currentValue = data[key]
|
return Reflect.get(...arguments)
|
||||||
|
},
|
||||||
Object.defineProperty(_data, key, {
|
set(_, key, value) {
|
||||||
get() {
|
Reflect.set(...arguments)
|
||||||
subscribe(getTarget(), { key, value: currentValue })
|
onChange && onChange(key, value)
|
||||||
return currentValue
|
notify(_data)
|
||||||
},
|
return true
|
||||||
set(newVal) {
|
},
|
||||||
currentValue = newVal
|
|
||||||
onChange && onChange(key, newVal)
|
|
||||||
notify(_data)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
Object.entries(watchers).forEach(([watcherName, watcher]) => {
|
Object.entries(watchers).forEach(([watcherName, watcher]) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user