Skip to content

Commit f6bc1e5

Browse files
committed
fix(desktop): route prevent-sleep writes through the side-effecting IPC
The generic store-set/store-delete/store-clear IPCs could write the preventSleep key in the settings store without starting/stopping the powerSaveBlocker, desyncing the persisted setting from the running blocker (a string "false" even resolved back to enabled on next launch). - store-set: reject writes to the preventSleep key; the dedicated set-prevent-sleep IPC is now the single write path - store-delete/store-clear: after touching the settings store, re-sync the blocker via syncPowerSaveBlocker() so the reset to the default-on behavior takes effect immediately Verified with the macOS prevent-sleep smoke (4/4) plus a Playwright guard check: rejected store-set leaves the assertion running, and store-delete/store-clear restore the NoIdleSleepAssertion. Signed-off-by: thomas-yanga <odie_majere@outlook.com>
1 parent 8b21551 commit f6bc1e5

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

packages/desktop/src/main/ipc.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import { archivePath, copyPath, extractPath, guardFileOpCall, movePath, removePa
1212
import { fileLog, isTracked } from "./git"
1313
import { getStore } from "./store"
1414
import { getPinchZoomEnabled, setPinchZoomEnabled, setTitlebar, updateTitlebar } from "./windows"
15-
import { getPreventSleepEnabled, setPreventSleepEnabled } from "./power"
15+
import { getPreventSleepEnabled, setPreventSleepEnabled, syncPowerSaveBlocker } from "./power"
16+
import { PREVENT_SLEEP_KEY, SETTINGS_STORE } from "./store-keys"
1617
import { browserView } from "./browser-view"
1718
import type { UpdaterController } from "./updater-controller"
1819
import { createUpdaterSubscriptions } from "./updater-subscriptions"
@@ -93,13 +94,21 @@ export function registerIpcHandlers(deps: Deps) {
9394
}
9495
})
9596
ipcMain.handle("store-set", (_event: IpcMainInvokeEvent, name: string, key: string, value: string) => {
97+
// preventSleep drives a live powerSaveBlocker side effect; routing writes through the
98+
// dedicated set-prevent-sleep IPC keeps the blocker in sync with the persisted value.
99+
if (name === SETTINGS_STORE && key === PREVENT_SLEEP_KEY) {
100+
throw new Error(`"${PREVENT_SLEEP_KEY}" must be written via the set-prevent-sleep IPC`)
101+
}
96102
getStore(name).set(key, value)
97103
})
98104
ipcMain.handle("store-delete", (_event: IpcMainInvokeEvent, name: string, key: string) => {
99105
getStore(name).delete(key)
106+
// Deleting the key resets it to the default-on behavior; re-sync so the blocker matches.
107+
if (name === SETTINGS_STORE && key === PREVENT_SLEEP_KEY) syncPowerSaveBlocker()
100108
})
101109
ipcMain.handle("store-clear", (_event: IpcMainInvokeEvent, name: string) => {
102110
getStore(name).clear()
111+
if (name === SETTINGS_STORE) syncPowerSaveBlocker()
103112
})
104113
ipcMain.handle("store-keys", (_event: IpcMainInvokeEvent, name: string) => {
105114
const store = getStore(name)

packages/desktop/src/main/power.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ export function getPreventSleepEnabled(): boolean {
1414

1515
export function setPreventSleepEnabled(enabled: boolean): void {
1616
getStore().set(PREVENT_SLEEP_KEY, enabled)
17-
if (enabled) {
17+
syncPowerSaveBlocker()
18+
}
19+
20+
// Reconciles the running blocker with the persisted setting. Must be called after any write path
21+
// that can change the stored value outside setPreventSleepEnabled (generic store delete/clear).
22+
export function syncPowerSaveBlocker(): void {
23+
if (getPreventSleepEnabled()) {
1824
startPowerSaveBlocker()
1925
return
2026
}

0 commit comments

Comments
 (0)