Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
chore(core): integrate task_context with ws no-op
Elian Doranc94c54c64175
diff --git a/apps/server/src/services/task_context.ts b/apps/server/src/services/task_context.tsindex 79122895bd..59bd04f058 100644--- a/apps/server/src/services/task_context.ts+++ b/apps/server/src/services/task_context.ts@@ -1,79 +1,2 @@-"use strict";--import type { TaskData, TaskResult, TaskType, WebSocketMessage } from "@triliumnext/commons";-import ws from "./ws.js";--// taskId => TaskContext-const taskContexts: Record<string, TaskContext<any>> = {};--class TaskContext<T extends TaskType> {- private taskId: string;- private taskType: TaskType;- private progressCount: number;- private lastSentCountTs: number;- data: TaskData<T>;- noteDeletionHandlerTriggered: boolean;-- constructor(taskId: string, taskType: T, data: TaskData<T>) {- this.taskId = taskId;- this.taskType = taskType;- this.data = data;- this.noteDeletionHandlerTriggered = false;-- // progressCount is meant to represent just some progress - to indicate the task is not stuck- this.progressCount = -1; // we're incrementing immediately- this.lastSentCountTs = 0; // 0 will guarantee the 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 a 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();- }-- static getInstance<T extends TaskType>(taskId: string, taskType: T, data: TaskData<T>): TaskContext<T> {- if (!taskContexts[taskId]) {- taskContexts[taskId] = new TaskContext(taskId, taskType, data);- }-- return taskContexts[taskId];- }-- increaseProgressCount() {- this.progressCount++;-- if (Date.now() - this.lastSentCountTs >= 300 && this.taskId !== "no-progress-reporting") {- this.lastSentCountTs = Date.now();-- ws.sendMessageToAllClients({- type: "taskProgressCount",- taskId: this.taskId,- taskType: this.taskType,- data: this.data,- progressCount: this.progressCount- } as WebSocketMessage);- }- }-- reportError(message: string) {- ws.sendMessageToAllClients({- type: "taskError",- taskId: this.taskId,- taskType: this.taskType,- data: this.data,- message- } as WebSocketMessage);- }-- taskSucceeded(result: TaskResult<T>) {- ws.sendMessageToAllClients({- type: "taskSucceeded",- taskId: this.taskId,- taskType: this.taskType,- data: this.data,- result- } as WebSocketMessage);- }-}-+import { TaskContext } from "@triliumnext/core";export default TaskContext;diff --git a/packages/trilium-core/src/index.ts b/packages/trilium-core/src/index.tsindex 635f44c0bd..21aff83f3f 100644--- a/packages/trilium-core/src/index.ts+++ b/packages/trilium-core/src/index.ts@@ -29,6 +29,7 @@ export { default as note_types } from "./services/note_types";export { default as tree } from "./services/tree";export { default as cloning } from "./services/cloning";export { default as handlers } from "./services/handlers";+export { default as TaskContext } from "./services/task_context";export { default as becca } from "./becca/becca";export { default as becca_loader } from "./becca/becca_loader";diff --git a/packages/trilium-core/src/services/task_context.ts b/packages/trilium-core/src/services/task_context.tsnew file mode 100644index 0000000000..79122895bd--- /dev/null+++ b/packages/trilium-core/src/services/task_context.ts@@ -0,0 +1,79 @@+"use strict";++import type { TaskData, TaskResult, TaskType, WebSocketMessage } from "@triliumnext/commons";+import ws from "./ws.js";++// taskId => TaskContext+const taskContexts: Record<string, TaskContext<any>> = {};++class TaskContext<T extends TaskType> {+ private taskId: string;+ private taskType: TaskType;+ private progressCount: number;+ private lastSentCountTs: number;+ data: TaskData<T>;+ noteDeletionHandlerTriggered: boolean;++ constructor(taskId: string, taskType: T, data: TaskData<T>) {+ this.taskId = taskId;+ this.taskType = taskType;+ this.data = data;+ this.noteDeletionHandlerTriggered = false;++ // progressCount is meant to represent just some progress - to indicate the task is not stuck+ this.progressCount = -1; // we're incrementing immediately+ this.lastSentCountTs = 0; // 0 will guarantee the 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 a 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();+ }++ static getInstance<T extends TaskType>(taskId: string, taskType: T, data: TaskData<T>): TaskContext<T> {+ if (!taskContexts[taskId]) {+ taskContexts[taskId] = new TaskContext(taskId, taskType, data);+ }++ return taskContexts[taskId];+ }++ increaseProgressCount() {+ this.progressCount++;++ if (Date.now() - this.lastSentCountTs >= 300 && this.taskId !== "no-progress-reporting") {+ this.lastSentCountTs = Date.now();++ ws.sendMessageToAllClients({+ type: "taskProgressCount",+ taskId: this.taskId,+ taskType: this.taskType,+ data: this.data,+ progressCount: this.progressCount+ } as WebSocketMessage);+ }+ }++ reportError(message: string) {+ ws.sendMessageToAllClients({+ type: "taskError",+ taskId: this.taskId,+ taskType: this.taskType,+ data: this.data,+ message+ } as WebSocketMessage);+ }++ taskSucceeded(result: TaskResult<T>) {+ ws.sendMessageToAllClients({+ type: "taskSucceeded",+ taskId: this.taskId,+ taskType: this.taskType,+ data: this.data,+ result+ } as WebSocketMessage);+ }+}++export default TaskContext;diff --git a/packages/trilium-core/src/services/ws.ts b/packages/trilium-core/src/services/ws.tsnew file mode 100644index 0000000000..95a41ee201--- /dev/null+++ b/packages/trilium-core/src/services/ws.ts@@ -0,0 +1,5 @@+export default {+ sendMessageToAllClients(message: object) {+ console.warn("Ignored ws", message);+ }+}