/** * The browser half's only data channel. * * The client cannot call the host over RPC (external bundles cannot contribute a * Remote namespace), so it polls `GET /dsh-balance/state` and validates every * response through the shared {@link parseBalanceState}. A malformed or failed * response **keeps the previous snapshot** rather than clearing the UI: a transient * host restart must not make the number vanish. * * Polling is reference-counted off `subscribe`, so the timer only runs while the * pill is actually mounted, and it pauses while the document is hidden. * * Transport, clock and timers are injected so the unit tests run without a DOM. * * @module dsh-balance/client/balance-source */ import type { HostObservable } from '@deepseek-ai/dsh-client-ui-slots'; import { type BalanceState } from '../protocol.js'; /** What the pill renders. */ export interface BalanceSnapshot { /** The newest validated host snapshot; null until the first success. */ readonly state: BalanceState | null; /** `loading` before the first answer, `ready` after a success, `offline` after a failure. */ readonly phase: 'loading' | 'ready' | 'offline'; /** Local time of the most recent attempt. */ readonly attemptedAt: number; /** Local time of the most recent success; null while none. */ readonly updatedAt: number | null; /** Host clock minus local clock, from the last response. */ readonly skewMs: number; /** Consecutive failed attempts. */ readonly failures: number; /** Whether a request is in flight. */ readonly inFlight: boolean; } /** Injectable environment. */ export interface BalanceSourceOptions { readonly fetchImpl?: (input: string, init?: RequestInit) => Promise; readonly now?: () => number; readonly isHidden?: () => boolean; readonly onVisibilityChange?: (listener: () => void) => () => void; readonly setTimer?: (listener: () => void, ms: number) => unknown; readonly clearTimer?: (handle: unknown) => void; /** Base URL for the two routes; defaults to the document origin. */ readonly baseUrl?: () => string; } /** A pollable, disposable snapshot source. */ export interface BalanceSource extends HostObservable { /** * Ask the host to poll the platform now, then re-read the snapshot. * * The two are separate on purpose: the host's own poll cadence stays authoritative, * and this only shortens the wait after a user clicks refresh. */ refresh(): void; /** Release the timer, the listener set and any in-flight request. */ dispose(): void; } /** The {@link BalanceState} in the protocol lives under this shape; re-exported for the component. */ export type { BalanceState }; /** * Build the polling source. * @param options - injected transport, clock, timers and visibility signal. * @returns the source; `subscribe` starts polling and the last unsubscribe stops it. */ export declare function createBalanceSource(options?: BalanceSourceOptions): BalanceSource;