Skip to content

Commit a5300a5

Browse files
committed
fix saving cache data
1 parent 7e9d1b8 commit a5300a5

File tree

3 files changed

+11
-16
lines changed

3 files changed

+11
-16
lines changed

embeds/generic-preview.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ export class GenericPreviewEmbed implements EmbedSource {
2121
const preview = document.createElement("a");
2222
preview.setAttr("href", link);
2323
preview.classList.add("preview");
24-
preview.textContent = "Loading embed...";
24+
preview.textContent = "Loading preview...";
2525

2626
const loadPreview = async () => {
2727
let metadata;
2828

29-
if (settings.useCacheForGenericLinks && link in plugin.linkPreviewCache) {
30-
metadata = plugin.linkPreviewCache[link];
29+
if (settings.useCacheForGenericLinks && link in settings.genericPreviewCache) {
30+
metadata = settings.genericPreviewCache[link];
3131
} else {
3232
const res = await requestUrl({ url: link });
3333
metadata = await getPreviewFromContent({
@@ -37,7 +37,7 @@ export class GenericPreviewEmbed implements EmbedSource {
3737
});
3838

3939
if (settings.useCacheForGenericLinks && "title" in metadata) {
40-
plugin.linkPreviewCache[link] = metadata;
40+
plugin.saveGenericPreviewCache(link, metadata);
4141
}
4242
}
4343

main.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,12 @@ export default class SimpleEmbedsPlugin extends Plugin {
3939
];
4040
processedMarkdown: Debouncer<[]>;
4141
currentTheme: "dark" | "light";
42-
4342
genericPreviewEmbed = new GenericPreviewEmbed();
44-
// use a separate property to save to cache during session
45-
// saveSettings triggers reload and results in a loop
46-
linkPreviewCache: { [url: string]: GenericPreviewMetadata };
4743

4844
async onload() {
4945
console.log(`Loading ${this.manifest.name} v${this.manifest.version}`);
5046
await this.loadSettings();
51-
this.linkPreviewCache = this.settings.genericPreviewCache;
47+
console.log(this.settings);
5248
this.addSettingTab(new SimpleEmbedPluginSettingTab(this.app, this));
5349

5450
this.currentTheme = this._getCurrentTheme();
@@ -89,12 +85,6 @@ export default class SimpleEmbedsPlugin extends Plugin {
8985
}
9086

9187
onunload() {
92-
// save the cache before unloading
93-
console.log("Saving link preview cache to settings");
94-
this.saveSettings({
95-
genericPreviewCache: this.linkPreviewCache,
96-
});
97-
9888
console.log(`Unloading ${this.manifest.name}`);
9989
this.processedMarkdown = null;
10090
}
@@ -113,6 +103,11 @@ export default class SimpleEmbedsPlugin extends Plugin {
113103
});
114104
}
115105

106+
async saveGenericPreviewCache(link: string, metadata: GenericPreviewMetadata) {
107+
this.settings.genericPreviewCache[link] = metadata;
108+
await this.saveData(this.settings);
109+
}
110+
116111
private _getCurrentTheme(): "dark" | "light" {
117112
return document.body.classList.contains("theme-dark") ? "dark" : "light";
118113
}

settings-tab.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class SimpleEmbedPluginSettingTab extends PluginSettingTab {
6666
});
6767
new Setting(containerEl)
6868
.setName("Clear link preview metadata cache")
69-
.addButton(async (button) => {
69+
.addButton((button) => {
7070
button
7171
.setButtonText("Clear")
7272
.onClick(async () => {

0 commit comments

Comments
 (0)