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

onboarding.ts

47 lines1.7 KB

1import { ConnectionOnboarding } from "@t3tools/client-runtime/connection";
2import {
3 createAtomCommandScheduler,
4 createRuntimeCommand,
5} from "@t3tools/client-runtime/state/runtime";
6import type { EnvironmentId } from "@t3tools/contracts";
7import * as Effect from "effect/Effect";
8​
9import { connectionAtomRuntime } from "./runtime";
10​
11const onboardingScheduler = createAtomCommandScheduler();
12​
13export const connectPairingUrl = createRuntimeCommand(connectionAtomRuntime, {
14 label: "mobile:connection:connect-pairing-url",
15 scheduler: onboardingScheduler,
16 concurrency: {
17 mode: "singleFlight",
18 // Adding a route to a different machine with the same link is its own
19 // operation: it must check its own expected machine.
20 key: (input: { readonly pairingUrl: string; readonly expectedEnvironmentId?: EnvironmentId }) =>
21 JSON.stringify([input.pairingUrl, input.expectedEnvironmentId ?? null]),
22 },
23 execute: (input: {
24 readonly pairingUrl: string;
25 readonly expectedEnvironmentId?: EnvironmentId;
26 }) =>
27 ConnectionOnboarding.ConnectionOnboarding.pipe(
28 Effect.flatMap((onboarding) => onboarding.registerPairing(input)),
29 ),
30});
31​
32export const updateBearerConnection = createRuntimeCommand(connectionAtomRuntime, {
33 label: "mobile:connection:update-bearer",
34 scheduler: onboardingScheduler,
35 concurrency: {
36 mode: "serial",
37 key: (input: { readonly environmentId: EnvironmentId }) => input.environmentId,
38 },
39 execute: (input: {
40 readonly environmentId: EnvironmentId;
41 readonly label: string;
42 readonly httpBaseUrl: string;
43 }) =>
44 ConnectionOnboarding.ConnectionOnboarding.pipe(
45 Effect.flatMap((onboarding) => onboarding.updateBearer(input)),
46 ),
47});