Skip to content

Build your personal knowledge base with Trilium Notes

10x speed-up of event propagation

zadam42e262a1c2ba
diff --git a/src/public/app/widgets/component.js b/src/public/app/widgets/component.js
index b9eddeaf06..cc461c6373 100644
--- a/src/public/app/widgets/component.js
+++ b/src/public/app/widgets/component.js
@@ -16,7 +16,7 @@ export default class Component {
this.componentId = `comp-` + this.sanitizedClassName + '-' + utils.randomString(8);
/** @type Component[] */
this.children = [];
- this.initialized = Promise.resolve();
+ this.initialized = null;
}
get sanitizedClassName() {
@@ -42,10 +42,16 @@ export default class Component {
/** @returns {Promise} */
handleEvent(name, data) {
- return Promise.all([
- this.initialized.then(() => this.callMethod(this[name + 'Event'], data)),
- this.handleEventInChildren(name, data)
- ]);
+ const callMethodPromise = this.initialized
+ ? this.initialized.then(() => this.callMethod(this[name + 'Event'], 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} */
@@ -61,7 +67,8 @@ export default class Component {
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} */
diff --git a/src/public/app/widgets/containers/ribbon_container.js b/src/public/app/widgets/containers/ribbon_container.js
index c5b3715382..6cd4db53e6 100644
--- a/src/public/app/widgets/containers/ribbon_container.js
+++ b/src/public/app/widgets/containers/ribbon_container.js
@@ -186,9 +186,13 @@ export default class RibbonContainer extends NoteContextAwareWidget {
const activeChild = this.getActiveRibbonWidget();
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?.();
- });
+ }
}
} else {
this.lastActiveComponentId = null;
diff --git a/src/public/app/widgets/containers/right_pane_container.js b/src/public/app/widgets/containers/right_pane_container.js
index 204c48cb46..e991a7cff0 100644
--- a/src/public/app/widgets/containers/right_pane_container.js
+++ b/src/public/app/widgets/containers/right_pane_container.js
@@ -23,7 +23,12 @@ export default class RightPaneContainer extends FlexContainer {
// 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
// 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;