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

wsl.ts

117 lines4.9 KB

1import { DesktopWslStateSchema, type DesktopWslState } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import * as Option from "effect/Option";
4import * as Schema from "effect/Schema";
5​
6import * as DesktopLifecycle from "../../app/DesktopLifecycle.ts";
7import * as DesktopAppSettings from "../../settings/DesktopAppSettings.ts";
8import * as DesktopWslBackend from "../../wsl/DesktopWslBackend.ts";
9import * as DesktopWslEnvironment from "../../wsl/DesktopWslEnvironment.ts";
10import * as IpcChannels from "../channels.ts";
11import { makeIpcMethod } from "../DesktopIpc.ts";
12​
13const readWslState: Effect.Effect<
14 DesktopWslState,
15 never,
16 | DesktopAppSettings.DesktopAppSettings
17 | DesktopWslEnvironment.DesktopWslEnvironment
18 | DesktopWslBackend.DesktopWslBackend
19> = Effect.gen(function* () {
20 const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
21 const wslEnvironment = yield* DesktopWslEnvironment.DesktopWslEnvironment;
22 const wslBackend = yield* DesktopWslBackend.DesktopWslBackend;
23 const settings = yield* appSettings.get;
24 const available = settings.localEnvironmentEnabled && (yield* wslEnvironment.isAvailable);
25 // Windows ships a wsl.exe stub that can prompt to install WSL when invoked.
26 // Keep disabled state reads from launching it to enumerate distros.
27 const distros = available && settings.wslBackendEnabled ? yield* wslEnvironment.listDistros : [];
28 const preflightError = yield* wslBackend.lastPreflightError;
29 return {
30 enabled: settings.wslBackendEnabled,
31 distro: settings.wslDistro,
32 available,
33 wslOnly: settings.wslOnly,
34 distros,
35 preflightError: settings.wslOnly ? null : Option.getOrNull(preflightError),
36 };
37});
38​
39export const getWslState = makeIpcMethod({
40 channel: IpcChannels.GET_WSL_STATE_CHANNEL,
41 payload: Schema.Void,
42 result: DesktopWslStateSchema,
43 handler: Effect.fn("desktop.ipc.wsl.getState")(function* () {
44 return yield* readWslState;
45 }),
46});
47​
48export const setWslBackendEnabled = makeIpcMethod({
49 channel: IpcChannels.SET_WSL_BACKEND_ENABLED_CHANNEL,
50 payload: Schema.Boolean,
51 result: DesktopWslStateSchema,
52 handler: Effect.fn("desktop.ipc.wsl.setEnabled")(function* (enabled) {
53 const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
54 const wslBackend = yield* DesktopWslBackend.DesktopWslBackend;
55 const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;
56 const previousSettings = yield* appSettings.get;
57 const updateSettings = enabled
58 ? appSettings.setWslBackendEnabled(true)
59 : appSettings.applyWslWindowsFallback;
60 const change = yield* updateSettings;
61 const settings = yield* appSettings.get;
62 const changedWslOnlyPrimary = enabled
63 ? settings.wslOnly
64 : previousSettings.wslBackendEnabled && previousSettings.wslOnly;
65 if (changedWslOnlyPrimary && change.changed) {
66 const state = yield* readWslState;
67 yield* lifecycle.relaunch(`wslBackendEnabled=${enabled}`);
68 return state;
69 }
70 // Reconcile is idempotent and never fails; no need for a swap-style
71 // rollback when the WSL side has trouble coming up.
72 yield* wslBackend.reconcile;
73 return yield* readWslState;
74 }),
75});
76​
77export const setWslDistro = makeIpcMethod({
78 channel: IpcChannels.SET_WSL_DISTRO_CHANNEL,
79 payload: Schema.NullOr(Schema.String),
80 result: DesktopWslStateSchema,
81 handler: Effect.fn("desktop.ipc.wsl.setDistro")(function* (distro) {
82 const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
83 const wslBackend = yield* DesktopWslBackend.DesktopWslBackend;
84 const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;
85 const change = yield* appSettings.setWslDistro(distro);
86 const settings = yield* appSettings.get;
87 // In active wsl-only mode the pool's primary IS the WSL backend, and its
88 // distro is captured when that backend starts, so relaunch to replace it.
89 if (settings.wslBackendEnabled && settings.wslOnly && change.changed) {
90 const state = yield* readWslState;
91 yield* lifecycle.relaunch(`wslDistro=${distro ?? "default"}`);
92 return state;
93 }
94 yield* wslBackend.reconcile;
95 return yield* readWslState;
96 }),
97});
98​
99export const setWslOnly = makeIpcMethod({
100 channel: IpcChannels.SET_WSL_ONLY_CHANNEL,
101 payload: Schema.Boolean,
102 result: DesktopWslStateSchema,
103 handler: Effect.fn("desktop.ipc.wsl.setOnly")(function* (enabled) {
104 // wsl-only decides which backend the pool spins up as "primary", and that
105 // decision is captured once at layer init. A disabled WSL backend always
106 // leaves Windows primary active, so mode changes can be staged without a
107 // relaunch and applied by the subsequent enable call.
108 const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
109 const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;
110 const change = yield* appSettings.setWslOnly(enabled);
111 const state = yield* readWslState;
112 if (state.enabled && change.changed) {
113 yield* lifecycle.relaunch(`wslOnly=${enabled}`);
114 }
115 return state;
116 }),
117});