diff --git a/src/main.ts b/src/main.ts index 81635b8..2fa528e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,6 +6,7 @@ import { provideGlobalGridOptions, } from "ag-grid-community"; import { mainModule } from "./modules/main/module"; +import { clearNoRefreshCache } from "./modules/editor/codeblockHandler/CodeblockProcessor"; // Register all community features ModuleRegistry.registerModules([AllCommunityModule]); @@ -27,4 +28,8 @@ export default class SqlSealPlugin extends Plugin { const init = await this.container.get("init"); init(); } + + onunload() { + clearNoRefreshCache(); + } } diff --git a/src/modules/editor/codeblockHandler/CodeblockProcessor.ts b/src/modules/editor/codeblockHandler/CodeblockProcessor.ts index 7d34d22..aa15d87 100644 --- a/src/modules/editor/codeblockHandler/CodeblockProcessor.ts +++ b/src/modules/editor/codeblockHandler/CodeblockProcessor.ts @@ -14,6 +14,14 @@ import { registerObservers } from "../../../utils/registerObservers"; import { Settings } from "../../settings/Settings"; import { ModernCellParser } from "../../syntaxHighlight/cellParser/ModernCellParser"; +// Persist NO REFRESH results across processor recreations (e.g., reading mode re-renders). +// Key: sourcePath + query. Cleared when the plugin unloads. +const noRefreshCache = new Map() + +export function clearNoRefreshCache() { + noRefreshCache.clear() +} + export class CodeblockProcessor extends MarkdownRenderChild { registrator: OmnibusRegistrator; renderer: RenderReturn; @@ -154,6 +162,21 @@ export class CodeblockProcessor extends MarkdownRenderChild { ...this.ctx.frontmatter, }; + // NO REFRESH: serve from cache when Obsidian recreates the processor + // (e.g., reading-mode preview re-renders on file save). + if (!this.flags.refresh) { + const cacheKey = `${this.sourceKey}:::${this.query}` + const cached = noRefreshCache.get(cacheKey) + if (cached) { + this.renderer.render({ + data: cached.data, + columns: cached.columns, + flags: this.flags, + frontmatter: variables, + }); + return + } + } if (this.flags.explain) { // Rendering explain @@ -161,11 +184,17 @@ export class CodeblockProcessor extends MarkdownRenderChild { this.explainEl.textContent = result; } - + const { data, columns } = (await this.db.select( transformedQuery, variables, ))!; // FIXME: check this + + // Cache result for NO REFRESH blocks so processor recreations don't re-query. + if (!this.flags.refresh) { + noRefreshCache.set(`${this.sourceKey}:::${this.query}`, { data, columns }) + } + this.renderer.render({ data, columns,