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

DesktopWebLinks.ts

97 lines3.8 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 ElectronWindow from "../electron/ElectronWindow.ts";
7import { WEB_LINK_OPEN_CHANNEL } from "../ipc/channels.ts";
8​
9/** An http(s) link the operating system handed to T3 Code as the default browser. */
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​
16/** An HTML file the operating system asked T3 Code to open, as browsers are asked to. */
17export const isWebPageFile = (path: string) => /\.x?html?$/i.test(path);
18​
19/**
20 * Web links and HTML files macOS opens with T3 Code once it is the default
21 * browser. Files arrive as file:// URLs. They can
22 * arrive before the app is ready (the link that launched it) or while the web
23 * app reloads, so they wait until the renderer says it is listening.
24 */
25export class DesktopWebLinks extends Context.Service<
26 DesktopWebLinks,
27 {
28 /** Queues a link and delivers it as soon as the renderer is listening. */
29 readonly receive: (url: string) => Effect.Effect<void>;
30 /** The renderer started or stopped listening; listening flushes queued links. */
31 readonly setRendererReady: (ready: boolean) => Effect.Effect<void>;
32 }
33>()("@t3tools/desktop/app/DesktopWebLinks") {}
34​
35const make = Effect.gen(function* () {
36 const electronWindow = yield* ElectronWindow.ElectronWindow;
37 const pending: Array<string> = [];
38 /** The page that said it is listening; a reload or crash replaces it, and it must say so again. */
39 let listening: Electron.WebContents | null = null;
40 let detachListening: (() => void) | null = null;
41​
42 const stopListening = () => {
43 detachListening?.();
44 detachListening = null;
45 listening = null;
46 };
47​
48 const flush = Effect.gen(function* () {
49 if (listening === null || pending.length === 0) return;
50 const window = yield* electronWindow.currentMainOrFirst;
51 if (Option.isNone(window) || window.value.webContents !== listening) return;
52 if (listening.isDestroyed()) {
53 stopListening();
54 return;
55 }
56 for (const url of pending.splice(0)) listening.send(WEB_LINK_OPEN_CHANNEL, url);
57 yield* electronWindow.reveal(window.value);
58 });
59​
60 const startListening = Effect.gen(function* () {
61 const window = yield* electronWindow.currentMainOrFirst;
62 if (Option.isNone(window) || window.value.webContents.isDestroyed()) return;
63 const webContents = window.value.webContents;
64 if (listening === webContents) return;
65 stopListening();
66 listening = webContents;
67 // Links that arrive while a new page loads wait for it to listen again.
68 const onNavigation = (
69 event: Electron.Event<Electron.WebContentsDidStartNavigationEventParams>,
70 ) => {
71 if (event.isMainFrame && !event.isSameDocument) stopListening();
72 };
73 webContents.on("did-start-navigation", onNavigation);
74 webContents.on("render-process-gone", stopListening);
75 webContents.once("destroyed", stopListening);
76 detachListening = () => {
77 webContents.removeListener("did-start-navigation", onNavigation);
78 webContents.removeListener("render-process-gone", stopListening);
79 webContents.removeListener("destroyed", stopListening);
80 };
81 });
82​
83 return DesktopWebLinks.of({
84 receive: (url) =>
85 Effect.suspend(() => {
86 pending.push(url);
87 return flush;
88 }).pipe(Effect.withSpan("DesktopWebLinks.receive")),
89 setRendererReady: (ready) =>
90 (ready ? startListening : Effect.sync(stopListening)).pipe(
91 Effect.andThen(flush),
92 Effect.withSpan("DesktopWebLinks.setRendererReady"),
93 ),
94 });
95});
96​
97export const layer = Layer.effect(DesktopWebLinks, make);