#296 - State caching for content dashboard

This commit is contained in:
Elio Struyf
2022-03-23 08:41:22 +01:00
parent 70d8bfe273
commit 491b32baaa
3 changed files with 80 additions and 40 deletions
+3
View File
@@ -12,6 +12,9 @@ export const ExtensionState = {
},
Media: {
Sorting: `frontMatter:Dashboard:Media:Sorting`,
},
Pages: {
Cache: `frontMatter:Dashboard:Pages:Cache`,
}
},
+22 -11
View File
@@ -1,32 +1,43 @@
export enum DashboardMessage {
getViewType = 'getViewType',
reload = 'reload',
setPageViewType = 'setPageViewType',
// Content dashboard
getData = 'getData',
openFile = 'openFile',
getTheme = 'getTheme',
createContent = 'createContent',
createByContentType = 'createByContentType',
createByTemplate = 'createByTemplate',
updateSetting = 'updateSetting',
initializeProject = 'initializeProject',
reload = 'reload',
setPageViewType = 'setPageViewType',
refreshPages = 'refreshPages',
searchPages = 'searchPages',
// Media Dashboard
openFile = 'openFile',
getMedia = 'getMedia',
copyToClipboard = 'copyToClipboard',
refreshMedia = 'refreshMedia',
refreshPages = 'refreshPages',
uploadMedia = 'uploadMedia',
deleteMedia = 'deleteMedia',
revealMedia = 'revealMedia',
insertPreviewImage = 'insertPreviewImage',
updateMediaMetadata = 'updateMediaMetadata',
createMediaFolder = 'createMediaFolder',
setFramework = 'setFramework',
setState = 'setState',
runCustomScript = 'runCustomScript',
// Data dashboard
getDataEntries = 'getDataEntries',
putDataEntries = 'putDataEntries',
sendTelemetry = 'sendTelemetry',
// Snippets dashboard
insertSnippet = 'insertSnippet',
addSnippet = 'addSnippet',
updateSnippet = 'updateSnippet',
// Other
getTheme = 'getTheme',
updateSetting = 'updateSetting',
initializeProject = 'initializeProject',
setFramework = 'setFramework',
setState = 'setState',
runCustomScript = 'runCustomScript',
sendTelemetry = 'sendTelemetry',
}
+55 -29
View File
@@ -4,11 +4,11 @@ import { basename, dirname, join } from "path";
import { commands, FileSystemWatcher, RelativePattern, TextDocument, Uri, workspace } from "vscode";
import { Dashboard } from "../../commands/Dashboard";
import { Folders } from "../../commands/Folders";
import { COMMAND_NAME, DefaultFields, SETTING_CONTENT_STATIC_FOLDER, SETTING_SEO_DESCRIPTION_FIELD } from "../../constants";
import { COMMAND_NAME, DefaultFields, ExtensionState, SETTING_CONTENT_STATIC_FOLDER, SETTING_SEO_DESCRIPTION_FIELD } from "../../constants";
import { DashboardCommand } from "../../dashboardWebView/DashboardCommand";
import { DashboardMessage } from "../../dashboardWebView/DashboardMessage";
import { Page } from "../../dashboardWebView/models";
import { ArticleHelper, Logger, Settings } from "../../helpers";
import { ArticleHelper, Extension, Logger, Settings } from "../../helpers";
import { ContentType } from "../../helpers/ContentType";
import { DateHelper } from "../../helpers/DateHelper";
import { Notifications } from "../../helpers/Notifications";
@@ -21,6 +21,36 @@ export class PagesListener extends BaseListener {
private static watchers: { [path: string]: FileSystemWatcher } = {};
private static lastPages: Page[] = [];
/**
* Process the messages for the dashboard views
* @param msg
*/
public static async process(msg: { command: DashboardMessage, data: any }) {
super.process(msg);
switch(msg.command) {
case DashboardMessage.getData:
this.getPagesData();
break;
case DashboardMessage.createContent:
await commands.executeCommand(COMMAND_NAME.createContent);
break;
case DashboardMessage.createByContentType:
await commands.executeCommand(COMMAND_NAME.createByContentType);
break;
case DashboardMessage.createByTemplate:
await commands.executeCommand(COMMAND_NAME.createByTemplate);
break;
case DashboardMessage.refreshPages:
this.getPagesData(true);
break;
}
}
/**
* Saved file watcher
* @returns
*/
public static saveFileWatcher() {
return workspace.onDidSaveTextDocument((doc: TextDocument) => {
if (ArticleHelper.isSupportedFile(doc)) {
@@ -63,33 +93,12 @@ export class PagesListener extends BaseListener {
}
/**
* Process the messages for the dashboard views
* @param msg
* Watcher for processing page updates
* @param file
*/
public static async process(msg: { command: DashboardMessage, data: any }) {
super.process(msg);
switch(msg.command) {
case DashboardMessage.getData:
this.getPagesData();
break;
case DashboardMessage.createContent:
await commands.executeCommand(COMMAND_NAME.createContent);
break;
case DashboardMessage.createByContentType:
await commands.executeCommand(COMMAND_NAME.createByContentType);
break;
case DashboardMessage.createByTemplate:
await commands.executeCommand(COMMAND_NAME.createByTemplate);
break;
case DashboardMessage.refreshPages:
this.getPagesData();
break;
}
}
private static async watcherExec(file: Uri) {
if (Dashboard.isOpen) {
const ext = Extension.getInstance();
Logger.info(`File watcher execution for: ${file.fsPath}`)
const pageIdx = this.lastPages.findIndex(p => p.fmFilePath === file.fsPath);
@@ -100,9 +109,10 @@ export class PagesListener extends BaseListener {
if (updatedPage) {
this.lastPages[pageIdx] = updatedPage;
this.sendMsg(DashboardCommand.pages, this.lastPages);
await ext.setState(ExtensionState.Dashboard.Pages.Cache, this.lastPages, "workspace");
}
} else {
this.getPagesData();
this.getPagesData(true);
}
}
}
@@ -110,7 +120,18 @@ export class PagesListener extends BaseListener {
/**
* Retrieve all the markdown pages
*/
private static async getPagesData() {
private static async getPagesData(clear: boolean = false) {
const ext = Extension.getInstance();
// Get data from the cache
if (!clear) {
const cachedPages = await ext.getState<Page[]>(ExtensionState.Dashboard.Pages.Cache, "workspace");
if (cachedPages) {
this.sendMsg(DashboardCommand.pages, cachedPages);
}
}
// Update the dashboard with the fresh data
const folderInfo = await Folders.getInfo();
const pages: Page[] = [];
@@ -136,10 +157,15 @@ export class PagesListener extends BaseListener {
this.lastPages = pages;
this.sendMsg(DashboardCommand.pages, pages);
await ext.setState(ExtensionState.Dashboard.Pages.Cache, pages, "workspace");
}
/**
* Get fresh page data
*/
public static refresh() {
this.getPagesData();
this.getPagesData(true);
}
/**