Created
July 13, 2017 13:06
-
-
Save quantumproducer/badf6432d01c0288db5ed16b5abb60c8 to your computer and use it in GitHub Desktop.
class Component
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 Component { | |
constructor(o) { | |
this.components = {}; | |
this.listeners = {}; | |
} | |
init(o) { | |
} | |
install(name, c) { | |
c.t = this; | |
this.components[name] = c; | |
let topics = c.interestedTopics(); | |
for (let t of topics) { | |
this.addListener(c, t); | |
} | |
} | |
addListener(c, t) { | |
if (!this.listeners[t]) { | |
this.listeners[t] = []; | |
} | |
this.listeners[t].push(c); | |
} | |
grab(name) { | |
return this.components[name]; | |
} | |
loop() { | |
} | |
loopComponents() { | |
var keys = Object.keys(this.components); | |
for (let key of keys) { | |
var c = this.components[key]; | |
this.components[key].loop(); | |
} | |
} | |
msg(title, body) { | |
let listenerGroup = this.listeners[title]; | |
if (listenerGroup) { | |
for (let listener of listenerGroup) { | |
listener.handleMessage(title, body); | |
} | |
} | |
let componentKeys = Object.keys(this.components); | |
for (let key of componentKeys) { | |
var c = this.components[key]; | |
this.components[key].msg(title, body); | |
} | |
} | |
handleMessage(title, body) { | |
} | |
interestedTopics() { | |
return []; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment