| 1 | import { ConnectionOnboarding } from "@t3tools/client-runtime/connection"; |
| 2 | import { |
| 3 | createAtomCommandScheduler, |
| 4 | createRuntimeCommand, |
| 5 | } from "@t3tools/client-runtime/state/runtime"; |
| 6 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | |
| 9 | import { connectionAtomRuntime } from "./runtime"; |
| 10 | |
| 11 | const onboardingScheduler = createAtomCommandScheduler(); |
| 12 | |
| 13 | export 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 | /** Set when adding a route to this saved machine. */ |
| 26 | readonly expectedEnvironmentId?: EnvironmentId; |
| 27 | }) => |
| 28 | ConnectionOnboarding.ConnectionOnboarding.pipe( |
| 29 | Effect.flatMap((onboarding) => onboarding.registerPairing(input)), |
| 30 | ), |
| 31 | }); |
| 32 | |
| 33 | export const updateBearerConnection = createRuntimeCommand(connectionAtomRuntime, { |
| 34 | label: "mobile:connection:update-bearer", |
| 35 | scheduler: onboardingScheduler, |
| 36 | concurrency: { |
| 37 | mode: "serial", |
| 38 | key: (input: { readonly environmentId: EnvironmentId }) => input.environmentId, |
| 39 | }, |
| 40 | execute: (input: { |
| 41 | readonly environmentId: EnvironmentId; |
| 42 | readonly label: string; |
| 43 | readonly httpBaseUrl: string; |
| 44 | }) => |
| 45 | ConnectionOnboarding.ConnectionOnboarding.pipe( |
| 46 | Effect.flatMap((onboarding) => onboarding.updateBearer(input)), |
| 47 | ), |
| 48 | }); |