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

DesktopClientSettings.ts

128 lines4.7 KB

1import { ClientSettingsSchema, type ClientSettings } from "@t3tools/contracts";
2import { fromLenientJson } from "@t3tools/shared/schemaJson";
3import * as Context from "effect/Context";
4import * as Crypto from "effect/Crypto";
5import * as Data from "effect/Data";
6import * as Effect from "effect/Effect";
7import * as FileSystem from "effect/FileSystem";
8import * as Layer from "effect/Layer";
9import * as Option from "effect/Option";
10import * as Path from "effect/Path";
11import * as PlatformError from "effect/PlatformError";
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>()("@t3tools/desktop/settings/DesktopClientSettings") {}
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.orElseSucceed(() => 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 readonly suffix: string;
78}): Effect.fn.Return<void, PlatformError.PlatformError | Schema.SchemaError> {
79 const directory = input.path.dirname(input.settingsPath);
80 const tempPath = `${input.settingsPath}.${process.pid}.${input.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 const crypto = yield* Crypto.Crypto;
94​
95 return DesktopClientSettings.of({
96 get: readClientSettings(fileSystem, environment.clientSettingsPath).pipe(
97 Effect.withSpan("desktop.clientSettings.get"),
98 ),
99 set: (settings) =>
100 crypto.randomUUIDv4.pipe(
101 Effect.map((uuid) => uuid.replace(/-/g, "")),
102 Effect.flatMap((suffix) =>
103 writeClientSettings({
104 fileSystem,
105 path,
106 settingsPath: environment.clientSettingsPath,
107 settings,
108 suffix,
109 }),
110 ),
111 Effect.mapError((cause) => new DesktopClientSettingsWriteError({ cause })),
112 Effect.withSpan("desktop.clientSettings.set"),
113 ),
114 });
115 }),
116);
117​
118export const layerTest = (initialSettings: Option.Option<ClientSettings> = Option.none()) =>
119 Layer.effect(
120 DesktopClientSettings,
121 Effect.gen(function* () {
122 const settingsRef = yield* Ref.make(initialSettings);
123 return DesktopClientSettings.of({
124 get: Ref.get(settingsRef),
125 set: (settings) => Ref.set(settingsRef, Option.some(settings)),
126 });
127 }),
128 );