GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(desktop): Linux notifications no longer freeze the app when the n… · pingdotgg/t3code@fbade18

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/desktop/src
    • app
      • DesktopApp.test.ts
      • DesktopApp.ts
    • electron
      • ElectronNotifications.ts
      • LinuxNotificationService.ts
    • main.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/desktop/src/app/DesktopApp.test.ts‎

Copy file name to clipboardExpand all lines: apps/desktop/src/app/DesktopApp.test.ts
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import * as DesktopBackendPool from "../backend/DesktopBackendPool.ts";
3737
import * as DesktopServerExposure from "../backend/DesktopServerExposure.ts";
3838
import * as ElectronApp from "../electron/ElectronApp.ts";
3939
import * as ElectronDialog from "../electron/ElectronDialog.ts";
40+
import * as ElectronNotifications from "../electron/ElectronNotifications.ts";
4041
import * as ElectronProtocol from "../electron/ElectronProtocol.ts";
4142
import * as ElectronSafeStorage from "../electron/ElectronSafeStorage.ts";
4243
import * as PreviewPasskeys from "../preview/Passkeys.ts";
@@ -133,6 +134,10 @@ it.effect(
133134
Effect.provideService(PreviewPasskeys.PreviewPasskeys, {
134135
configure: Effect.void,
135136
} as PreviewPasskeys.PreviewPasskeys["Service"]),
137+
Effect.provideService(
138+
ElectronNotifications.ElectronNotifications,
139+
ElectronNotifications.makeWith(undefined),
140+
),
136141
Effect.provideService(DesktopApplicationMenu.DesktopApplicationMenu, {
137142
configure: Effect.void,
138143
}),
Collapse file

‎apps/desktop/src/app/DesktopApp.ts‎

Copy file name to clipboardExpand all lines: apps/desktop/src/app/DesktopApp.ts
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as NetService from "@t3tools/shared/Net";
99
import * as SecureRandom from "effect/crypto/SecureRandom";
1010
import * as ElectronApp from "../electron/ElectronApp.ts";
1111
import * as ElectronDialog from "../electron/ElectronDialog.ts";
12+
import * as ElectronNotifications from "../electron/ElectronNotifications.ts";
1213
import * as ElectronProtocol from "../electron/ElectronProtocol.ts";
1314
import * as ElectronSafeStorage from "../electron/ElectronSafeStorage.ts";
1415
import { installDesktopIpcHandlers } from "../ipc/DesktopIpcHandlers.ts";
@@ -277,6 +278,7 @@ const startup = Effect.gen(function* () {
277278
const updates = yield* DesktopUpdates.DesktopUpdates;
278279
const environment = yield* DesktopEnvironment.DesktopEnvironment;
279280
const previewPasskeys = yield* PreviewPasskeys.PreviewPasskeys;
281+
const notifications = yield* ElectronNotifications.ElectronNotifications;
280282

281283
yield* shellEnvironment.installIntoProcess;
282284
const hasCommandLinePasswordStore =
@@ -333,6 +335,7 @@ const startup = Effect.gen(function* () {
333335
}
334336
yield* appIdentity.configure;
335337
yield* previewPasskeys.configure;
338+
yield* notifications.configure;
336339
yield* applicationMenu.configure;
337340
yield* updates.configure;
338341
yield* DesktopRemoteUpdates.listen;
Collapse file

‎apps/desktop/src/electron/ElectronNotifications.ts‎

Copy file name to clipboard
+60Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

‎apps/desktop/src/electron/LinuxNotificationService.ts‎

Copy file name to clipboard
+50Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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

‎apps/desktop/src/main.ts‎

Copy file name to clipboardExpand all lines: apps/desktop/src/main.ts
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import * as DesktopIpc from "./ipc/DesktopIpc.ts";
2727
import * as ElectronApp from "./electron/ElectronApp.ts";
2828
import * as ElectronDialog from "./electron/ElectronDialog.ts";
2929
import * as ElectronMenu from "./electron/ElectronMenu.ts";
30+
import * as ElectronNotifications from "./electron/ElectronNotifications.ts";
3031
import * as ElectronPowerMonitor from "./electron/ElectronPowerMonitor.ts";
3132
import * as ElectronProtocol from "./electron/ElectronProtocol.ts";
3233
import * as ElectronSafeStorage from "./electron/ElectronSafeStorage.ts";
@@ -132,6 +133,7 @@ const layerElectron = Layer.mergeAll(
132133
ElectronApp.layer,
133134
ElectronDialog.layer,
134135
ElectronMenu.layer,
136+
ElectronNotifications.layer,
135137
ElectronPowerMonitor.layer,
136138
ElectronProtocol.layer,
137139
ElectronSafeStorage.layer,

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.