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

ElectronApp.ts

213 lines7.2 KB

1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Schema from "effect/Schema";
5import * as Scope from "effect/Scope";
6​
7import * as Electron from "electron";
8​
9export interface ElectronAppMetadata {
10 readonly appVersion: string;
11 readonly appPath: string;
12 readonly isPackaged: boolean;
13 readonly resourcesPath: string;
14 readonly runningUnderArm64Translation: boolean;
16​
17export class ElectronAppMetadataReadError extends Schema.TaggedErrorClass<ElectronAppMetadataReadError>()(
18 "ElectronAppMetadataReadError",
19 {
20 property: Schema.Literals(["app-version", "app-path"]),
21 cause: Schema.Defect(),
22 },
23) {
24 override get message(): string {
25 return `Failed to read Electron app metadata property "${this.property}".`;
26 }
28​
29export class ElectronAppWhenReadyError extends Schema.TaggedErrorClass<ElectronAppWhenReadyError>()(
30 "ElectronAppWhenReadyError",
31 {
32 isPackaged: Schema.Boolean,
33 cause: Schema.Defect(),
34 },
35) {
36 override get message(): string {
37 return `Failed to wait for the Electron app to become ready (packaged: ${this.isPackaged}).`;
38 }
40​
41export class ElectronApp extends Context.Service<
42 ElectronApp,
43 {
44 readonly metadata: Effect.Effect<ElectronAppMetadata, ElectronAppMetadataReadError>;
45 readonly name: Effect.Effect<string>;
46 /**
47 * The OS locale, read from the operating system rather than from Chromium's
48 * resolved application locale — the packaged app ships only the `en-US`
49 * locale pak, so `app.getLocale()` and the renderer's `Intl` default are
50 * pinned to `en-US` however the machine is configured.
51 */
52 readonly systemLocale: Effect.Effect<string>;
53 readonly whenReady: Effect.Effect<void, ElectronAppWhenReadyError>;
54 readonly quit: Effect.Effect<void>;
55 readonly exit: (code: number) => Effect.Effect<void>;
56 readonly relaunch: (options: Electron.RelaunchOptions) => Effect.Effect<void>;
57 readonly setPath: (
58 name: Parameters<Electron.App["setPath"]>[0],
59 path: string,
60 ) => Effect.Effect<void>;
61 readonly setName: (name: string) => Effect.Effect<void>;
62 readonly setAboutPanelOptions: (
63 options: Electron.AboutPanelOptionsOptions,
64 ) => Effect.Effect<void>;
65 readonly setAppUserModelId: (id: string) => Effect.Effect<void>;
66 readonly getAppMetrics: Effect.Effect<ReadonlyArray<Electron.ProcessMetric>>;
67 readonly isDefaultProtocolClient: (protocol: string) => Effect.Effect<boolean>;
68 readonly setAsDefaultProtocolClient: (
69 protocol: string,
70 path?: string,
71 args?: readonly string[],
72 ) => Effect.Effect<boolean>;
73 readonly setDesktopName: (desktopName: string) => Effect.Effect<void>;
74 readonly setDockIcon: (iconPath: string) => Effect.Effect<void>;
75 readonly appendCommandLineSwitch: (switchName: string, value?: string) => Effect.Effect<void>;
76 readonly onBeforeQuitForUpdate: (
77 listener: () => void,
78 ) => Effect.Effect<void, never, Scope.Scope>;
79 readonly removeCommandLineSwitch: (switchName: string) => Effect.Effect<void>;
80 readonly on: <Args extends ReadonlyArray<unknown>>(
81 eventName: string,
82 listener: (...args: Args) => void,
83 ) => Effect.Effect<void, never, Scope.Scope>;
84 }
85>()("@t3tools/desktop/electron/ElectronApp") {}
86​
87const addScopedAppListener = <Args extends ReadonlyArray<unknown>>(
88 eventName: string,
89 listener: (...args: Args) => void,
90): Effect.Effect<void, never, Scope.Scope> =>
91 Effect.acquireRelease(
92 Effect.sync(() => {
93 Electron.app.on(eventName as any, listener as any);
94 }),
95 () =>
96 Effect.sync(() => {
97 Electron.app.removeListener(eventName as any, listener as any);
98 }),
99 ).pipe(Effect.asVoid);
100​
101export const make = ElectronApp.of({
102 metadata: Effect.gen(function* () {
103 const appVersion = yield* Effect.try({
104 try: () => Electron.app.getVersion(),
105 catch: (cause) =>
106 new ElectronAppMetadataReadError({
107 property: "app-version",
108 cause,
109 }),
110 });
111 const appPath = yield* Effect.try({
112 try: () => Electron.app.getAppPath(),
113 catch: (cause) =>
114 new ElectronAppMetadataReadError({
115 property: "app-path",
116 cause,
117 }),
118 });
119​
120 return {
121 appVersion,
122 appPath,
123 isPackaged: Electron.app.isPackaged,
124 resourcesPath: process.resourcesPath,
125 runningUnderArm64Translation: Electron.app.runningUnderARM64Translation === true,
126 };
127 }),
128 name: Effect.sync(() => Electron.app.name),
129 // macOS derives this from NSLocale, which uses POSIX-style identifiers
130 // (`en_GB`). `Intl` rejects those outright rather than normalizing them, so
131 // the tag is normalized here rather than in the renderer that consumes it.
132 systemLocale: Effect.sync(() => Electron.app.getSystemLocale().replace(/_/g, "-")),
133 whenReady: Effect.gen(function* () {
134 const isPackaged = Electron.app.isPackaged;
135 yield* Effect.tryPromise({
136 try: () => Electron.app.whenReady(),
137 catch: (cause) => new ElectronAppWhenReadyError({ isPackaged, cause }),
138 });
139 }),
140 quit: Effect.sync(() => {
141 Electron.app.quit();
142 }),
143 exit: (code) =>
144 Effect.sync(() => {
145 Electron.app.exit(code);
146 }),
147 relaunch: (options) =>
148 Effect.sync(() => {
149 Electron.app.relaunch(options);
150 }),
151 setPath: (name, path) =>
152 Effect.sync(() => {
153 Electron.app.setPath(name, path);
154 }),
155 setName: (name) =>
156 Effect.sync(() => {
157 Electron.app.setName(name);
158 }),
159 setAboutPanelOptions: (options) =>
160 Effect.sync(() => {
161 Electron.app.setAboutPanelOptions(options);
162 }),
163 setAppUserModelId: (id) =>
164 Effect.sync(() => {
165 Electron.app.setAppUserModelId(id);
166 }),
167 getAppMetrics: Effect.sync(() => Electron.app.getAppMetrics()),
168 isDefaultProtocolClient: (protocol) =>
169 Effect.sync(() => Electron.app.isDefaultProtocolClient(protocol)),
170 setAsDefaultProtocolClient: (protocol, path, args) =>
171 Effect.sync(() => {
172 if (path === undefined) {
173 return Electron.app.setAsDefaultProtocolClient(protocol);
174 }
175 return Electron.app.setAsDefaultProtocolClient(protocol, path, [...(args ?? [])]);
176 }),
177 setDesktopName: (desktopName) =>
178 Effect.sync(() => {
179 const linuxApp = Electron.app as Electron.App & {
180 setDesktopName?: (desktopName: string) => void;
181 };
182 linuxApp.setDesktopName?.(desktopName);
183 }),
184 setDockIcon: (iconPath) =>
185 Effect.sync(() => {
186 Electron.app.dock?.setIcon(iconPath);
187 }),
188 appendCommandLineSwitch: (switchName, value) =>
189 Effect.sync(() => {
190 if (value === undefined) {
191 Electron.app.commandLine.appendSwitch(switchName);
192 return;
193 }
194 Electron.app.commandLine.appendSwitch(switchName, value);
195 }),
196 onBeforeQuitForUpdate: (listener) =>
197 Effect.acquireRelease(
198 Effect.sync(() => {
199 Electron.autoUpdater.on("before-quit-for-update", listener);
200 }),
201 () =>
202 Effect.sync(() => {
203 Electron.autoUpdater.removeListener("before-quit-for-update", listener);
204 }),
205 ).pipe(Effect.asVoid),
206 removeCommandLineSwitch: (switchName) =>
207 Effect.sync(() => {
208 Electron.app.commandLine.removeSwitch(switchName);
209 }),
210 on: addScopedAppListener,
211});
212​
213export const layer = Layer.succeed(ElectronApp, make);