sailfork / t3code-github / apps/ desktop/ src/ ipc/ methods/ wsl.ts

import { DesktopWslStateSchema, type DesktopWslState } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";

import * as DesktopLifecycle from "../../app/DesktopLifecycle.ts";
import * as DesktopAppSettings from "../../settings/DesktopAppSettings.ts";
import * as DesktopWslBackend from "../../wsl/DesktopWslBackend.ts";
import * as DesktopWslEnvironment from "../../wsl/DesktopWslEnvironment.ts";
import * as IpcChannels from "../channels.ts";
import { makeIpcMethod } from "../DesktopIpc.ts";

const readWslState: Effect.Effect<
  DesktopWslState,
  never,
  | DesktopAppSettings.DesktopAppSettings
  | DesktopWslEnvironment.DesktopWslEnvironment
  | DesktopWslBackend.DesktopWslBackend
> = Effect.gen(function* () {
  const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
  const wslEnvironment = yield* DesktopWslEnvironment.DesktopWslEnvironment;
  const wslBackend = yield* DesktopWslBackend.DesktopWslBackend;
  const settings = yield* appSettings.get;
  const available = settings.localEnvironmentEnabled && (yield* wslEnvironment.isAvailable);
  // Listing distros on non-WSL hosts spawns wsl.exe only to time out.
  const distros = available ? yield* wslEnvironment.listDistros : [];
  const preflightError = yield* wslBackend.lastPreflightError;
  return {
    enabled: settings.wslBackendEnabled,
    distro: settings.wslDistro,
    available,
    wslOnly: settings.wslOnly,
    distros,
    preflightError: settings.wslOnly ? null : Option.getOrNull(preflightError),
  };
});

export const getWslState = makeIpcMethod({
  channel: IpcChannels.GET_WSL_STATE_CHANNEL,
  payload: Schema.Void,
  result: DesktopWslStateSchema,
  handler: Effect.fn("desktop.ipc.wsl.getState")(function* () {
    return yield* readWslState;
  }),
});

export const setWslBackendEnabled = makeIpcMethod({
  channel: IpcChannels.SET_WSL_BACKEND_ENABLED_CHANNEL,
  payload: Schema.Boolean,
  result: DesktopWslStateSchema,
  handler: Effect.fn("desktop.ipc.wsl.setEnabled")(function* (enabled) {
    const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
    const wslBackend = yield* DesktopWslBackend.DesktopWslBackend;
    const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;
    const previousSettings = yield* appSettings.get;
    const updateSettings = enabled
      ? appSettings.setWslBackendEnabled(true)
      : appSettings.applyWslWindowsFallback;
    const change = yield* updateSettings;
    const settings = yield* appSettings.get;
    const changedWslOnlyPrimary = enabled
      ? settings.wslOnly
      : previousSettings.wslBackendEnabled && previousSettings.wslOnly;
    if (changedWslOnlyPrimary && change.changed) {
      const state = yield* readWslState;
      yield* lifecycle.relaunch(`wslBackendEnabled=${enabled}`);
      return state;
    }
    // Reconcile never fails, so no rollback is needed.
    yield* wslBackend.reconcile;
    return yield* readWslState;
  }),
});

export const setWslDistro = makeIpcMethod({
  channel: IpcChannels.SET_WSL_DISTRO_CHANNEL,
  payload: Schema.NullOr(Schema.String),
  result: DesktopWslStateSchema,
  handler: Effect.fn("desktop.ipc.wsl.setDistro")(function* (distro) {
    const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
    const wslBackend = yield* DesktopWslBackend.DesktopWslBackend;
    const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;
    const change = yield* appSettings.setWslDistro(distro);
    const settings = yield* appSettings.get;
    // WSL-only primary backends capture their distro at startup; relaunch to change it.
    if (settings.wslBackendEnabled && settings.wslOnly && change.changed) {
      const state = yield* readWslState;
      yield* lifecycle.relaunch(`wslDistro=${distro ?? "default"}`);
      return state;
    }
    yield* wslBackend.reconcile;
    return yield* readWslState;
  }),
});

export const setWslOnly = makeIpcMethod({
  channel: IpcChannels.SET_WSL_ONLY_CHANNEL,
  payload: Schema.Boolean,
  result: DesktopWslStateSchema,
  handler: Effect.fn("desktop.ipc.wsl.setOnly")(function* (enabled) {
    // The pool captures its primary at initialization; disabled WSL can stage mode changes until enable.
    const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
    const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;
    const change = yield* appSettings.setWslOnly(enabled);
    const state = yield* readWslState;
    if (state.enabled && change.changed) {
      yield* lifecycle.relaunch(`wslOnly=${enabled}`);
    }
    return state;
  }),
});