mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	refactoring of component event system + little docs
This commit is contained in:
		| @@ -65,22 +65,24 @@ class AppContext extends Component { | |||||||
|         this.triggerEvent('initialRenderComplete'); |         this.triggerEvent('initialRenderComplete'); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async triggerEvent(name, data) { |     /** @return {Promise} */ | ||||||
|         await this.handleEvent(name, data); |     triggerEvent(name, data) { | ||||||
|  |         return this.handleEvent(name, data); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async triggerCommand(name, data = {}) { |     /** @return {Promise} */ | ||||||
|  |     triggerCommand(name, data = {}) { | ||||||
|         for (const executor of this.executors) { |         for (const executor of this.executors) { | ||||||
|             const called = await executor.handleCommand(name, data); |             const fun = executor[name + "Command"]; | ||||||
|  |  | ||||||
|             if (called) { |             if (fun) { | ||||||
|                 return; |                 return executor.callMethod(fun, data); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         console.debug(`Unhandled command ${name}, converting to event.`); |         console.debug(`Unhandled command ${name}, converting to event.`); | ||||||
|  |  | ||||||
|         await this.triggerEvent(name, data); |         return this.triggerEvent(name, data); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     getComponentByEl(el) { |     getComponentByEl(el) { | ||||||
|   | |||||||
| @@ -1,6 +1,18 @@ | |||||||
| import utils from '../services/utils.js'; | import utils from '../services/utils.js'; | ||||||
| import Mutex from "../services/mutex.js"; | import Mutex from "../services/mutex.js"; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Abstract class for all components in the Trilium's frontend. | ||||||
|  |  * | ||||||
|  |  * Contains also event implementation with following properties: | ||||||
|  |  * - event / command distribution is synchronous which among others mean that events are well ordered - event | ||||||
|  |  *   which was sent out first will also be processed first by the component since it was added to the mutex queue | ||||||
|  |  *   as the first one | ||||||
|  |  * - execution of the event / command is asynchronous - each component executes the event on its own without regard for | ||||||
|  |  *   other components. | ||||||
|  |  * - although the execution is async, we are collecting all the promises and therefore it is possible to wait until the | ||||||
|  |  *   event / command is executed in all components - by simply awaiting the `triggerEvent()`. | ||||||
|  |  */ | ||||||
| export default class Component { | export default class Component { | ||||||
|     constructor() { |     constructor() { | ||||||
|         this.componentId = `comp-${this.constructor.name}-` + utils.randomString(6); |         this.componentId = `comp-${this.constructor.name}-` + utils.randomString(6); | ||||||
| @@ -24,50 +36,40 @@ export default class Component { | |||||||
|         return this; |         return this; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async handleEvent(name, data) { |     /** @return {Promise} */ | ||||||
|         await this.initialized; |     handleEvent(name, data) { | ||||||
|  |         return Promise.all([ | ||||||
|         const fun = this[name + 'Event']; |             this.initialized.then(() => this.callMethod(this[name + 'Event'], data)), | ||||||
|  |             this.handleEventInChildren(name, data) | ||||||
|         const start = Date.now(); |         ]); | ||||||
|  |  | ||||||
|         await this.callMethod(fun, data); |  | ||||||
|  |  | ||||||
|         const end = Date.now(); |  | ||||||
|  |  | ||||||
|         if (end - start > 10 && glob.PROFILING_LOG) { |  | ||||||
|             console.log(`Event ${name} in component ${this.componentId} took ${end-start}ms`); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         await this.handleEventInChildren(name, data); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async triggerEvent(name, data) { |     /** @return {Promise} */ | ||||||
|         await this.parent.triggerEvent(name, data); |     triggerEvent(name, data) { | ||||||
|  |         return this.parent.triggerEvent(name, data); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async handleEventInChildren(name, data) { |     /** @return {Promise} */ | ||||||
|  |     handleEventInChildren(name, data) { | ||||||
|         const promises = []; |         const promises = []; | ||||||
|  |  | ||||||
|         for (const child of this.children) { |         for (const child of this.children) { | ||||||
|             promises.push(child.handleEvent(name, data)); |             promises.push(child.handleEvent(name, data)); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         await Promise.all(promises); |         return Promise.all(promises); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async triggerCommand(name, data = {}) { |     /** @return {Promise} */ | ||||||
|         const called = await this.handleCommand(name, data); |     triggerCommand(name, data = {}) { | ||||||
|  |  | ||||||
|         if (!called) { |  | ||||||
|             await this.parent.triggerCommand(name, data); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     async handleCommand(name, data) { |  | ||||||
|         const fun = this[name + 'Command']; |         const fun = this[name + 'Command']; | ||||||
|  |  | ||||||
|         return await this.callMethod(fun, data); |         if (fun) { | ||||||
|  |             return this.callMethod(fun, data); | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             return this.parent.triggerCommand(name, data); | ||||||
|  |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async callMethod(fun, data) { |     async callMethod(fun, data) { | ||||||
|   | |||||||
| @@ -38,7 +38,7 @@ const TPL = ` | |||||||
| export default class NoteInfoWidget extends CollapsibleWidget { | export default class NoteInfoWidget extends CollapsibleWidget { | ||||||
|     getWidgetTitle() { return "Note info"; } |     getWidgetTitle() { return "Note info"; } | ||||||
|  |  | ||||||
|     doRenderBody() { |     async doRenderBody() { | ||||||
|         this.$body.html(TPL); |         this.$body.html(TPL); | ||||||
|  |  | ||||||
|         this.$noteId = this.$body.find(".note-info-note-id"); |         this.$noteId = this.$body.find(".note-info-note-id"); | ||||||
|   | |||||||
| @@ -17,15 +17,17 @@ export default class TabCachingWidget extends TabAwareWidget { | |||||||
|         return this.$widget = $(`<div class="marker" style="display: none;">`); |         return this.$widget = $(`<div class="marker" style="display: none;">`); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async handleEventInChildren(name, data) { |     handleEventInChildren(name, data) { | ||||||
|         // stop propagation of the event to the children, individual tab widget should not know about tab switching |         // stop propagation of the event to the children, individual tab widget should not know about tab switching | ||||||
|         // since they are per-tab |         // since they are per-tab | ||||||
|         if (name === 'tabNoteSwitchedAndActivated') { |         if (name === 'tabNoteSwitchedAndActivated') { | ||||||
|             await super.handleEventInChildren('tabNoteSwitched', data); |             return super.handleEventInChildren('tabNoteSwitched', data); | ||||||
|         } |         } | ||||||
|         else if (name !== 'activeTabChanged') { |         else if (name !== 'activeTabChanged') { | ||||||
|             await super.handleEventInChildren(name, data); |             return super.handleEventInChildren(name, data); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         return Promise.resolve(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async newTabOpenedEvent({tabId}) { |     async newTabOpenedEvent({tabId}) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user