| ... | ... | @@ -1,6 +1,7 @@ |
| 1 | 1 | import * as Context from "effect/Context"; |
| 2 | 2 | import * as Effect from "effect/Effect"; |
| 3 | 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as Schema from "effect/Schema"; |
| 4 | 5 | import * as Scope from "effect/Scope"; |
| 5 | 6 | |
| 6 | 7 | import * as Electron from "electron"; |
| ... | ... | @@ -13,12 +14,36 @@ export interface ElectronAppMetadata { |
| 13 | 14 | readonly runningUnderArm64Translation: boolean; |
| 14 | 15 | } |
| 15 | 16 | |
| 17 | export 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 | |
| 29 | export 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 | |
| 16 | 41 | export class ElectronApp extends Context.Service< |
| 17 | 42 | ElectronApp, |
| 18 | 43 | { |
| 19 | | readonly metadata: Effect.Effect<ElectronAppMetadata>; |
| 44 | readonly metadata: Effect.Effect<ElectronAppMetadata, ElectronAppMetadataReadError>; |
| 20 | 45 | readonly name: Effect.Effect<string>; |
| 21 | | readonly whenReady: Effect.Effect<void>; |
| 46 | readonly whenReady: Effect.Effect<void, ElectronAppWhenReadyError>; |
| 22 | 47 | readonly quit: Effect.Effect<void>; |
| 23 | 48 | readonly exit: (code: number) => Effect.Effect<void>; |
| 24 | 49 | readonly relaunch: (options: Electron.RelaunchOptions) => Effect.Effect<void>; |
| ... | ... | @@ -63,15 +88,40 @@ const addScopedAppListener = <Args extends ReadonlyArray<unknown>>( |
| 63 | 88 | ).pipe(Effect.asVoid); |
| 64 | 89 | |
| 65 | 90 | export const make = ElectronApp.of({ |
| 66 | | metadata: Effect.sync(() => ({ |
| 67 | | appVersion: Electron.app.getVersion(), |
| 68 | | appPath: Electron.app.getAppPath(), |
| 69 | | isPackaged: Electron.app.isPackaged, |
| 70 | | resourcesPath: process.resourcesPath, |
| 71 | | runningUnderArm64Translation: Electron.app.runningUnderARM64Translation === true, |
| 72 | | })), |
| 91 | metadata: Effect.gen(function* () { |
| 92 | const appVersion = yield* Effect.try({ |
| 93 | try: () => Electron.app.getVersion(), |
| 94 | catch: (cause) => |
| 95 | new ElectronAppMetadataReadError({ |
| 96 | property: "app-version", |
| 97 | cause, |
| 98 | }), |
| 99 | }); |
| 100 | const appPath = yield* Effect.try({ |
| 101 | try: () => Electron.app.getAppPath(), |
| 102 | catch: (cause) => |
| 103 | new ElectronAppMetadataReadError({ |
| 104 | property: "app-path", |
| 105 | cause, |
| 106 | }), |
| 107 | }); |
| 108 | |
| 109 | return { |
| 110 | appVersion, |
| 111 | appPath, |
| 112 | isPackaged: Electron.app.isPackaged, |
| 113 | resourcesPath: process.resourcesPath, |
| 114 | runningUnderArm64Translation: Electron.app.runningUnderARM64Translation === true, |
| 115 | }; |
| 116 | }), |
| 73 | 117 | name: Effect.sync(() => Electron.app.name), |
| 74 | | whenReady: Effect.promise(() => Electron.app.whenReady()).pipe(Effect.asVoid), |
| 118 | whenReady: Effect.gen(function* () { |
| 119 | const isPackaged = Electron.app.isPackaged; |
| 120 | yield* Effect.tryPromise({ |
| 121 | try: () => Electron.app.whenReady(), |
| 122 | catch: (cause) => new ElectronAppWhenReadyError({ isPackaged, cause }), |
| 123 | }); |
| 124 | }), |
| 75 | 125 | quit: Effect.sync(() => { |
| 76 | 126 | Electron.app.quit(); |
| 77 | 127 | }), |