30 lines
781 B
JavaScript
Raw Normal View History

2020-01-15 21:36:01 +01:00
export default class Component {
/** @param {AppContext} appContext */
constructor(appContext) {
2020-01-18 18:01:16 +01:00
this.componentId = `component-${this.constructor.name}`;
2020-01-15 21:36:01 +01:00
this.appContext = appContext;
/** @type Component[] */
this.children = [];
2020-01-18 18:01:16 +01:00
this.initialized = Promise.resolve();
2020-01-15 21:36:01 +01:00
}
2020-01-18 18:01:16 +01:00
async eventReceived(name, data) {
await this.initialized;
console.log(`Received ${name} to ${this.componentId}`);
2020-01-15 21:36:01 +01:00
const fun = this[name + 'Listener'];
if (typeof fun === 'function') {
2020-01-18 18:01:16 +01:00
await fun.call(this, data);
2020-01-15 21:36:01 +01:00
}
for (const child of this.children) {
child.eventReceived(name, data);
}
}
trigger(name, data) {
this.appContext.trigger(name, data);
}
}