Optimize settings and API key management
Improves settings loading and saving by preventing model cache merging and tracking API key changes Prevents type drift in settings by destructuring saved data Adds tracking of last API key to avoid unnecessary prompt generator updates Ensures only meaningful settings are merged with defaults Reduces redundant operations and improves plugin performance
This commit is contained in:
1 file changed
+15
-15
@@ -37,6 +37,8 @@ export default class FreewritingPromptsPlugin extends Plugin {
|
|||||||
timedCommand: TimedPromptsCommand;
|
timedCommand: TimedPromptsCommand;
|
||||||
/** Command handler for note prompt insertion */
|
/** Command handler for note prompt insertion */
|
||||||
noteCommand: NotePromptsCommand;
|
noteCommand: NotePromptsCommand;
|
||||||
|
/** Tracks last saved API key to avoid unnecessary cache clears */
|
||||||
|
private lastApiKey?: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when the plugin loads.
|
* Called when the plugin loads.
|
||||||
@@ -49,7 +51,13 @@ export default class FreewritingPromptsPlugin extends Plugin {
|
|||||||
async onload() {
|
async onload() {
|
||||||
// Load data once and use it for both settings and model cache
|
// Load data once and use it for both settings and model cache
|
||||||
const data = await this.loadData() as FreewritingPromptsData | null;
|
const data = await this.loadData() as FreewritingPromptsData | null;
|
||||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, data);
|
|
||||||
|
// Destructure to prevent modelCache from being merged into settings (type drift)
|
||||||
|
const { modelCache, ...savedSettings } = data ?? {};
|
||||||
|
this.settings = Object.assign({}, DEFAULT_SETTINGS, savedSettings);
|
||||||
|
|
||||||
|
// Track initial API key to detect changes on save
|
||||||
|
this.lastApiKey = this.settings.apiKey;
|
||||||
|
|
||||||
// Initialize services
|
// Initialize services
|
||||||
this.promptGenerator = new PromptGeneratorService(this.settings);
|
this.promptGenerator = new PromptGeneratorService(this.settings);
|
||||||
@@ -58,8 +66,8 @@ export default class FreewritingPromptsPlugin extends Plugin {
|
|||||||
this.noteCommand = new NotePromptsCommand(this.promptGenerator);
|
this.noteCommand = new NotePromptsCommand(this.promptGenerator);
|
||||||
|
|
||||||
// Load model cache
|
// Load model cache
|
||||||
if (data?.modelCache) {
|
if (modelCache) {
|
||||||
this.modelService.loadCache(data.modelCache);
|
this.modelService.loadCache(modelCache);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register commands
|
// Register commands
|
||||||
@@ -93,16 +101,6 @@ export default class FreewritingPromptsPlugin extends Plugin {
|
|||||||
|
|
||||||
// MARK: - Settings Management
|
// MARK: - Settings Management
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads plugin settings from disk.
|
|
||||||
*
|
|
||||||
* Merges saved settings with defaults to ensure any new settings added
|
|
||||||
* in plugin updates have sensible default values.
|
|
||||||
*/
|
|
||||||
async loadSettings() {
|
|
||||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves plugin settings to disk.
|
* Saves plugin settings to disk.
|
||||||
*
|
*
|
||||||
@@ -118,9 +116,11 @@ export default class FreewritingPromptsPlugin extends Plugin {
|
|||||||
};
|
};
|
||||||
await this.saveData(data);
|
await this.saveData(data);
|
||||||
|
|
||||||
// Update the prompt generator with new settings
|
// Update the prompt generator only if API key actually changed
|
||||||
if (this.promptGenerator) {
|
// This avoids clearing the prompt cache unnecessarily on every save
|
||||||
|
if (this.promptGenerator && this.lastApiKey !== this.settings.apiKey) {
|
||||||
this.promptGenerator.updateApiKey(this.settings.apiKey);
|
this.promptGenerator.updateApiKey(this.settings.apiKey);
|
||||||
|
this.lastApiKey = this.settings.apiKey;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user