10x speed-up of event propagation

This commit is contained in:
zadam 2022-06-23 23:03:35 +02:00
parent ae46b3df58
commit 42e262a1c2
3 changed files with 25 additions and 9 deletions

View File

@ -16,7 +16,7 @@ export default class Component {
this.componentId = `comp-` + this.sanitizedClassName + '-' + utils.randomString(8); this.componentId = `comp-` + this.sanitizedClassName + '-' + utils.randomString(8);
/** @type Component[] */ /** @type Component[] */
this.children = []; this.children = [];
this.initialized = Promise.resolve(); this.initialized = null;
} }
get sanitizedClassName() { get sanitizedClassName() {
@ -42,10 +42,16 @@ export default class Component {
/** @returns {Promise} */ /** @returns {Promise} */
handleEvent(name, data) { handleEvent(name, data) {
return Promise.all([ const callMethodPromise = this.initialized
this.initialized.then(() => this.callMethod(this[name + 'Event'], data)), ? this.initialized.then(() => this.callMethod(this[name + 'Event'], data))
this.handleEventInChildren(name, data) : this.callMethod(this[name + 'Event'], data);
]);
const childrenPromise = this.handleEventInChildren(name, data);
// don't create promises if not needed (optimization)
return callMethodPromise && childrenPromise
? Promise.all([callMethodPromise, childrenPromise])
: null;
} }
/** @returns {Promise} */ /** @returns {Promise} */
@ -61,7 +67,8 @@ export default class Component {
promises.push(child.handleEvent(name, data)); promises.push(child.handleEvent(name, data));
} }
return Promise.all(promises); // don't create promises if not needed (optimization)
return promises.find(p => p) ? Promise.all(promises) : null;
} }
/** @returns {Promise} */ /** @returns {Promise} */

View File

@ -186,9 +186,13 @@ export default class RibbonContainer extends NoteContextAwareWidget {
const activeChild = this.getActiveRibbonWidget(); const activeChild = this.getActiveRibbonWidget();
if (activeChild && (refreshActiveTab || !wasAlreadyActive)) { if (activeChild && (refreshActiveTab || !wasAlreadyActive)) {
activeChild.handleEvent('noteSwitched', {noteContext: this.noteContext, notePath: this.notePath}).then(() => { const handleEventPromise = activeChild.handleEvent('noteSwitched', {noteContext: this.noteContext, notePath: this.notePath});
if (handleEventPromise) {
handleEventPromise.then(() => activeChild.focus?.());
} else {
activeChild.focus?.(); activeChild.focus?.();
}); }
} }
} else { } else {
this.lastActiveComponentId = null; this.lastActiveComponentId = null;

View File

@ -23,7 +23,12 @@ export default class RightPaneContainer extends FlexContainer {
// right pane is displayed only if some child widget is active // right pane is displayed only if some child widget is active
// we'll reevaluate the visibility based on events which are probable to cause visibility change // we'll reevaluate the visibility based on events which are probable to cause visibility change
// but these events needs to be finished and only then we check // but these events needs to be finished and only then we check
promise.then(() => this.reevaluateIsEnabledCommand()); if (promise) {
promise.then(() => this.reevaluateIsEnabledCommand());
}
else {
this.reevaluateIsEnabledCommand();
}
} }
return promise; return promise;