-
-
Save ycmjason/6891cc66e8e97caf36e5a27373662e63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import debounce from 'lodash.debounce' | |
const watchers: (() => any)[] = [] | |
const watch = (callback: () => any) => { | |
callback() | |
watchers.push(debounce(callback, 0)) // debouncing 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