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
refactor: further improve task context types
Elian Doran39fecb3ffed6
diff --git a/apps/client/src/services/import.ts b/apps/client/src/services/import.tsindex 6121ab422d..2300ca101f 100644--- a/apps/client/src/services/import.ts+++ b/apps/client/src/services/import.ts@@ -82,7 +82,7 @@ ws.subscribeToMessages(async (message) => {toastService.showPersistent(toast);- if (typeof message.result === "object" && message.result.importedNoteId) {+ if (message.result.importedNoteId) {await appContext.tabManager.getActiveContext()?.setNote(message.result.importedNoteId);}}@@ -104,7 +104,7 @@ ws.subscribeToMessages(async (message: WebSocketMessage) => {toastService.showPersistent(toast);- if (typeof message.result === "object" && message.result.parentNoteId) {+ if (message.result.parentNoteId) {await appContext.tabManager.getActiveContext()?.setNote(message.result.importedNoteId, {viewScope: {viewMode: "attachments"diff --git a/apps/server/src/services/task_context.ts b/apps/server/src/services/task_context.tsindex 7cef303f83..60ece320b1 100644--- a/apps/server/src/services/task_context.ts+++ b/apps/server/src/services/task_context.ts@@ -1,20 +1,20 @@"use strict";-import type { TaskData } from "@triliumnext/commons";+import type { TaskType } from "@triliumnext/commons";import ws from "./ws.js";// taskId => TaskContext-const taskContexts: Record<string, TaskContext> = {};+const taskContexts: Record<string, TaskContext<TaskType>> = {};-class TaskContext {+class TaskContext<TaskTypeT extends TaskType> {private taskId: string;- private taskType: string | null;+ private taskType: TaskType;private progressCount: number;private lastSentCountTs: number;data: TaskData | null;noteDeletionHandlerTriggered: boolean;- constructor(taskId: string, taskType: string | null = null, data: {} | null = {}) {+ constructor(taskId: string, taskType: TaskTypeT, data: {} | null = {}) {this.taskId = taskId;this.taskType = taskType;this.data = data;@@ -31,7 +31,7 @@ class TaskContext {this.increaseProgressCount();}- static getInstance(taskId: string, taskType: string, data: {} | null = null): TaskContext {+ static getInstance<TaskTypeT extends TaskType>(taskId: string, taskType: TaskTypeT, data: {} | null = null): TaskContext<TaskTypeT> {if (!taskContexts[taskId]) {taskContexts[taskId] = new TaskContext(taskId, taskType, data);}diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.tsindex 2454fbf1bb..114ff4eea0 100644--- a/packages/commons/src/lib/server_api.ts+++ b/packages/commons/src/lib/server_api.ts@@ -271,7 +271,7 @@ export interface EntityChangeRecord {entity?: EntityRow;}-type TaskStatus<TypeT, DataT> = {+type TaskStatus<TypeT, DataT, ResultT> = {type: "taskProgressCount",taskId: string;taskType: TypeT;@@ -288,18 +288,20 @@ type TaskStatus<TypeT, DataT> = {taskId: string;taskType: TypeT;data: DataT;- result?: string | Record<string, string | undefined>+ result: ResultT;}type TaskDefinitions =- TaskStatus<"protectNotes", { protect: boolean; }>- | TaskStatus<"importNotes", null>- | TaskStatus<"importAttachments", null>- | TaskStatus<"deleteNotes", null>- | TaskStatus<"undeleteNotes", null>- | TaskStatus<"export", null>+ TaskStatus<"protectNotes", { protect: boolean; }, null>+ | TaskStatus<"importNotes", null, { importedNoteId: string }>+ | TaskStatus<"importAttachments", null, { parentNoteId?: string; importedNoteId: string }>+ | TaskStatus<"deleteNotes", null, null>+ | TaskStatus<"undeleteNotes", null, null>+ | TaskStatus<"export", null, null>;+export type TaskType = TaskDefinitions["taskType"];+export interface OpenedFileUpdateStatus {entityType: string;entityId: string;