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

asset-url-state.ts

42 lines1.6 KB

1import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
2import type { AssetUrlState as SharedAssetUrlState } from "@t3tools/client-runtime/state/assets";
3​
4export type AssetUrlFailureReason = "disconnected" | "failed" | "missing-file-access";
5​
6export type AssetUrlState =
7 | { readonly _tag: "Loading" }
8 | { readonly _tag: "Failure"; readonly reason: AssetUrlFailureReason }
9 | Extract<SharedAssetUrlState, { readonly _tag: "Success" }>;
10​
11export function deriveAssetUrlState(input: {
12 readonly connectionPhase: EnvironmentConnectionPhase;
13 readonly shared: SharedAssetUrlState;
14}): AssetUrlState {
15 if (
16 input.connectionPhase === "offline" ||
17 input.connectionPhase === "reconnecting" ||
18 input.connectionPhase === "error" ||
19 input.connectionPhase === "unsupported"
20 ) {
21 return { _tag: "Failure", reason: "disconnected" };
22 }
23 if (input.shared._tag === "Success") {
24 return input.shared;
25 }
26 switch (input.connectionPhase) {
27 // "available" is the idle, not yet dialled state. A pending query there is
28 // still on its way, but the query atom fails at once while idle, so a
29 // failure means the environment is not connected rather than the file is
30 // missing.
31 case "available":
32 return input.shared._tag === "Failure"
33 ? { _tag: "Failure", reason: "disconnected" }
34 : { _tag: "Loading" };
35 case "connecting":
36 return { _tag: "Loading" };
37 case "connected":
38 return input.shared._tag === "Failure"
39 ? { _tag: "Failure", reason: input.shared.reason ?? "failed" }
40 : { _tag: "Loading" };
41 }