Skip to content

Instantly share code, notes, and snippets.

@ycmjason
Created February 26, 2020 16:26
Show Gist options
  • Save ycmjason/b8826b7fb07b8f312b827e8052b9a4c7 to your computer and use it in GitHub Desktop.
Save ycmjason/b8826b7fb07b8f312b827e8052b9a4c7 to your computer and use it in GitHub Desktop.
const watchers: (() => any)[] = []
const watch = (callback: () => any) => {
callback() // this is what Vue 3 watch() will do
watchers.push(callback)
}
const reactive = <T extends object>(t: T): T => {
return new Proxy(t, {
set(target, key: keyof T, value) {
target[key] = value
watchers.forEach(watcher => watcher())
return true
},
get(target, key: keyof T) {
return target[key]
},
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment