Created
December 13, 2016 15:45
-
-
Save thomhos/4e5e5275a394be5bc94d00764c64114f to your computer and use it in GitHub Desktop.
Push based state change propagation
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
/* | |
* 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