1import { Connection } from "@t3tools/client-runtime/connection";
2import { ShellSnapshotLoader } from "@t3tools/client-runtime/state/shell";
3import { PullRequestDiffLoader } from "@t3tools/client-runtime/state/pull-requests";
4import {
5 BoundedThreadSnapshotLoader,
6 ThreadHistoryController,
7} from "@t3tools/client-runtime/state/threads";
8import * as Layer from "effect/Layer";
9import { Atom } from "effect/reactivity";
10
11import type { FoundationHotModule } from "../lib/foundation-fast-refresh";
12import { hotSwappableAtomRuntime } from "../lib/hot-swappable-atom-runtime";
13import * as Runtime from "../lib/runtime";
14import { appAtomRegistry } from "../state/atom-registry";
15import * as BackgroundActivity from "./background-activity";
16import * as ConnectionPlatform from "./platform";
17
18declare const module: { readonly hot?: FoundationHotModule } | undefined;
19
20const layerProvidedConnectionPlatform = ConnectionPlatform.layer.pipe(Layer.provide(Runtime.layer));
21
22const layerSnapshotLoader = Layer.mergeAll(
23 PullRequestDiffLoader.layer,
24 BoundedThreadSnapshotLoader.layer,
25 ShellSnapshotLoader.layer,
26 ThreadHistoryController.layer,
27);
28
29type ConnectionLayerSource =
30 | typeof Connection.layer
31 | typeof layerSnapshotLoader
32 | typeof Runtime.layer
33 | typeof ConnectionPlatform.layer
34 | typeof BackgroundActivity.layerObserver
35 | typeof BackgroundActivity.layerReporter;
36
37const layerProvidedClientConnection = layerSnapshotLoader.pipe(
38 Layer.provideMerge(
39 Connection.layerWithOptions({ usageLimitSources: true, usageLimitsCommand: true }),
40 ),
41 Layer.provideMerge(
42 Layer.mergeAll(
43 Runtime.layer,
44 layerProvidedConnectionPlatform,
45 BackgroundActivity.layerObserver,
46 ),
47 ),
48);
49
50const layerConnection = BackgroundActivity.layerReporter.pipe(
51 Layer.provideMerge(layerProvidedClientConnection),
52);
53
54export const connectionAtomRuntime: Atom.AtomRuntime<
55 Layer.Success<ConnectionLayerSource>,
56 Layer.Error<ConnectionLayerSource>
57> = hotSwappableAtomRuntime({
58 id: "t3.mobile.connection-runtime",
59 hotModule: typeof module === "undefined" ? undefined : module.hot,
60 registry: appAtomRegistry,
61 layer: layerConnection,
62});