mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			72 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			72 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| "use strict";
 | |
| 
 | |
| const ws = require('./ws');
 | |
| 
 | |
| // taskId => TaskContext
 | |
| const taskContexts = {};
 | |
| 
 | |
| class TaskContext {
 | |
|     constructor(taskId, taskType, data) {
 | |
|         this.taskId = taskId;
 | |
|         this.taskType = taskType;
 | |
|         this.data = data;
 | |
| 
 | |
|         // progressCount is meant to represent just some progress - to indicate the task is not stuck
 | |
|         this.progressCount = -1; // we're incrementing immediatelly
 | |
|         this.lastSentCountTs = 0; // 0 will guarantee first message will be sent
 | |
| 
 | |
|         // just the fact this has been initialized is a progress which should be sent to clients
 | |
|         // this is esp. important when importing big files/images which take long time to upload/process
 | |
|         // which means that first "real" increaseProgressCount() will be called quite late and user is without
 | |
|         // feedback until then
 | |
|         this.increaseProgressCount();
 | |
|     }
 | |
| 
 | |
|     /** @return {TaskContext} */
 | |
|     static getInstance(taskId, taskType, data) {
 | |
|         if (!taskContexts[taskId]) {
 | |
|             taskContexts[taskId] = new TaskContext(taskId, taskType, data);
 | |
|         }
 | |
| 
 | |
|         return taskContexts[taskId];
 | |
|     }
 | |
| 
 | |
|     increaseProgressCount() {
 | |
|         this.progressCount++;
 | |
| 
 | |
|         if (Date.now() - this.lastSentCountTs >= 300 && this.taskId !== 'initial-demo-import') {
 | |
|             this.lastSentCountTs = Date.now();
 | |
| 
 | |
|             ws.sendMessageToAllClients({
 | |
|                 type: 'taskProgressCount',
 | |
|                 taskId: this.taskId,
 | |
|                 taskType: this.taskType,
 | |
|                 data: this.data,
 | |
|                 progressCount: this.progressCount
 | |
|             });
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     reportError(message) {
 | |
|         ws.sendMessageToAllClients({
 | |
|             type: 'taskError',
 | |
|             taskId: this.taskId,
 | |
|             taskType: this.taskType,
 | |
|             data: this.data,
 | |
|             message: message
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     taskSucceeded(result) {
 | |
|         ws.sendMessageToAllClients({
 | |
|             type: 'taskSucceeded',
 | |
|             taskId: this.taskId,
 | |
|             taskType: this.taskType,
 | |
|             data: this.data,
 | |
|             result: result
 | |
|         });
 | |
|     }
 | |
| }
 | |
| 
 | |
| module.exports = TaskContext;
 |