Skip to content

Commit

Permalink
#863 fix empty page folder cache
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Oct 7, 2024
1 parent 38f128e commit 63ea564
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/commands/Article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 19 additions & 6 deletions src/commands/Folders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -50,7 +50,7 @@ export class Folders {

public static clearCached() {
Logger.verbose(`Folders:clearCached`);
Folders._folders = [];
Folders._folders = undefined;
}

/**
Expand Down Expand Up @@ -327,7 +327,7 @@ export class Folders {
public static async get(): Promise<ContentFolder[]> {
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;
}
Expand Down Expand Up @@ -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<ContentFolder[]>} A promise that resolves to an array of content folders.
*/
public static async getCachedOrFresh(): Promise<ContentFolder[]> {
if (Folders._folders && Folders._folders.length > 0) {
return Folders._folders;
}

return await Folders.get();
}

/**
* Update the folder settings
* @param folders
Expand Down Expand Up @@ -688,7 +701,7 @@ export class Folders {
public static async getPageFolderByFilePath(
filePath: string
): Promise<ContentFolder | undefined> {
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))
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/localization/localization.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
/**
Expand Down

0 comments on commit 63ea564

Please sign in to comment.