GeezerVison is showing github.com — fix(desktop): Linux notifications no longer freeze the app when the n… · pingdotgg/t3code@fbade18
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/desktop/src/app/DesktopApp.test.ts +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,6 +37,7 @@ import * as DesktopBackendPool from "../backend/DesktopBackendPool.ts"; | 37 | 37 | import * as DesktopServerExposure from "../backend/DesktopServerExposure.ts"; | 38 | 38 | import * as ElectronApp from "../electron/ElectronApp.ts"; | 39 | 39 | import * as ElectronDialog from "../electron/ElectronDialog.ts"; | | 40 | +import * as ElectronNotifications from "../electron/ElectronNotifications.ts"; | 40 | 41 | import * as ElectronProtocol from "../electron/ElectronProtocol.ts"; | 41 | 42 | import * as ElectronSafeStorage from "../electron/ElectronSafeStorage.ts"; | 42 | 43 | import * as PreviewPasskeys from "../preview/Passkeys.ts"; | @@ -133,6 +134,10 @@ it.effect( | 133 | 134 | Effect.provideService(PreviewPasskeys.PreviewPasskeys, { | 134 | 135 | configure: Effect.void, | 135 | 136 | } as PreviewPasskeys.PreviewPasskeys["Service"]), | | 137 | + Effect.provideService( | | 138 | + ElectronNotifications.ElectronNotifications, | | 139 | + ElectronNotifications.makeWith(undefined), | | 140 | + ), | 136 | 141 | Effect.provideService(DesktopApplicationMenu.DesktopApplicationMenu, { | 137 | 142 | configure: Effect.void, | 138 | 143 | }), | |
Collapse file Copy file name to clipboardExpand all lines: apps/desktop/src/app/DesktopApp.ts +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,6 +9,7 @@ import * as NetService from "@t3tools/shared/Net"; | 9 | 9 | import * as SecureRandom from "effect/crypto/SecureRandom"; | 10 | 10 | import * as ElectronApp from "../electron/ElectronApp.ts"; | 11 | 11 | import * as ElectronDialog from "../electron/ElectronDialog.ts"; | | 12 | +import * as ElectronNotifications from "../electron/ElectronNotifications.ts"; | 12 | 13 | import * as ElectronProtocol from "../electron/ElectronProtocol.ts"; | 13 | 14 | import * as ElectronSafeStorage from "../electron/ElectronSafeStorage.ts"; | 14 | 15 | import { installDesktopIpcHandlers } from "../ipc/DesktopIpcHandlers.ts"; | @@ -277,6 +278,7 @@ const startup = Effect.gen(function* () { | 277 | 278 | const updates = yield* DesktopUpdates.DesktopUpdates; | 278 | 279 | const environment = yield* DesktopEnvironment.DesktopEnvironment; | 279 | 280 | const previewPasskeys = yield* PreviewPasskeys.PreviewPasskeys; | | 281 | + const notifications = yield* ElectronNotifications.ElectronNotifications; | 280 | 282 |
| 281 | 283 | yield* shellEnvironment.installIntoProcess; | 282 | 284 | const hasCommandLinePasswordStore = | @@ -333,6 +335,7 @@ const startup = Effect.gen(function* () { | 333 | 335 | } | 334 | 336 | yield* appIdentity.configure; | 335 | 337 | yield* previewPasskeys.configure; | | 338 | + yield* notifications.configure; | 336 | 339 | yield* applicationMenu.configure; | 337 | 340 | yield* updates.configure; | 338 | 341 | yield* DesktopRemoteUpdates.listen; | |
Collapse file Copy file name to clipboard +60Lines changed: 60 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import * as HostProcess from "@t3tools/shared/HostProcess"; | | 2 | +import * as Context from "effect/Context"; | | 3 | +import * as Effect from "effect/Effect"; | | 4 | +import * as Layer from "effect/Layer"; | | 5 | +import type * as Scope from "effect/Scope"; | | 6 | + | | 7 | +import * as Electron from "electron"; | | 8 | + | | 9 | +import { watchLinuxNotificationService } from "./LinuxNotificationService.ts"; | | 10 | + | | 11 | +export class ElectronNotifications extends Context.Service< | | 12 | + ElectronNotifications, | | 13 | + { | | 14 | + readonly configure: Effect.Effect<void, never, Scope.Scope>; | | 15 | + } | | 16 | +>()("@t3tools/desktop/electron/ElectronNotifications") {} | | 17 | + | | 18 | +type WatchNotificationService = () => { | | 19 | + readonly available: () => boolean; | | 20 | + readonly stop: () => void; | | 21 | +}; | | 22 | + | | 23 | +export const makeWith = (watchNotificationService: WatchNotificationService | undefined) => | | 24 | + ElectronNotifications.of({ | | 25 | + configure: Effect.gen(function* () { | | 26 | + if (!watchNotificationService) return; | | 27 | + yield* Effect.acquireRelease( | | 28 | + Effect.sync(() => { | | 29 | + let watcher: ReturnType<WatchNotificationService> | undefined; | | 30 | + try { | | 31 | + watcher = watchNotificationService(); | | 32 | + } catch { | | 33 | + watcher = undefined; | | 34 | + } | | 35 | + // Renderers see "denied" until the service answers, so `new Notification` never reaches | | 36 | + // libnotify while activation can still stall Electron's main thread. | | 37 | + Electron.session.defaultSession.setPermissionCheckHandler((_webContents, permission) => | | 38 | + permission === "notifications" | | 39 | + ? watcher?.available() === true | | 40 | + : // Electron's answer when no check handler is installed. | | 41 | + permission !== "deprecated-sync-clipboard-read", | | 42 | + ); | | 43 | + return watcher; | | 44 | + }), | | 45 | + (watcher) => | | 46 | + Effect.sync(() => { | | 47 | + Electron.session.defaultSession.setPermissionCheckHandler(null); | | 48 | + watcher?.stop(); | | 49 | + }), | | 50 | + ); | | 51 | + }).pipe(Effect.withSpan("desktop.electronNotifications.configure")), | | 52 | + }); | | 53 | + | | 54 | +/** @public */ | | 55 | +export const make = Effect.gen(function* () { | | 56 | + const platform = yield* HostProcess.Platform; | | 57 | + return makeWith(platform === "linux" ? watchLinuxNotificationService : undefined); | | 58 | +}); | | 59 | + | | 60 | +export const layer = Layer.effect(ElectronNotifications, make); |
Collapse file Copy file name to clipboard +50Lines changed: 50 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { Message, sessionBus, type MessageBus } from "dbus-next"; | | 2 | + | | 3 | +const DBUS = "org.freedesktop.DBus"; | | 4 | +const NOTIFICATIONS = "org.freedesktop.Notifications"; | | 5 | + | | 6 | +/** | | 7 | + * Tracks whether the desktop notification service answers on the session bus. Electron shows | | 8 | + * notifications through libnotify on its main thread, which blocks until the bus gives up | | 9 | + * activating a service that never claims its name, so callers must only notify once this has | | 10 | + * seen the service start. | | 11 | + */ | | 12 | +export function watchLinuxNotificationService(bus: MessageBus = sessionBus()) { | | 13 | + let available = false; | | 14 | + let probing = false; | | 15 | + let closed = false; | | 16 | + | | 17 | + const settle = (started: boolean) => () => { | | 18 | + available = started; | | 19 | + probing = false; | | 20 | + }; | | 21 | + const probe = () => { | | 22 | + probing = true; | | 23 | + bus | | 24 | + .call( | | 25 | + new Message({ | | 26 | + destination: DBUS, | | 27 | + path: "/org/freedesktop/DBus", | | 28 | + interface: DBUS, | | 29 | + member: "StartServiceByName", | | 30 | + signature: "su", | | 31 | + body: [NOTIFICATIONS, 0], | | 32 | + }), | | 33 | + ) | | 34 | + .then(settle(true), settle(false)); | | 35 | + }; | | 36 | + | | 37 | + bus.on("error", () => undefined); | | 38 | + probe(); | | 39 | + | | 40 | + return { | | 41 | + available: () => { | | 42 | + if (!available && !probing && !closed) probe(); | | 43 | + return available; | | 44 | + }, | | 45 | + stop: () => { | | 46 | + closed = true; | | 47 | + bus.disconnect(); | | 48 | + }, | | 49 | + }; | | 50 | +} |
Collapse file Copy file name to clipboardExpand all lines: apps/desktop/src/main.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,6 +27,7 @@ import * as DesktopIpc from "./ipc/DesktopIpc.ts"; | 27 | 27 | import * as ElectronApp from "./electron/ElectronApp.ts"; | 28 | 28 | import * as ElectronDialog from "./electron/ElectronDialog.ts"; | 29 | 29 | import * as ElectronMenu from "./electron/ElectronMenu.ts"; | | 30 | +import * as ElectronNotifications from "./electron/ElectronNotifications.ts"; | 30 | 31 | import * as ElectronPowerMonitor from "./electron/ElectronPowerMonitor.ts"; | 31 | 32 | import * as ElectronProtocol from "./electron/ElectronProtocol.ts"; | 32 | 33 | import * as ElectronSafeStorage from "./electron/ElectronSafeStorage.ts"; | @@ -132,6 +133,7 @@ const layerElectron = Layer.mergeAll( | 132 | 133 | ElectronApp.layer, | 133 | 134 | ElectronDialog.layer, | 134 | 135 | ElectronMenu.layer, | | 136 | + ElectronNotifications.layer, | 135 | 137 | ElectronPowerMonitor.layer, | 136 | 138 | ElectronProtocol.layer, | 137 | 139 | ElectronSafeStorage.layer, | |
|