Skip to content

Instantly share code, notes, and snippets.

@quantumproducer
Created July 13, 2017 13:06
Show Gist options
  • Save quantumproducer/badf6432d01c0288db5ed16b5abb60c8 to your computer and use it in GitHub Desktop.
Save quantumproducer/badf6432d01c0288db5ed16b5abb60c8 to your computer and use it in GitHub Desktop.
class Component
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