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

DesktopWebLinks.ts

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