Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DesktopWebLinks.ts

89 lines3.3 KB

1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Option from "effect/Option";
5​
6import * as DesktopWindow from "../window/DesktopWindow.ts";
7import * as ElectronWindow from "../electron/ElectronWindow.ts";
8import { WEB_LINK_OPEN_CHANNEL } from "../ipc/channels.ts";
9​
10export const isWebLink = (value: string) => {
11 if (!URL.canParse(value)) return false;
12 const { protocol } = new URL(value);
13 return protocol === "http:" || protocol === "https:";
14};
15​
16export const isWebPageFile = (path: string) => /\.x?html?$/i.test(path);
17​
18export class DesktopWebLinks extends Context.Service<
19 DesktopWebLinks,
20 {
21 readonly receive: (url: string) => Effect.Effect<void>;
22 readonly setRendererReady: (ready: boolean) => Effect.Effect<void>;
23 }
24>()("@t3tools/desktop/app/DesktopWebLinks") {}
25​
26const make = Effect.gen(function* () {
27 const electronWindow = yield* ElectronWindow.ElectronWindow;
28 const desktopWindow = yield* DesktopWindow.DesktopWindow;
29 const pending: Array<string> = [];
30 let listening: Electron.WebContents | null = null;
31 let detachListening: (() => void) | null = null;
32 let rendererHasListened = false;
33​
34 const stopListening = () => {
35 detachListening?.();
36 detachListening = null;
37 listening = null;
38 };
39​
40 const flush = Effect.gen(function* () {
41 if (listening === null || pending.length === 0) return;
42 const window = yield* electronWindow.currentMainOrFirst;
43 if (Option.isNone(window) || window.value.webContents !== listening) return;
44 if (listening.isDestroyed()) {
45 stopListening();
46 return;
47 }
48 for (const url of pending.splice(0)) listening.send(WEB_LINK_OPEN_CHANNEL, url);
49 yield* desktopWindow.activate.pipe(Effect.ignore);
50 });
51​
52 const startListening = Effect.gen(function* () {
53 const window = yield* electronWindow.currentMainOrFirst;
54 if (Option.isNone(window) || window.value.webContents.isDestroyed()) return;
55 const webContents = window.value.webContents;
56 if (listening === webContents) return;
57 stopListening();
58 listening = webContents;
59 rendererHasListened = true;
60 const onNavigation = (
61 event: Electron.Event<Electron.WebContentsDidStartNavigationEventParams>,
62 ) => {
63 if (event.isMainFrame && !event.isSameDocument) stopListening();
64 };
65 webContents.on("did-start-navigation", onNavigation);
66 webContents.on("render-process-gone", stopListening);
67 webContents.once("destroyed", stopListening);
68 detachListening = () => {
69 webContents.removeListener("did-start-navigation", onNavigation);
70 webContents.removeListener("render-process-gone", stopListening);
71 webContents.removeListener("destroyed", stopListening);
72 };
73 });
74​
75 return DesktopWebLinks.of({
76 receive: Effect.fn("DesktopWebLinks.receive")(function* (url) {
77 pending.push(url);
78 yield* flush;
79 if (!rendererHasListened || Option.isSome(yield* electronWindow.main)) return;
80 // macOS fires `activate` only on reopen, not when a link opens the app.
81 yield* desktopWindow.activate.pipe(Effect.ignore);
82 }),
83 setRendererReady: Effect.fn("DesktopWebLinks.setRendererReady")(function (ready) {
84 return ready ? startListening : Effect.sync(stopListening);
85 }, Effect.andThen(flush)),
86 });
87});
88​
89export const layer = Layer.effect(DesktopWebLinks, make);