| ... | @@ -1,6 +1,7 @@ | ... | @@ -1,6 +1,7 @@ |
| 1 | import * as Context from "effect/Context"; | 1 | import * as Context from "effect/Context"; |
| 2 | import * as Effect from "effect/Effect"; | 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; | 3 | import * as Layer from "effect/Layer"; |
| | 4 | import * as Schema from "effect/Schema"; |
| 4 | import * as Scope from "effect/Scope"; | 5 | import * as Scope from "effect/Scope"; |
| 5 | | 6 | |
| 6 | import * as Electron from "electron"; | 7 | import * as Electron from "electron"; |
| ... | @@ -13,12 +14,36 @@ export interface ElectronAppMetadata { | ... | @@ -13,12 +14,36 @@ export interface ElectronAppMetadata { |
| 13 | readonly runningUnderArm64Translation: boolean; | 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 | export class ElectronApp extends Context.Service< | 41 | export class ElectronApp extends Context.Service< |
| 17 | ElectronApp, | 42 | ElectronApp, |
| 18 | { | 43 | { |
| 19 | readonly metadata: Effect.Effect<ElectronAppMetadata>; | 44 | readonly metadata: Effect.Effect<ElectronAppMetadata, ElectronAppMetadataReadError>; |
| 20 | readonly name: Effect.Effect<string>; | 45 | readonly name: Effect.Effect<string>; |
| 21 | readonly whenReady: Effect.Effect<void>; | 46 | readonly whenReady: Effect.Effect<void, ElectronAppWhenReadyError>; |
| 22 | readonly quit: Effect.Effect<void>; | 47 | readonly quit: Effect.Effect<void>; |
| 23 | readonly exit: (code: number) => Effect.Effect<void>; | 48 | readonly exit: (code: number) => Effect.Effect<void>; |
| 24 | readonly relaunch: (options: Electron.RelaunchOptions) => Effect.Effect<void>; | 49 | readonly relaunch: (options: Electron.RelaunchOptions) => Effect.Effect<void>; |
| ... | @@ -63,15 +88,40 @@ const addScopedAppListener = <Args extends ReadonlyArray<unknown>>( | ... | @@ -63,15 +88,40 @@ const addScopedAppListener = <Args extends ReadonlyArray<unknown>>( |
| 63 | ).pipe(Effect.asVoid); | 88 | ).pipe(Effect.asVoid); |
| 64 | | 89 | |
| 65 | export const make = ElectronApp.of({ | 90 | export const make = ElectronApp.of({ |
| 66 | metadata: Effect.sync(() => ({ | 91 | metadata: Effect.gen(function* () { |
| 67 | appVersion: Electron.app.getVersion(), | 92 | const appVersion = yield* Effect.try({ |
| 68 | appPath: Electron.app.getAppPath(), | 93 | try: () => Electron.app.getVersion(), |
| 69 | isPackaged: Electron.app.isPackaged, | 94 | catch: (cause) => |
| 70 | resourcesPath: process.resourcesPath, | 95 | new ElectronAppMetadataReadError({ |
| 71 | runningUnderArm64Translation: Electron.app.runningUnderARM64Translation === true, | 96 | property: "app-version", |
| 72 | })), | 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 | name: Effect.sync(() => Electron.app.name), | 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 | quit: Effect.sync(() => { | 125 | quit: Effect.sync(() => { |
| 76 | Electron.app.quit(); | 126 | Electron.app.quit(); |
| 77 | }), | 127 | }), |