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