Skip to content

Instantly share code, notes, and snippets.

@thomhos
Created December 13, 2016 15:45
Show Gist options
  • Save thomhos/4e5e5275a394be5bc94d00764c64114f to your computer and use it in GitHub Desktop.
Save thomhos/4e5e5275a394be5bc94d00764c64114f to your computer and use it in GitHub Desktop.
Push based state change propagation
/*
* Each property has getters and setters with subscribers
*/
let subscribers = new Set()
let activeJob = null
let a = 3
const state = {
get a () {
subscribers.add(activeJob)
return a
},
set a (newValue) {
a = newValue
subscribers.forEach(job = job())
}
}
/*
* Autorun functions takes the update function and turns it into a job
*/
const autorun = update => {
function job () {
activeJob = job
update()
activeJob = null
}
job()
}
/*
* View updates are basically a job triggered by state changes
*/
autorun(() => {
view = render(state)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment