Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -27,4 +28,8 @@ export default class SqlSealPlugin extends Plugin {
const init = await this.container.get("init");
init();
}

onunload() {
clearNoRefreshCache();
}
}
31 changes: 30 additions & 1 deletion src/modules/editor/codeblockHandler/CodeblockProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, { data: unknown[], columns: string[] }>()

export function clearNoRefreshCache() {
noRefreshCache.clear()
}

export class CodeblockProcessor extends MarkdownRenderChild {
registrator: OmnibusRegistrator;
renderer: RenderReturn;
Expand Down Expand Up @@ -154,18 +162,39 @@ 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
const result = await this.db.explain(transformedQuery, variables);
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,
Expand Down
Loading