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

DesktopClientSettings.ts

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