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

localEnvironment.ts

47 lines2.1 KB

1import * as Effect from "effect/Effect";
2import * as FileSystem from "effect/FileSystem";
3import * as Schema from "effect/Schema";
4​
5import * as DesktopEnvironment from "../../app/DesktopEnvironment.ts";
6import * as DesktopLifecycle from "../../app/DesktopLifecycle.ts";
7import * as DesktopAppSettings from "../../settings/DesktopAppSettings.ts";
8import * as IpcChannels from "../channels.ts";
9import { makeIpcMethod, makeSyncIpcMethod } from "../DesktopIpc.ts";
10​
11export const getLocalEnvironmentEnabled = makeSyncIpcMethod({
12 channel: IpcChannels.GET_LOCAL_ENVIRONMENT_ENABLED_CHANNEL,
13 result: Schema.Boolean,
14 handler: Effect.fn("desktop.ipc.localEnvironment.getEnabled")(function* () {
15 const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
16 return (yield* appSettings.get).localEnvironmentEnabled;
17 }),
18});
19​
20export const setLocalEnvironmentEnabled = makeIpcMethod({
21 channel: IpcChannels.SET_LOCAL_ENVIRONMENT_ENABLED_CHANNEL,
22 payload: Schema.Boolean,
23 result: Schema.Void,
24 handler: Effect.fn("desktop.ipc.localEnvironment.setEnabled")(function* (enabled) {
25 const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
26 const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;
27 const change = yield* appSettings.setLocalEnvironmentEnabled(enabled);
28 if (change.changed) {
29 yield* lifecycle.relaunch(`localEnvironmentEnabled=${enabled}`);
30 }
31 }),
32});
33​
34// A background service sharing this app's T3 home records its environment ID in the same state dir.
35export const getHostEnvironmentId = makeIpcMethod({
36 channel: IpcChannels.GET_HOST_ENVIRONMENT_ID_CHANNEL,
37 payload: Schema.Void,
38 result: Schema.NullOr(Schema.String),
39 handler: Effect.fn("desktop.ipc.localEnvironment.getHostEnvironmentId")(function* () {
40 const environment = yield* DesktopEnvironment.DesktopEnvironment;
41 const fileSystem = yield* FileSystem.FileSystem;
42 const contents = yield* fileSystem
43 .readFileString(environment.path.join(environment.stateDir, "environment-id"))
44 .pipe(Effect.orElseSucceed(() => ""));
45 return contents.trim() || null;
46 }),
47});