Skip to content

Instantly share code, notes, and snippets.

@superMDguy
Last active September 25, 2017 20:36
Show Gist options
  • Save superMDguy/5ddbeb3cb516a22d8452be6603c07923 to your computer and use it in GitHub Desktop.
Save superMDguy/5ddbeb3cb516a22d8452be6603c07923 to your computer and use it in GitHub Desktop.
// See https://github.com/vuejs/vue/blob/be9ac624c81bb698ed75628fe0cbeaba4a2fc991/src/core/observer/watcher.js
// for full implementation
class Watcher {
constructor(getter, cb) {
this.getter = getter // function that returns a value based on reactive properties
this.cb = cb // function that is run on value updates, and is passed value and old value
this.value = this.get()
this.cb(this.value, null)
}
get() {
pushTarget(this) // from dep.js
const value = this.getter()
popTarget() // from dep.js
return value
}
addDep(dep) {
dep.addSub(this)
}
update() {
const value = this.get()
const oldValue = this.value
this.value = value
this.cb(value, oldValue)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment