From fcd8c212e4152eeeb0cf2e3865486beb5496b950 Mon Sep 17 00:00:00 2001 From: Weixuan Fu Date: Wed, 17 Jan 2024 10:57:43 +0800 Subject: [PATCH] make generateFilePath async --- ui/src/Api.ts | 3 ++- ui/src/App.tsx | 1 + ui/src/db-tables/DiskFileUtils.ts | 18 +++++++++++------- ui/src/db-tables/IndexDBUtils.ts | 2 ++ ui/src/db-tables/TableBase.ts | 6 ++++++ ui/src/db-tables/WorkspaceDB.ts | 1 + ui/src/utils.tsx | 4 ++-- ui/vite.config.ts | 4 ++-- 8 files changed, 27 insertions(+), 12 deletions(-) diff --git a/ui/src/Api.ts b/ui/src/Api.ts index fad0e65..cbb726f 100644 --- a/ui/src/Api.ts +++ b/ui/src/Api.ts @@ -1,6 +1,7 @@ import { Table } from "./db-tables/WorkspaceDB"; export async function getDB(table: Table): Promise { + console.warn("getDB is deprecated", table); try { const response = await fetch(`/workspace/get_db?table=${table}`); if (!response.ok) { @@ -150,7 +151,7 @@ export async function scanLocalNewFiles(path: string, existFlowIds: string[]) { }, body: JSON.stringify({ path, - existFlowIds + existFlowIds, }), }); const result = await response.json(); diff --git a/ui/src/App.tsx b/ui/src/App.tsx index 2052fea..e823f3b 100644 --- a/ui/src/App.tsx +++ b/ui/src/App.tsx @@ -92,6 +92,7 @@ export default function App() { document.title = name + " - ComfyUI"; } else { localStorage.setItem("curFlowID", id); + document.title = "ComfyUI - " + name; } }; diff --git a/ui/src/db-tables/DiskFileUtils.ts b/ui/src/db-tables/DiskFileUtils.ts index 4c5a5f7..19c96fd 100644 --- a/ui/src/db-tables/DiskFileUtils.ts +++ b/ui/src/db-tables/DiskFileUtils.ts @@ -12,11 +12,11 @@ import { import { toFileNameFriendly } from "../utils"; export async function saveJsonFileMyWorkflows(workflow: Workflow) { - const file_path = generateFilePath(workflow); + const file_path = await generateFilePath(workflow); if (file_path == null) { return; } - const fullPath = generateFilePathAbsolute(workflow); + const fullPath = await generateFilePathAbsolute(workflow); updateFlow(workflow.id, { filePath: fullPath ?? undefined, }); @@ -31,15 +31,17 @@ export async function saveJsonFileMyWorkflows(workflow: Workflow) { await updateFile(file_path, JSON.stringify(flow)); } -export function deleteJsonFileMyWorkflows(workflow: Workflow) { +export async function deleteJsonFileMyWorkflows(workflow: Workflow) { if (workflow.name == null) { return; } - const file_path = generateFilePath(workflow); + const file_path = await generateFilePath(workflow); file_path != null && deleteFile(file_path); } -export function generateFilePath(workflow: Workflow): string | null { +export async function generateFilePath( + workflow: Workflow +): Promise { let filePath = toFileNameFriendly(workflow.name) + ".json"; let curFolderID = workflow.parentFolderID; while (curFolderID != null) { @@ -55,8 +57,10 @@ export function generateFilePath(workflow: Workflow): string | null { return filePath ?? null; } -export function generateFilePathAbsolute(workflow: Workflow): string | null { - const subPath = generateFilePath(workflow); +export async function generateFilePathAbsolute( + workflow: Workflow +): Promise { + const subPath = await generateFilePath(workflow); let myWorkflowsDir = userSettingsTable?.getSetting("myWorkflowsDir"); if (myWorkflowsDir == null) { console.error("myWorkflowsDir is not set"); diff --git a/ui/src/db-tables/IndexDBUtils.ts b/ui/src/db-tables/IndexDBUtils.ts index 6858100..2cb840a 100644 --- a/ui/src/db-tables/IndexDBUtils.ts +++ b/ui/src/db-tables/IndexDBUtils.ts @@ -47,6 +47,8 @@ async function writeWorkspaceTable(data: string): Promise { } export async function updateWorkspaceIndexDB() { + // console.log("mutating deprecated indexdb backup"); + return; try { const comfyspaceData = await curComfyspaceJson(); await writeWorkspaceTable(comfyspaceData); diff --git a/ui/src/db-tables/TableBase.ts b/ui/src/db-tables/TableBase.ts index c3918c3..02a630d 100644 --- a/ui/src/db-tables/TableBase.ts +++ b/ui/src/db-tables/TableBase.ts @@ -13,14 +13,20 @@ export class TableBase { public async listAll(): Promise { const objs = await indexdb[this.tableName].toArray(); if (objs?.length) return objs as T[]; + console.warn("indexdb not found", this.tableName, "fallback to legacy db"); const records = await this.getRecords(); return Object.values(records); } public async getRecords(): Promise> { + console.error("[DEPRECATED]getRecords() call", this.tableName); + return await loadTable(this.tableName); } public async get(id: string): Promise { + const obj = await indexdb[this.tableName].get(id); + if (obj) return obj as T; + console.warn("indexdb not found", this.tableName, "fallback to legacy db"); const records = await this.getRecords(); return records[id]; } diff --git a/ui/src/db-tables/WorkspaceDB.ts b/ui/src/db-tables/WorkspaceDB.ts index 674b04f..cef4ae2 100644 --- a/ui/src/db-tables/WorkspaceDB.ts +++ b/ui/src/db-tables/WorkspaceDB.ts @@ -58,6 +58,7 @@ export let changelogsTable: ChangelogsTable | null = null; export let mediaTable: MediaTable | null = null; export const loadTable = async (name: Table) => { + const records = await indexdb[name].toCollection(); let jsonStr = await getDB(name); let json: any; try { diff --git a/ui/src/utils.tsx b/ui/src/utils.tsx index 20bafb0..ffdcff9 100644 --- a/ui/src/utils.tsx +++ b/ui/src/utils.tsx @@ -156,7 +156,7 @@ export async function validateOrSaveAllJsonFileMyWorkflows( deleteEmptyFolder = false ) { for (const workflow of listWorkflows()) { - const fullPath = generateFilePathAbsolute(workflow); + const fullPath = await generateFilePathAbsolute(workflow); if (workflow.filePath != fullPath) { // file path changed workflow.filePath != null && @@ -400,7 +400,7 @@ export function generateUrlHashWithFlowId(id: string) { export async function rewriteAllLocalFiles() { for (const workflow of listWorkflows()) { try { - const fullPath = generateFilePathAbsolute(workflow); + const fullPath = await generateFilePathAbsolute(workflow); const flow = JSON.parse(workflow.json); flow.extra[COMFYSPACE_TRACKING_FIELD_NAME] = { id: workflow.id, diff --git a/ui/vite.config.ts b/ui/vite.config.ts index b7b78f1..471fd08 100644 --- a/ui/vite.config.ts +++ b/ui/vite.config.ts @@ -12,8 +12,8 @@ export default defineConfig({ watch: { include: ["src/**"], }, - // minify: false, // ___DEBUG__MODE only - // sourcemap: true, // ___DEBUG___MODE only + minify: false, // ___DEBUG__MODE only + sourcemap: true, // ___DEBUG___MODE only emptyOutDir: true, rollupOptions: { // externalize deps that shouldn't be bundled into your library