refactor(ext/popup): extract teardownSettingsCommon (Plan C Phase 5)
DEV-C P2: settings.ts:56-65 and settings-vault.ts:15-22 had near- identical cleanup paths. Single source for closeGeneratorPanel + activeKeyHandler removal. Helper takes the handler as a parameter and returns null so each caller still owns its own module-scoped handler state. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@ import type {
|
||||
import type { SessionTimeoutConfig } from '../../shared/messages';
|
||||
import { relativeTime } from '../../shared/relative-time';
|
||||
import { openGeneratorPanel, closeGeneratorPanel, isGeneratorPanelOpen } from './generator-panel';
|
||||
import { teardownSettingsCommon } from './settings';
|
||||
import { GLYPH_NEXT } from '../../shared/glyphs';
|
||||
|
||||
let pendingSettings: VaultSettings | null = null;
|
||||
@@ -17,11 +18,7 @@ let pendingSession: SessionTimeoutConfig | null = null;
|
||||
let baseSession: SessionTimeoutConfig | null = null;
|
||||
|
||||
export function teardown(): void {
|
||||
closeGeneratorPanel();
|
||||
if (activeKeyHandler) {
|
||||
document.removeEventListener('keydown', activeKeyHandler);
|
||||
activeKeyHandler = null;
|
||||
}
|
||||
activeKeyHandler = teardownSettingsCommon(activeKeyHandler);
|
||||
pendingSettings = null;
|
||||
pendingSession = null;
|
||||
baseSession = null;
|
||||
|
||||
@@ -53,13 +53,29 @@ export async function renderSettings(container: HTMLElement): Promise<void> {
|
||||
await renderSection(activeSection);
|
||||
}
|
||||
|
||||
export function teardownSettings(): void {
|
||||
/**
|
||||
* Common cleanup invoked by both the device-settings teardown
|
||||
* (settings.ts) and the vault-settings teardown (settings-vault.ts).
|
||||
* Centralized to avoid the "regression class with known prior leaks"
|
||||
* DEV-C P2 flagged.
|
||||
*
|
||||
* Closes the generator popover and detaches the supplied keydown
|
||||
* handler from the document if present. Returns the new handler value
|
||||
* (always null), so the caller can do `handler = teardownSettingsCommon(handler)`.
|
||||
*/
|
||||
export function teardownSettingsCommon(
|
||||
keyHandler: ((e: KeyboardEvent) => void) | null,
|
||||
): null {
|
||||
closeGeneratorPanel();
|
||||
teardownSecuritySection();
|
||||
if (activeKeyHandler) {
|
||||
document.removeEventListener('keydown', activeKeyHandler);
|
||||
activeKeyHandler = null;
|
||||
if (keyHandler) {
|
||||
document.removeEventListener('keydown', keyHandler);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function teardownSettings(): void {
|
||||
activeKeyHandler = teardownSettingsCommon(activeKeyHandler);
|
||||
teardownSecuritySection();
|
||||
pendingVaultSettings = null;
|
||||
sessionHandle = null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user