Open History to see the last commit
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;
15}
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 }
27}
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 }
39}
40
41export class ElectronApp extends Context.Service<
42 ElectronApp,
43 {
44 readonly metadata: Effect.Effect<ElectronAppMetadata, ElectronAppMetadataReadError>;
45 readonly name: Effect.Effect<string>;
46 readonly whenReady: Effect.Effect<void, ElectronAppWhenReadyError>;
47 readonly quit: Effect.Effect<void>;
48 readonly exit: (code: number) => Effect.Effect<void>;
49 readonly relaunch: (options: Electron.RelaunchOptions) => Effect.Effect<void>;
50 readonly setPath: (
51 name: Parameters<Electron.App["setPath"]>[0],
52 path: string,
53 ) => Effect.Effect<void>;
54 readonly setName: (name: string) => Effect.Effect<void>;
55 readonly setAboutPanelOptions: (
56 options: Electron.AboutPanelOptionsOptions,
57 ) => Effect.Effect<void>;
58 readonly setAppUserModelId: (id: string) => Effect.Effect<void>;
59 readonly getAppMetrics: Effect.Effect<ReadonlyArray<Electron.ProcessMetric>>;
60 readonly isDefaultProtocolClient: (protocol: string) => Effect.Effect<boolean>;
61 readonly setAsDefaultProtocolClient: (
62 protocol: string,
63 path?: string,
64 args?: readonly string[],
65 ) => Effect.Effect<boolean>;
66 readonly setDesktopName: (desktopName: string) => Effect.Effect<void>;
67 readonly setDockIcon: (iconPath: string) => Effect.Effect<void>;
68 readonly appendCommandLineSwitch: (switchName: string, value?: string) => Effect.Effect<void>;
69 readonly onBeforeQuitForUpdate: (
70 listener: () => void,
71 ) => Effect.Effect<void, never, Scope.Scope>;
72 readonly on: <Args extends ReadonlyArray<unknown>>(
73 eventName: string,
74 listener: (...args: Args) => void,
75 ) => Effect.Effect<void, never, Scope.Scope>;
76 }
77>()("@t3tools/desktop/electron/ElectronApp") {}
78
79const addScopedAppListener = <Args extends ReadonlyArray<unknown>>(
80 eventName: string,
81 listener: (...args: Args) => void,
82): Effect.Effect<void, never, Scope.Scope> =>
83 Effect.acquireRelease(
84 Effect.sync(() => {
85 Electron.app.on(eventName as any, listener as any);
86 }),
87 () =>
88 Effect.sync(() => {
89 Electron.app.removeListener(eventName as any, listener as any);
90 }),
91 ).pipe(Effect.asVoid);
92
93export const make = ElectronApp.of({
94 metadata: Effect.gen(function* () {
95 const appVersion = yield* Effect.try({
96 try: () => Electron.app.getVersion(),
97 catch: (cause) =>
98 new ElectronAppMetadataReadError({
99 property: "app-version",
100 cause,
101 }),
102 });
103 const appPath = yield* Effect.try({
104 try: () => Electron.app.getAppPath(),
105 catch: (cause) =>
106 new ElectronAppMetadataReadError({
107 property: "app-path",
108 cause,
109 }),
110 });
111
112 return {
113 appVersion,
114 appPath,
115 isPackaged: Electron.app.isPackaged,
116 resourcesPath: process.resourcesPath,
117 runningUnderArm64Translation: Electron.app.runningUnderARM64Translation === true,
118 };
119 }),
120 name: Effect.sync(() => Electron.app.name),
121 whenReady: Effect.gen(function* () {
122 const isPackaged = Electron.app.isPackaged;
123 yield* Effect.tryPromise({
124 try: () => Electron.app.whenReady(),
125 catch: (cause) => new ElectronAppWhenReadyError({ isPackaged, cause }),
126 });
127 }),
128 quit: Effect.sync(() => {
129 Electron.app.quit();
130 }),
131 exit: (code) =>
132 Effect.sync(() => {
133 Electron.app.exit(code);
134 }),
135 relaunch: (options) =>
136 Effect.sync(() => {
137 Electron.app.relaunch(options);
138 }),
139 setPath: (name, path) =>
140 Effect.sync(() => {
141 Electron.app.setPath(name, path);
142 }),
143 setName: (name) =>
144 Effect.sync(() => {
145 Electron.app.setName(name);
146 }),
147 setAboutPanelOptions: (options) =>
148 Effect.sync(() => {
149 Electron.app.setAboutPanelOptions(options);
150 }),
151 setAppUserModelId: (id) =>
152 Effect.sync(() => {
153 Electron.app.setAppUserModelId(id);
154 }),
155 getAppMetrics: Effect.sync(() => Electron.app.getAppMetrics()),
156 isDefaultProtocolClient: (protocol) =>
157 Effect.sync(() => Electron.app.isDefaultProtocolClient(protocol)),
158 setAsDefaultProtocolClient: (protocol, path, args) =>
159 Effect.sync(() => {
160 if (path === undefined) {
161 return Electron.app.setAsDefaultProtocolClient(protocol);
162 }
163 return Electron.app.setAsDefaultProtocolClient(protocol, path, [...(args ?? [])]);
164 }),
165 setDesktopName: (desktopName) =>
166 Effect.sync(() => {
167 const linuxApp = Electron.app as Electron.App & {
168 setDesktopName?: (desktopName: string) => void;
169 };
170 linuxApp.setDesktopName?.(desktopName);
171 }),
172 setDockIcon: (iconPath) =>
173 Effect.sync(() => {
174 Electron.app.dock?.setIcon(iconPath);
175 }),
176 appendCommandLineSwitch: (switchName, value) =>
177 Effect.sync(() => {
178 if (value === undefined) {
179 Electron.app.commandLine.appendSwitch(switchName);
180 return;
181 }
182 Electron.app.commandLine.appendSwitch(switchName, value);
183 }),
184 onBeforeQuitForUpdate: (listener) =>
185 Effect.acquireRelease(
186 Effect.sync(() => {
187 Electron.autoUpdater.on("before-quit-for-update", listener);
188 }),
189 () =>
190 Effect.sync(() => {
191 Electron.autoUpdater.removeListener("before-quit-for-update", listener);
192 }),
193 ).pipe(Effect.asVoid),
194 on: addScopedAppListener,
195});
196
197export const layer = Layer.succeed(ElectronApp, make);