mirror of
https://github.com/stablyai/orca.git
synced 2026-11-17 03:15:01 +00:00
* refactor(mobile): group host catalog credentials descriptors and connection history * refactor(mobile): import RPC sender and result types from defining owners * refactor(mobile): colocate push delivery with registration operations * refactor(mobile): point push recordings at delivery owner
99 lines
3.1 KiB
TypeScript
99 lines
3.1 KiB
TypeScript
import type { ConnectionLogStore } from './connection/connection-log-buffer'
|
|
import type { ConnectionLogEntry } from './connection/connection-log-types'
|
|
import type { HostProfile } from '../host-catalog/host-profile'
|
|
import type { RpcClientContextValue } from '../transport/connection/rpc-client-context-contract'
|
|
|
|
/** Route identity token: compared by reference to detect navigating away and back, never read. */
|
|
export type DiagnosticsRouteKey = Record<string, never>
|
|
|
|
export type DiagnosticsHostSelection = {
|
|
hostId: string
|
|
requestedHostId: string | undefined
|
|
routeKey?: DiagnosticsRouteKey
|
|
}
|
|
|
|
export type DiagnosticsSubmissionState = 'sending' | 'sent' | 'failed'
|
|
export type DiagnosticsSubmissionStates = Readonly<Record<string, DiagnosticsSubmissionState>>
|
|
|
|
export function selectDiagnosticsHostId(
|
|
hosts: readonly HostProfile[],
|
|
requestedHostId: string | undefined,
|
|
previousHostId: string | null
|
|
): string | null {
|
|
if (requestedHostId && hosts.some((host) => host.id === requestedHostId)) {
|
|
return requestedHostId
|
|
}
|
|
if (previousHostId && hosts.some((host) => host.id === previousHostId)) {
|
|
return previousHostId
|
|
}
|
|
return hosts[0]?.id ?? null
|
|
}
|
|
|
|
export function resolveDiagnosticsHostId(
|
|
hosts: readonly HostProfile[],
|
|
requestedHostId: string | undefined,
|
|
manualSelection: DiagnosticsHostSelection | null,
|
|
routeKey?: DiagnosticsRouteKey
|
|
): string | null {
|
|
const selected = manualSelection
|
|
if (selected && selected.requestedHostId === requestedHostId && selected.routeKey === routeKey) {
|
|
const manualHostId = selected.hostId
|
|
if (hosts.some((host) => host.id === manualHostId)) {
|
|
return manualHostId
|
|
}
|
|
}
|
|
return selectDiagnosticsHostId(hosts, requestedHostId, null)
|
|
}
|
|
|
|
export function getDiagnosticsSubmissionState(
|
|
states: DiagnosticsSubmissionStates,
|
|
key: string | null
|
|
): DiagnosticsSubmissionState | 'idle' {
|
|
return key ? (states[key] ?? 'idle') : 'idle'
|
|
}
|
|
|
|
export function updateDiagnosticsSubmissionState(
|
|
states: DiagnosticsSubmissionStates,
|
|
key: string,
|
|
state: DiagnosticsSubmissionState | null
|
|
): DiagnosticsSubmissionStates {
|
|
const next = { ...states }
|
|
if (state) {
|
|
next[key] = state
|
|
} else {
|
|
delete next[key]
|
|
}
|
|
return next
|
|
}
|
|
|
|
export async function readHydratedConnectionLog(
|
|
store: Pick<ConnectionLogStore, 'hydrate' | 'get'>,
|
|
hostId: string
|
|
): Promise<readonly ConnectionLogEntry[]> {
|
|
try {
|
|
await store.hydrate(hostId)
|
|
} catch {
|
|
await store.hydrate(hostId).catch(() => {})
|
|
}
|
|
return store.get(hostId)
|
|
}
|
|
|
|
export async function readConnectionDiagnosticsSnapshot(
|
|
context: Pick<
|
|
RpcClientContextValue,
|
|
'getState' | 'getReconnectAttempt' | 'getLastConnectedAt' | 'getActivePath' | 'getPendingPath'
|
|
>,
|
|
store: Pick<ConnectionLogStore, 'hydrate' | 'get'>,
|
|
hostId: string
|
|
) {
|
|
const entries = await readHydratedConnectionLog(store, hostId)
|
|
return {
|
|
state: context.getState(hostId),
|
|
reconnectAttempts: context.getReconnectAttempt(hostId),
|
|
lastConnectedAt: context.getLastConnectedAt(hostId),
|
|
activePath: context.getActivePath(hostId),
|
|
pendingPath: context.getPendingPath(hostId),
|
|
entries
|
|
}
|
|
}
|