Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
perf: use a function to save settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Aug 22, 2022
1 parent b484788 commit 3c43e45
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 17 deletions.
3 changes: 1 addition & 2 deletions Snippet-downloader/addSnippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ export async function updateRepo(repoPath: string, snippetList: SnippetRepo[], v
}
snippetList = removeSnippetFromExcluded(repoPath,snippetList, errorSnippets, excludedSnippets);
new Notice(`${repoPath} successfully updated 🎉`);
console.log(errorSnippets, snippetList);
return [errorSnippets, snippetList];
return [snippetList, errorSnippets];
}

export async function updateSpecificSnippet(item: SnippetUpdate, settings: SnippetDownloaderSettings) {
Expand Down
13 changes: 8 additions & 5 deletions Snippet-downloader/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ export default class snippetDownloader extends Plugin {
await this.saveSettings();
}
}

async updateList(newSettings: SnippetRepo[] | (string | SnippetRepo[])[]){
this.settings.snippetList = <SnippetRepo[]>newSettings[0];
this.settings.errorSnippet = <string>newSettings[1];
await this.saveSettings();
}

async onload() {
await this.loadSettings();
Expand All @@ -38,9 +44,7 @@ export default class snippetDownloader extends Plugin {
new SnippetDownloaderModals(this.app, async (result) => {
if (result) {
const newSettings = await addSnippet(result.trim(), this.settings, this.app.vault);
this.settings.snippetList = <SnippetRepo[]>newSettings[0]
this.settings.errorSnippet = <string>newSettings[1]
await this.saveSettings();
await this.updateList(newSettings);
}
}).open();
}
Expand All @@ -60,8 +64,7 @@ export default class snippetDownloader extends Plugin {
for (const repoName of snippetList) {
//@ts-ignore
updatedSettings= await updateRepo(repoName.repo, snippetList, this.app.vault, excludedSnippet, errorSnippet);
this.settings.snippetList = <SnippetRepo[]>updatedSettings[1];
this.settings.errorSnippet = <string>updatedSettings[0];
await this.updateList(updatedSettings);
}
await this.saveSettings();
}
Expand Down
6 changes: 2 additions & 4 deletions Snippet-downloader/modals/excludeSnippet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {App, FuzzySuggestModal, Notice} from "obsidian";
import {SnippetDownloaderSettings, SnippetRepo} from "../settings";
import {SnippetDownloaderSettings} from "../settings";
import snippetDownloader from "../main";
import {basename, searchExcluded} from "../utils";
import {removeSnippetFromExcluded} from "../removeSnippet";
Expand Down Expand Up @@ -52,9 +52,7 @@ export class ExcludeSnippet extends FuzzySuggestModal<SnippetExclude> {

async onChooseItem(item: SnippetExclude, evt: MouseEvent | KeyboardEvent) {
const newSettings=await addExcludedSnippet(item, this.settings);
this.settings.snippetList = <SnippetRepo[]>newSettings[0];
this.settings.excludedSnippet = <string>newSettings[1];
await this.plugin.saveSettings();
await this.plugin.updateList(newSettings);
new Notice(`${basename(item.snippetPath)} has been excluded! 🎉`)
}
}
8 changes: 2 additions & 6 deletions Snippet-downloader/modals/updateSnippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ export class RepoDownloader extends FuzzySuggestModal<RepoUpdate> {

async onChooseItem(item: RepoUpdate, evt: MouseEvent | KeyboardEvent) {
const allSettings = await updateRepo(item.repoName, this.settings.snippetList, this.app.vault, this.settings.excludedSnippet, this.settings.errorSnippet);
this.settings.snippetList =<SnippetRepo[]>allSettings[1];
this.settings.errorSnippet=<string>allSettings[0];
await this.plugin.saveSettings();
await this.plugin.updateList(allSettings);
}

}
Expand All @@ -88,8 +86,6 @@ export class SpecificSnippetDownloader extends FuzzySuggestModal<SnippetUpdate>

async onChooseItem(item: SnippetUpdate, evt: MouseEvent | KeyboardEvent) {
const newList = await updateSpecificSnippet(item, this.settings);
this.settings.snippetList = <SnippetRepo[]>newList[0];
this.settings.errorSnippet = <string>newList[1];
await this.plugin.saveSettings();
await this.plugin.updateList(newList);
}
}

0 comments on commit 3c43e45

Please sign in to comment.