64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import {
|
|
createStateSnapshot,
|
|
normalizeStoredState,
|
|
type GatewayAutoState,
|
|
type OperationState,
|
|
type StateSnapshot,
|
|
type StoredState,
|
|
} from '../../../shared/contracts/state.js';
|
|
import type { FailoverSnapshot } from '../../../shared/failover.js';
|
|
|
|
interface RuntimeState {
|
|
running?: boolean;
|
|
startedAt?: string | null;
|
|
}
|
|
|
|
export interface StateReadResult {
|
|
snapshot: StateSnapshot;
|
|
storedState: StoredState;
|
|
gatewayAuto: GatewayAutoState;
|
|
configExists: boolean;
|
|
}
|
|
|
|
interface StateServiceDependencies {
|
|
appMode: string;
|
|
readStoredState: () => unknown;
|
|
refreshRuntime: () => Promise<RuntimeState>;
|
|
getGatewayAutoState: () => GatewayAutoState;
|
|
getOperationState: () => OperationState;
|
|
configExists: () => boolean;
|
|
getFailoverSnapshot?: () => FailoverSnapshot;
|
|
}
|
|
|
|
function subscriptionHost(value: unknown) {
|
|
try {
|
|
return `${new URL(String(value)).host}/…`;
|
|
} catch {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
export function createStateService(dependencies: StateServiceDependencies) {
|
|
return {
|
|
async read(): Promise<StateReadResult> {
|
|
const runtime = await dependencies.refreshRuntime();
|
|
const storedState = normalizeStoredState(dependencies.readStoredState());
|
|
const gatewayAuto = dependencies.getGatewayAutoState();
|
|
const configExists = dependencies.configExists();
|
|
const snapshot = createStateSnapshot({
|
|
storedState,
|
|
runtime,
|
|
gatewayAuto,
|
|
appMode: dependencies.appMode,
|
|
configExists,
|
|
subscriptionHost: subscriptionHost(storedState.subscriptionUrl),
|
|
operation: dependencies.getOperationState(),
|
|
failoverSnapshot: dependencies.getFailoverSnapshot?.(),
|
|
});
|
|
return { snapshot, storedState, gatewayAuto, configExists };
|
|
},
|
|
};
|
|
}
|
|
|
|
export type StateService = ReturnType<typeof createStateService>;
|