Skip to content

Instantly share code, notes, and snippets.

@haipham
Forked from anteriovieira/autorun.js
Created November 22, 2017 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haipham/3406b375fa26deccff8981e984157de5 to your computer and use it in GitHub Desktop.
Save haipham/3406b375fa26deccff8981e984157de5 to your computer and use it in GitHub Desktop.
VueJs
var subscribers = []
var activeJob = null
var a = 3
var state = {
get a () {
if(subscribers.indexOf(activeJob) < 0){
subscribers.push(activeJob)
}
return a
},
set a (newValue){
a = newValue
subscribers.forEach(job => job())
}
}
var autorun = update => {
function job() {
activeJob = job
update()
activeJob = null
}
job()
}
autorun(() => {
console.log((state.a * 10))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment