Use proxy instead of defineProperty

This commit is contained in:
Vadim
2021-10-05 19:33:38 +03:00
parent 04fc80ecec
commit 54e219b883

View File

@@ -85,23 +85,18 @@ 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, {
get() {
subscribe(getTarget(), { key, value: currentValue })
return currentValue
}, },
set(newVal) { set(_, key, value) {
currentValue = newVal Reflect.set(...arguments)
onChange && onChange(key, newVal) onChange && onChange(key, value)
notify(_data) notify(_data)
return true
}, },
}) })
})
Object.entries(watchers).forEach(([watcherName, watcher]) => { Object.entries(watchers).forEach(([watcherName, watcher]) => {
watch(watcherName, () => watcher(_data)) watch(watcherName, () => watcher(_data))