fix: improve promise handling in DOM event listeners
Wrap async callbacks in void operators for DOM addEventListener calls to properly handle Promise<void> returns in void contexts. This follows TypeScript best practices and Obsidian plugin guidelines. Changes: - settings.ts: Fix 5 button click handlers (server controls, API key actions, config copy) - notification-history.ts: Fix export button click handler All async operations are properly awaited within void-wrapped IIFEs, ensuring errors are not silently swallowed while maintaining the expected void return type for event listeners.
This commit is contained in:
@@ -146,22 +146,28 @@ export class MCPServerSettingTab extends PluginSettingTab {
|
|||||||
|
|
||||||
if (isRunning) {
|
if (isRunning) {
|
||||||
buttonContainer.createEl('button', {text: 'Stop Server'})
|
buttonContainer.createEl('button', {text: 'Stop Server'})
|
||||||
.addEventListener('click', async () => {
|
.addEventListener('click', () => {
|
||||||
await this.plugin.stopServer();
|
void (async () => {
|
||||||
this.display();
|
await this.plugin.stopServer();
|
||||||
|
this.display();
|
||||||
|
})();
|
||||||
});
|
});
|
||||||
|
|
||||||
buttonContainer.createEl('button', {text: 'Restart Server'})
|
buttonContainer.createEl('button', {text: 'Restart Server'})
|
||||||
.addEventListener('click', async () => {
|
.addEventListener('click', () => {
|
||||||
await this.plugin.stopServer();
|
void (async () => {
|
||||||
await this.plugin.startServer();
|
await this.plugin.stopServer();
|
||||||
this.display();
|
await this.plugin.startServer();
|
||||||
|
this.display();
|
||||||
|
})();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
buttonContainer.createEl('button', {text: 'Start Server'})
|
buttonContainer.createEl('button', {text: 'Start Server'})
|
||||||
.addEventListener('click', async () => {
|
.addEventListener('click', () => {
|
||||||
await this.plugin.startServer();
|
void (async () => {
|
||||||
this.display();
|
await this.plugin.startServer();
|
||||||
|
this.display();
|
||||||
|
})();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,21 +221,25 @@ export class MCPServerSettingTab extends PluginSettingTab {
|
|||||||
|
|
||||||
// Copy button
|
// Copy button
|
||||||
const copyButton = apiKeyButtonContainer.createEl('button', {text: '📋 Copy Key'});
|
const copyButton = apiKeyButtonContainer.createEl('button', {text: '📋 Copy Key'});
|
||||||
copyButton.addEventListener('click', async () => {
|
copyButton.addEventListener('click', () => {
|
||||||
await navigator.clipboard.writeText(this.plugin.settings.apiKey || '');
|
void (async () => {
|
||||||
new Notice('✅ API key copied to clipboard');
|
await navigator.clipboard.writeText(this.plugin.settings.apiKey || '');
|
||||||
|
new Notice('✅ API key copied to clipboard');
|
||||||
|
})();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Regenerate button
|
// Regenerate button
|
||||||
const regenButton = apiKeyButtonContainer.createEl('button', {text: '🔄 Regenerate Key'});
|
const regenButton = apiKeyButtonContainer.createEl('button', {text: '🔄 Regenerate Key'});
|
||||||
regenButton.addEventListener('click', async () => {
|
regenButton.addEventListener('click', () => {
|
||||||
this.plugin.settings.apiKey = generateApiKey();
|
void (async () => {
|
||||||
await this.plugin.saveSettings();
|
this.plugin.settings.apiKey = generateApiKey();
|
||||||
new Notice('✅ New API key generated');
|
await this.plugin.saveSettings();
|
||||||
if (this.plugin.mcpServer?.isRunning()) {
|
new Notice('✅ New API key generated');
|
||||||
new Notice('⚠️ Server restart required for API key changes to take effect');
|
if (this.plugin.mcpServer?.isRunning()) {
|
||||||
}
|
new Notice('⚠️ Server restart required for API key changes to take effect');
|
||||||
this.display();
|
}
|
||||||
|
this.display();
|
||||||
|
})();
|
||||||
});
|
});
|
||||||
|
|
||||||
// API Key display (static, copyable text)
|
// API Key display (static, copyable text)
|
||||||
@@ -284,9 +294,11 @@ export class MCPServerSettingTab extends PluginSettingTab {
|
|||||||
text: '📋 Copy Configuration',
|
text: '📋 Copy Configuration',
|
||||||
cls: 'mcp-config-button'
|
cls: 'mcp-config-button'
|
||||||
});
|
});
|
||||||
copyConfigButton.addEventListener('click', async () => {
|
copyConfigButton.addEventListener('click', () => {
|
||||||
await navigator.clipboard.writeText(JSON.stringify(config, null, 2));
|
void (async () => {
|
||||||
new Notice('✅ Configuration copied to clipboard');
|
await navigator.clipboard.writeText(JSON.stringify(config, null, 2));
|
||||||
|
new Notice('✅ Configuration copied to clipboard');
|
||||||
|
})();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Config JSON display
|
// Config JSON display
|
||||||
@@ -413,9 +425,11 @@ export class MCPServerSettingTab extends PluginSettingTab {
|
|||||||
text: '📋 Copy Configuration',
|
text: '📋 Copy Configuration',
|
||||||
cls: 'mcp-config-button'
|
cls: 'mcp-config-button'
|
||||||
});
|
});
|
||||||
copyConfigButton.addEventListener('click', async () => {
|
copyConfigButton.addEventListener('click', () => {
|
||||||
await navigator.clipboard.writeText(JSON.stringify(config, null, 2));
|
void (async () => {
|
||||||
new Notice('✅ Configuration copied to clipboard');
|
await navigator.clipboard.writeText(JSON.stringify(config, null, 2));
|
||||||
|
new Notice('✅ Configuration copied to clipboard');
|
||||||
|
})();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Config JSON display
|
// Config JSON display
|
||||||
|
|||||||
@@ -162,14 +162,16 @@ export class NotificationHistoryModal extends Modal {
|
|||||||
|
|
||||||
// Export button
|
// Export button
|
||||||
const exportButton = actionsContainer.createEl('button', { text: 'Export to Clipboard' });
|
const exportButton = actionsContainer.createEl('button', { text: 'Export to Clipboard' });
|
||||||
exportButton.addEventListener('click', async () => {
|
exportButton.addEventListener('click', () => {
|
||||||
const exportData = JSON.stringify(this.filteredHistory, null, 2);
|
void (async () => {
|
||||||
await navigator.clipboard.writeText(exportData);
|
const exportData = JSON.stringify(this.filteredHistory, null, 2);
|
||||||
// Show temporary success message
|
await navigator.clipboard.writeText(exportData);
|
||||||
exportButton.textContent = '✅ Copied!';
|
// Show temporary success message
|
||||||
setTimeout(() => {
|
exportButton.textContent = '✅ Copied!';
|
||||||
exportButton.textContent = 'Export to Clipboard';
|
setTimeout(() => {
|
||||||
}, 2000);
|
exportButton.textContent = 'Export to Clipboard';
|
||||||
|
}, 2000);
|
||||||
|
})();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Close button
|
// Close button
|
||||||
|
|||||||
Reference in New Issue
Block a user