2019-10-17 21:11:35 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								"use strict";
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-01-09 18:36:24 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import type { TaskData } from "./task_context_interface.js";
							 | 
						
					
						
							
								
									
										
										
										
											2024-07-18 21:35:17 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import ws from "./ws.js";
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-17 21:11:35 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// taskId => TaskContext
							 | 
						
					
						
							
								
									
										
										
										
											2024-02-17 02:02:08 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								const taskContexts: Record<string, TaskContext> = {};
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-17 21:11:35 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								class TaskContext {
							 | 
						
					
						
							
								
									
										
										
										
											2024-02-17 02:02:08 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    private taskId: string;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    private taskType: string | null;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    private progressCount: number;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    private lastSentCountTs: number;
							 | 
						
					
						
							
								
									
										
										
										
											2024-02-25 14:52:20 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    data: TaskData | null;
							 | 
						
					
						
							
								
									
										
										
										
											2024-02-17 10:56:27 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    noteDeletionHandlerTriggered: boolean;
							 | 
						
					
						
							
								
									
										
										
										
											2024-12-22 15:42:15 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-02-17 02:02:08 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    constructor(taskId: string, taskType: string | null = null, data: {} | null = {}) {
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-17 21:11:35 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        this.taskId = taskId;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        this.taskType = taskType;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        this.data = data;
							 | 
						
					
						
							
								
									
										
										
										
											2022-06-05 14:58:19 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        this.noteDeletionHandlerTriggered = false;
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-17 21:11:35 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        // progressCount is meant to represent just some progress - to indicate the task is not stuck
							 | 
						
					
						
							
								
									
										
										
										
											2023-06-23 00:26:47 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        this.progressCount = -1; // we're incrementing immediately
							 | 
						
					
						
							
								
									
										
										
										
											2023-06-30 11:18:34 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        this.lastSentCountTs = 0; // 0 will guarantee the first message will be sent
							 | 
						
					
						
							
								
									
										
										
										
											2020-01-04 21:24:39 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        // just the fact this has been initialized is a progress which should be sent to clients
							 | 
						
					
						
							
								
									
										
										
										
											2023-06-30 11:18:34 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        // this is esp. important when importing big files/images which take a long time to upload/process
							 | 
						
					
						
							
								
									
										
										
										
											2020-01-04 21:24:39 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        // which means that first "real" increaseProgressCount() will be called quite late and user is without
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        // feedback until then
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        this.increaseProgressCount();
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-17 21:11:35 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-02-17 02:02:08 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    static getInstance(taskId: string, taskType: string, data: {} | null = null): TaskContext {
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-17 21:11:35 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if (!taskContexts[taskId]) {
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-18 23:19:16 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            taskContexts[taskId] = new TaskContext(taskId, taskType, data);
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-17 21:11:35 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return taskContexts[taskId];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-28 18:42:22 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    increaseProgressCount() {
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-17 21:11:35 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        this.progressCount++;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if (Date.now() - this.lastSentCountTs >= 300 && this.taskId !== "no-progress-reporting") {
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-17 21:11:35 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            this.lastSentCountTs = Date.now();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-28 18:42:22 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            ws.sendMessageToAllClients({
							 | 
						
					
						
							
								
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                type: "taskProgressCount",
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-17 21:11:35 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                taskId: this.taskId,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                taskType: this.taskType,
							 | 
						
					
						
							
								
									
										
										
										
											2024-03-17 21:37:40 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                data: this.data,
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-17 21:11:35 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                progressCount: this.progressCount
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            });
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-02-17 02:02:08 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    reportError(message: string) {
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-28 18:42:22 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        ws.sendMessageToAllClients({
							 | 
						
					
						
							
								
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            type: "taskError",
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-17 21:11:35 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            taskId: this.taskId,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            taskType: this.taskType,
							 | 
						
					
						
							
								
									
										
										
										
											2024-03-17 21:37:40 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            data: this.data,
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-17 21:11:35 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            message: message
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        });
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-06 21:30:27 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    taskSucceeded(result?: string | Record<string, string | undefined>) {
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-28 18:42:22 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        ws.sendMessageToAllClients({
							 | 
						
					
						
							
								
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            type: "taskSucceeded",
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-17 21:11:35 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            taskId: this.taskId,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            taskType: this.taskType,
							 | 
						
					
						
							
								
									
										
										
										
											2024-03-17 21:37:40 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            data: this.data,
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-19 00:11:07 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            result: result
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-17 21:11:35 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        });
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-07-18 21:50:12 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								export default TaskContext;
							 |