From 63ea5647342b5a01595c0d05fc46d26a95036449 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Mon, 7 Oct 2024 21:14:58 +0200 Subject: [PATCH] #863 fix empty page folder cache --- src/commands/Article.ts | 2 +- src/commands/Folders.ts | 25 +++++++++++++++++++------ src/localization/localization.enum.ts | 2 +- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/commands/Article.ts b/src/commands/Article.ts index 24ed1d2d..b0e35076 100644 --- a/src/commands/Article.ts +++ b/src/commands/Article.ts @@ -375,7 +375,7 @@ export class Article { const autoUpdate = Settings.get(SETTING_AUTO_UPDATE_DATE); // Is article located in one of the content folders - const folders = Folders.getCached(); + const folders = await Folders.getCachedOrFresh(); const documentPath = parseWinPath(document.fileName); const folder = folders.find((f) => documentPath.startsWith(f.path)); if (!folder) { diff --git a/src/commands/Folders.ts b/src/commands/Folders.ts index ed46d597..04fa6712 100644 --- a/src/commands/Folders.ts +++ b/src/commands/Folders.ts @@ -39,7 +39,7 @@ import { Preview } from './Preview'; export const WORKSPACE_PLACEHOLDER = `[[workspace]]`; export class Folders { - private static _folders: ContentFolder[] = []; + private static _folders: ContentFolder[] | undefined = undefined; public static async registerCommands() { const ext = Extension.getInstance(); @@ -50,7 +50,7 @@ export class Folders { public static clearCached() { Logger.verbose(`Folders:clearCached`); - Folders._folders = []; + Folders._folders = undefined; } /** @@ -327,7 +327,7 @@ export class Folders { public static async get(): Promise { Logger.verbose('Folders:get:start'); - if (Folders._folders.length > 0) { + if (Folders._folders && Folders._folders.length > 0) { Logger.verbose('Folders:get:end - cached folders'); return Folders._folders; } @@ -452,10 +452,23 @@ export class Folders { * Get the cached folder settings * @returns {ContentFolder[]} - The cached folder settings */ - public static getCached(): ContentFolder[] { + public static getCached(): ContentFolder[] | undefined { return Folders._folders; } + /** + * Retrieves the cached content folders if available, otherwise fetches fresh content folders. + * + * @returns {Promise} A promise that resolves to an array of content folders. + */ + public static async getCachedOrFresh(): Promise { + if (Folders._folders && Folders._folders.length > 0) { + return Folders._folders; + } + + return await Folders.get(); + } + /** * Update the folder settings * @param folders @@ -688,7 +701,7 @@ export class Folders { public static async getPageFolderByFilePath( filePath: string ): Promise { - const folders = Folders.getCached(); + const folders = await Folders.getCachedOrFresh(); const parsedPath = parseWinPath(filePath); const pageFolderMatches = folders .filter((folder) => parsedPath && folder.path && parsedPath.includes(folder.path)) @@ -719,7 +732,7 @@ export class Folders { return; } - const folders = Folders.getCached(); + const folders = await Folders.getCachedOrFresh(); let selectedFolder: ContentFolder | undefined; // Try to find the folder by content type diff --git a/src/localization/localization.enum.ts b/src/localization/localization.enum.ts index 8387f727..2af534f4 100644 --- a/src/localization/localization.enum.ts +++ b/src/localization/localization.enum.ts @@ -492,7 +492,7 @@ export enum LocalizationKey { */ dashboardDataViewDataViewCloseSelectedDataFile = 'dashboard.dataView.dataView.closeSelectedDataFile', /** - * Select your date type first + * Select your data type first */ dashboardDataViewEmptyViewHeading = 'dashboard.dataView.emptyView.heading', /**