I want to access the latest values of the setting values of my plugin. That is, the setting values after a user make changes in the settings tab. For this I found the following 3 method.
Method 1 : Using Plugin Class
main.ts :
export default class ThePlugin extends Plugin {
onload() {
await this.loadSettings();
// Rest of my code...
}
async loadSettings() {
this.settings = Object.assign({}, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
Any file, which is not linked from main.ts :
import type <youPluginName> from '../main';
export function <name> (
plugin: <youPluginName>,
): Promise<void> {
console.log(plugin.settings.<anyOptionYouWantToAccess>);
}
Problem : The issue with this method I am facing is, whenever user makes any changes in the setting from the settings Tab, the data gets reflected in the data.json file. But after this update in the setting if the plugin tries to access the values of the setting, the old data which was loaded at the time of Obsidian startup is given back using the plugin.settings. So, after any changes in the settings, the user will have to restart Obsidian every time for the setting changes to take effect.
Method 2 : By reading the data.json file every time
I can create a function, which i can able to call from any file throughout my project and get the latest values of the setting values of my plugin.
Problem : I make use of the plugin setting values at a lot of places in my code and for many functionalities. This will be too many Disk read/write operations. So this will be the most horrible method. Because the only thing i want to avoid is Disk read/write operations.
Method 3 : By using localStorage
I can load the data from the data.json when the plugin loads and store it in the localStorage. Then i can simply create a function, which will return me any value of my plugin settings. Also, when the user will make any changes in the setting, i will store that in the localStorage as well as in the data.json. But while reading, now i dont have to read the data.json, i can simply read from localStorage.
Problem : I have a question here. I don’t know much about how localStorage works and specially in electron. Some sources say it’s stored on Disk using a database provided by electron. If, it’s a Database, then also this will be too many Disk read/write operation. Although this will be a much better approach then Method 2.
Obviously, ill improve my codebase to pass values parent entity to child entity, as much as possible. But still, i am doing few operation related to events, like detecting file modification in Obsidian. So, when the user will going to make contineous changes in a file, it will be detected as modified and then i am doing few checks, for which i need plugin’s setting values.
Question 1
Am i doing anything wrong in the above method, or obsidian only provides the values which are loaded at the obsidian startup?
Question 2
2.1 : When we do the setItem in the localStorage, where does it actually gets stored is it in RAM or its on Disk inside a Database?
2.2 : I also came across sessionStorage, will it be recommended to use ?