Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ export default class QuickAdd extends Plugin {
* Start the pending debounced settings write now. Returns the queued
* settings work, or null when there is none to wait for.
*/
private flushPendingSave(): Promise<void> | null {
flushPendingSave(): Promise<void> | null {
this.requestSave.run();
return this.persistChain;
}
Expand Down
4 changes: 4 additions & 0 deletions src/quickAddSettingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ export class QuickAddSettingsTab extends PluginSettingTab {
// net for the two Svelte mounts.
super.hide();
this.destroySettingViews();
// Closing settings is when a change is done. The pending write waits on
// the main window's timer, which the OS throttles while that window is
// hidden behind a Settings popout.
void this.plugin.flushPendingSave();
}

private destroySettingViews(): void {
Expand Down
20 changes: 13 additions & 7 deletions tests/e2e/settings-popout-save.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { setFocusEmulation, setVaultConfig } from "./uiHelpers";

const getContext = createQuickAddE2EHarness("settings-popout-save");

// A change made in the Settings popout is saved after the popout closes right after it (#2182):
// the debounced settings write must not be scheduled on the popout window.
// A change made in the Settings popout is saved when the popout closes right after it (#2182, #2213).
for (const mainWindow of ["visible", "hidden"] as const) {
it(`saves a reorder made in the Settings popout that closes right after, main window ${mainWindow}`, async () => {
const { obsidian, plugin } = getContext();
Expand All @@ -26,7 +25,7 @@ for (const mainWindow of ["visible", "hidden"] as const) {
const popout = await obsidian.dev.evalJson<boolean>("app.vault.getConfig('settingsPopoutWindow') ?? true");
try {
// Reorder by a native mouse drag in the popout, and close it 300 ms after the drop.
const inMemory = await obsidian.dev.evalJsonAsync<string[]>(`(async () => {
const { order: inMemory, pendingAtClose } = await obsidian.dev.evalJsonAsync<{ order: string[]; pendingAtClose: boolean }>(`(async () => {
const remote = require("@electron/remote");
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
${setVaultConfig("settingsPopoutWindow", true)};
Expand Down Expand Up @@ -69,13 +68,20 @@ for (const mainWindow of ["visible", "hidden"] as const) {
await popoutSleep(${mainWindow === "hidden" ? 2500 : 300});
contents.sendInputEvent({ type: "mouseUp", x: to.x, y: to.y, button: "left", clickCount: 1 });
await popoutSleep(300);
const order = app.plugins.plugins.quickadd.settings.choices.map((choice) => choice.name);
const plugin = app.plugins.plugins.quickadd;
const order = plugin.settings.choices.map((choice) => choice.name);
const pendingAtClose = plugin.pendingSave !== null;
app.setting.close();
return order;
// The write that closing started.
await plugin.persistChain;
return { order, pendingAtClose };
})()`);
expect(inMemory).not.toEqual(["Alpha", "Bravo", "Charlie"]);
// The write is debounced by 1 s, and a hidden window's timers run about once a second.
await expect.poll(onDisk, { timeout: 3000, interval: 250 }).toEqual(inMemory);
// Otherwise the debounce wrote it already, and closing proves nothing.
expect(pendingAtClose).toBe(true);
// Closing settings writes the change, rather than leaving it to a
// debounce on the main window's timer, which a hidden window throttles.
expect(onDisk()).toEqual(inMemory);
} finally {
await obsidian.dev.evalJson(`(() => {
require("@electron/remote").getCurrentWindow().show();
Expand Down
Loading