| 
									
										
										
										
											2020-02-01 11:15:58 +01:00
										 |  |  | import utils from '../services/utils.js'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 19:43:19 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * 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 | 
					
						
							| 
									
										
										
										
											2020-06-10 23:43:59 +02:00
										 |  |  |  *   which was sent out first will also be processed first by the component | 
					
						
							| 
									
										
										
										
											2020-02-29 19:43:19 +01:00
										 |  |  |  * - 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()`. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  | export default class Component { | 
					
						
							| 
									
										
										
										
											2020-02-27 00:58:10 +01:00
										 |  |  |     constructor() { | 
					
						
							| 
									
										
										
										
											2020-08-30 22:29:38 +02:00
										 |  |  |         this.componentId = `comp-` + this.sanitizedClassName + '-' + utils.randomString(8); | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  |         /** @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-08-30 22:29:38 +02:00
										 |  |  |     get sanitizedClassName() { | 
					
						
							|  |  |  |         // webpack mangles names and sometimes uses unsafe characters
 | 
					
						
							|  |  |  |         return this.constructor.name.replace(/[^A-Z0-9]/ig, "_"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-27 00:58:10 +01:00
										 |  |  |     setParent(parent) { | 
					
						
							|  |  |  |         /** @type Component */ | 
					
						
							|  |  |  |         this.parent = parent; | 
					
						
							| 
									
										
										
										
											2020-02-27 10:03:14 +01:00
										 |  |  |         return this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-16 22:14:18 +01:00
										 |  |  |     child(...components) { | 
					
						
							| 
									
										
										
										
											2020-03-16 21:16:09 +01:00
										 |  |  |         for (const component of components) { | 
					
						
							| 
									
										
										
										
											2020-03-16 22:14:18 +01:00
										 |  |  |             component.setParent(this); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             this.children.push(component); | 
					
						
							| 
									
										
										
										
											2020-03-16 21:16:09 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-24 14:53:45 +02:00
										 |  |  |     /** @returns {Promise} */ | 
					
						
							| 
									
										
										
										
											2020-02-29 19:43:19 +01:00
										 |  |  |     handleEvent(name, data) { | 
					
						
							|  |  |  |         return Promise.all([ | 
					
						
							|  |  |  |             this.initialized.then(() => this.callMethod(this[name + 'Event'], data)), | 
					
						
							|  |  |  |             this.handleEventInChildren(name, data) | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-24 14:53:45 +02:00
										 |  |  |     /** @returns {Promise} */ | 
					
						
							| 
									
										
										
										
											2020-02-29 19:43:19 +01:00
										 |  |  |     triggerEvent(name, data) { | 
					
						
							|  |  |  |         return this.parent.triggerEvent(name, data); | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-24 20:15:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-24 14:53:45 +02:00
										 |  |  |     /** @returns {Promise} */ | 
					
						
							| 
									
										
										
										
											2020-02-29 19:43:19 +01:00
										 |  |  |     handleEventInChildren(name, data) { | 
					
						
							| 
									
										
										
										
											2020-02-12 20:07:04 +01:00
										 |  |  |         const promises = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 20:15:53 +01:00
										 |  |  |         for (const child of this.children) { | 
					
						
							| 
									
										
										
										
											2020-02-16 19:21:17 +01:00
										 |  |  |             promises.push(child.handleEvent(name, data)); | 
					
						
							| 
									
										
										
										
											2020-02-12 20:07:04 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-24 20:15:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 19:43:19 +01:00
										 |  |  |         return Promise.all(promises); | 
					
						
							| 
									
										
										
										
											2020-01-24 20:15:53 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-15 09:43:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-24 14:53:45 +02:00
										 |  |  |     /** @returns {Promise} */ | 
					
						
							| 
									
										
										
										
											2020-02-29 19:43:19 +01:00
										 |  |  |     triggerCommand(name, data = {}) { | 
					
						
							| 
									
										
										
										
											2020-02-16 19:54:11 +01:00
										 |  |  |         const fun = this[name + 'Command']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 19:43:19 +01:00
										 |  |  |         if (fun) { | 
					
						
							|  |  |  |             return this.callMethod(fun, data); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             return this.parent.triggerCommand(name, data); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-02-16 19:54:11 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 10:41:21 +01:00
										 |  |  |     async callMethod(fun, data) { | 
					
						
							|  |  |  |         if (typeof fun !== 'function') { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-30 22:29:38 +02:00
										 |  |  |         const startTime = Date.now(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const promise = fun.call(this, data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const took = Date.now() - startTime; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (glob.isDev && took > 20) { // measuring only sync handlers
 | 
					
						
							|  |  |  |             console.log(`Call to ${fun.name} in ${this.componentId} took ${took}ms`); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-21 21:28:00 +01:00
										 |  |  |         if (glob.isDev) { | 
					
						
							| 
									
										
										
										
											2021-02-25 21:16:21 +01:00
										 |  |  |             await utils.timeLimit(promise, 20000, `Time limit failed on ${this.constructor.name} with ${fun.name}`); | 
					
						
							| 
									
										
										
										
											2021-02-21 21:28:00 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             // cheaper and in non-dev the extra reporting is lost anyway through reload
 | 
					
						
							|  |  |  |             await promise; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-02-15 09:43:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 23:43:59 +02:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2020-02-15 09:43:47 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-19 22:58:08 +02:00
										 |  |  | } |