Created
March 28, 2020 21:20
-
-
Save mini-eggs/f86ae76255324dd47010cea1f4ccf5e7 to your computer and use it in GitHub Desktop.
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
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