Skip to content

Instantly share code, notes, and snippets.

@mini-eggs
Created March 28, 2020 21:20
Show Gist options
  • Save mini-eggs/f86ae76255324dd47010cea1f4ccf5e7 to your computer and use it in GitHub Desktop.
Save mini-eggs/f86ae76255324dd47010cea1f4ccf5e7 to your computer and use it in GitHub Desktop.
class State {
constructor() {
this.nodes = {}
this.subscribers= [] // array of function
}
AddNode(nodeName) {
this.nodes[nodeName] = {}
this.emit()
}
AddRouteToNode(from, to, weight) {
this.nodes[from][to] = weight
this.emit()
}
emit() {
for (const func of this.subscribers) {
func(this.nodes)
}
}
subscribe(func) {
this.subscribers.push(func)
}
}
const state = new State()
class Canvas() {
constructor() {
state.subscribe(this.handleNewState)
this.render()
}
handleNewState() {
this.render()
}
render() {
// do the thing
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment