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

relayClientInstallDialog.ts

100 lines2.8 KB

1import type {
2 RelayClientInstallProgressEvent,
3 RelayClientInstallProgressStage,
4} from "@t3tools/contracts";
5​
6export type RelayClientInstallDialogState =
7 | { readonly status: "idle" }
8 | { readonly status: "confirming"; readonly version: string }
9 | {
10 readonly status: "installing";
11 readonly version: string;
12 readonly stage: RelayClientInstallProgressStage;
13 }
14 | {
15 readonly status: "closing";
16 readonly view:
17 | { readonly status: "confirming"; readonly version: string }
18 | {
19 readonly status: "installing";
20 readonly version: string;
21 readonly stage: RelayClientInstallProgressStage;
22 };
23 };
24​
25const idleState: RelayClientInstallDialogState = { status: "idle" };
26let state: RelayClientInstallDialogState = idleState;
27let resolveConfirmation: ((confirmed: boolean) => void) | null = null;
28const listeners = new Set<() => void>();
29​
30function publish(next: RelayClientInstallDialogState) {
31 state = next;
32 for (const listener of listeners) {
33 listener();
34 }
36​
37export function readRelayClientInstallDialogState(): RelayClientInstallDialogState {
38 return state;
40​
41export function subscribeRelayClientInstallDialog(listener: () => void): () => void {
42 listeners.add(listener);
43 return () => {
44 listeners.delete(listener);
45 };
47​
48export function requestRelayClientInstallConfirmation(version: string): Promise<boolean> {
49 if (state.status !== "idle") {
50 return Promise.reject(new Error("A relay client installation is already in progress."));
51 }
52​
53 publish({ status: "confirming", version });
54 return new Promise<boolean>((resolve) => {
55 resolveConfirmation = resolve;
56 });
58​
59export function respondToRelayClientInstallConfirmation(confirmed: boolean): void {
60 if (state.status !== "confirming" || !resolveConfirmation) {
61 return;
62 }
63​
64 const resolve = resolveConfirmation;
65 resolveConfirmation = null;
66 publish(
67 confirmed
68 ? { status: "installing", version: state.version, stage: "checking" }
69 : { status: "closing", view: state },
70 );
71 resolve(confirmed);
73​
74export function reportRelayClientInstallProgress(event: RelayClientInstallProgressEvent): void {
75 if (state.status !== "installing" || event.type !== "progress") {
76 return;
77 }
78 publish({ ...state, stage: event.stage });
80​
81export function finishRelayClientInstall(): void {
82 resolveConfirmation?.(false);
83 resolveConfirmation = null;
84 if (state.status === "confirming" || state.status === "installing") {
85 publish({ status: "closing", view: state });
86 }
88​
89export function completeRelayClientInstallDialogClose(): void {
90 if (state.status === "closing") {
91 publish(idleState);
92 }
94​
95export function resetRelayClientInstallDialogForTests(): void {
96 resolveConfirmation?.(false);
97 resolveConfirmation = null;
98 publish(idleState);
99 listeners.clear();