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

DesktopClientSettings.ts

118 lines4.3 KB

1import { ClientSettingsSchema, type ClientSettings } from "@t3tools/contracts";
2import { fromLenientJson } from "@t3tools/shared/schemaJson";
3import * as Context from "effect/Context";
4import * as Data from "effect/Data";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Layer from "effect/Layer";
8import * as Option from "effect/Option";
9import * as Path from "effect/Path";
10import * as PlatformError from "effect/PlatformError";
11import * as Random from "effect/Random";
12import * as Schema from "effect/Schema";
13import * as Ref from "effect/Ref";
14​
15import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
16​
17const ClientSettingsDocumentSchema = Schema.Struct({
18 settings: ClientSettingsSchema,
19});
20​
21const ClientSettingsJson = fromLenientJson(ClientSettingsSchema);
22const LegacyClientSettingsDocumentJson = fromLenientJson(ClientSettingsDocumentSchema);
23const decodeClientSettingsJson = (raw: string): Effect.Effect<ClientSettings, Schema.SchemaError> =>
24 Schema.decodeEffect(LegacyClientSettingsDocumentJson)(raw).pipe(
25 Effect.map((document) => document.settings),
26 Effect.catch(() => Schema.decodeEffect(ClientSettingsJson)(raw)),
27 );
28const encodeClientSettingsJson = Schema.encodeEffect(ClientSettingsJson);
29​
30export class DesktopClientSettingsWriteError extends Data.TaggedError(
31 "DesktopClientSettingsWriteError",
32)<{
33 readonly cause: PlatformError.PlatformError | Schema.SchemaError;
34}> {
35 override get message() {
36 return `Failed to write desktop client settings: ${this.cause.message}`;
37 }
39​
40export interface DesktopClientSettingsShape {
41 readonly get: Effect.Effect<Option.Option<ClientSettings>>;
42 readonly set: (settings: ClientSettings) => Effect.Effect<void, DesktopClientSettingsWriteError>;
44​
45export class DesktopClientSettings extends Context.Service<
46 DesktopClientSettings,
47 DesktopClientSettingsShape
48>()("t3/desktop/ClientSettings") {}
49​
50const readClientSettings = (
51 fileSystem: FileSystem.FileSystem,
52 settingsPath: string,
53): Effect.Effect<Option.Option<ClientSettings>> =>
54 fileSystem.readFileString(settingsPath).pipe(
55 Effect.option,
56 Effect.flatMap(
57 Option.match({
58 onNone: () => Effect.succeed(Option.none<ClientSettings>()),
59 onSome: (raw) =>
60 decodeClientSettingsJson(raw).pipe(
61 Effect.map((settings) => Option.some(settings)),
62 Effect.catch(() => Effect.succeed(Option.none<ClientSettings>())),
63 ),
64 }),
65 ),
66 );
67​
68const writeClientSettings = Effect.fnUntraced(function* (input: {
69 readonly fileSystem: FileSystem.FileSystem;
70 readonly path: Path.Path;
71 readonly settingsPath: string;
72 readonly settings: ClientSettings;
73}): Effect.fn.Return<void, PlatformError.PlatformError | Schema.SchemaError> {
74 const directory = input.path.dirname(input.settingsPath);
75 const suffix = (yield* Random.nextUUIDv4).replace(/-/g, "");
76 const tempPath = `${input.settingsPath}.${process.pid}.${suffix}.tmp`;
77 const encoded = yield* encodeClientSettingsJson(input.settings);
78 yield* input.fileSystem.makeDirectory(directory, { recursive: true });
79 yield* input.fileSystem.writeFileString(tempPath, `${encoded}\n`);
80 yield* input.fileSystem.rename(tempPath, input.settingsPath);
81});
82​
83export const layer = Layer.effect(
84 DesktopClientSettings,
85 Effect.gen(function* () {
86 const environment = yield* DesktopEnvironment.DesktopEnvironment;
87 const fileSystem = yield* FileSystem.FileSystem;
88 const path = yield* Path.Path;
89​
90 return DesktopClientSettings.of({
91 get: readClientSettings(fileSystem, environment.clientSettingsPath).pipe(
92 Effect.withSpan("desktop.clientSettings.get"),
93 ),
94 set: (settings) =>
95 writeClientSettings({
96 fileSystem,
97 path,
98 settingsPath: environment.clientSettingsPath,
99 settings,
100 }).pipe(
101 Effect.mapError((cause) => new DesktopClientSettingsWriteError({ cause })),
102 Effect.withSpan("desktop.clientSettings.set"),
103 ),
104 });
105 }),
106);
107​
108export const layerTest = (initialSettings: Option.Option<ClientSettings> = Option.none()) =>
109 Layer.effect(
110 DesktopClientSettings,
111 Effect.gen(function* () {
112 const settingsRef = yield* Ref.make(initialSettings);
113 return DesktopClientSettings.of({
114 get: Ref.get(settingsRef),
115 set: (settings) => Ref.set(settingsRef, Option.some(settings)),
116 });
117 }),
118 );