trilium/src/public/javascripts/widgets/component.js

38 lines
1 KiB
JavaScript
Raw Normal View History

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