refactor: format markdown files using prettier, fix spelling concerns, reduce Taskfile bloat
This commit is contained in:
608 files changed
+63317
-10219
No files matched your search
@@ -12,7 +12,11 @@ export type Motion = "system" | "reduced";
|
||||
|
||||
type ResolvedTheme = Exclude<Theme, "system">;
|
||||
|
||||
function storedChoice<T extends string>(key: string, choices: readonly T[], fallback: T): T {
|
||||
function storedChoice<T extends string>(
|
||||
key: string,
|
||||
choices: readonly T[],
|
||||
fallback: T,
|
||||
): T {
|
||||
try {
|
||||
const value = localStorage.getItem(key);
|
||||
return choices.includes(value as T) ? (value as T) : fallback;
|
||||
@@ -32,17 +36,27 @@ function persist(key: string, value: string): void {
|
||||
function resolveTheme(preference: Theme): ResolvedTheme {
|
||||
if (preference !== "system") return preference;
|
||||
try {
|
||||
return matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
||||
return matchMedia("(prefers-color-scheme: dark)").matches
|
||||
? "dark"
|
||||
: "light";
|
||||
} catch {
|
||||
return "dark";
|
||||
}
|
||||
}
|
||||
|
||||
export const useAppearanceStore = defineStore("appearance", () => {
|
||||
const theme = ref<Theme>(storedChoice(THEME_KEY, ["system", "dark", "light"], "dark"));
|
||||
const density = ref<Density>(storedChoice(DENSITY_KEY, ["comfortable", "compact"], "comfortable"));
|
||||
const motion = ref<Motion>(storedChoice(MOTION_KEY, ["system", "reduced"], "system"));
|
||||
const tooltipsEnabled = ref(storedChoice(TOOLTIPS_KEY, ["on", "off"], "on") === "on");
|
||||
const theme = ref<Theme>(
|
||||
storedChoice(THEME_KEY, ["system", "dark", "light"], "dark"),
|
||||
);
|
||||
const density = ref<Density>(
|
||||
storedChoice(DENSITY_KEY, ["comfortable", "compact"], "comfortable"),
|
||||
);
|
||||
const motion = ref<Motion>(
|
||||
storedChoice(MOTION_KEY, ["system", "reduced"], "system"),
|
||||
);
|
||||
const tooltipsEnabled = ref(
|
||||
storedChoice(TOOLTIPS_KEY, ["on", "off"], "on") === "on",
|
||||
);
|
||||
let initialized = false;
|
||||
|
||||
function applyTheme(): void {
|
||||
@@ -79,9 +93,12 @@ export const useAppearanceStore = defineStore("appearance", () => {
|
||||
document.documentElement.dataset.density = density.value;
|
||||
document.documentElement.dataset.motion = motion.value;
|
||||
try {
|
||||
matchMedia("(prefers-color-scheme: dark)").addEventListener("change", () => {
|
||||
if (theme.value === "system") applyTheme();
|
||||
});
|
||||
matchMedia("(prefers-color-scheme: dark)").addEventListener(
|
||||
"change",
|
||||
() => {
|
||||
if (theme.value === "system") applyTheme();
|
||||
},
|
||||
);
|
||||
} catch {
|
||||
// matchMedia is absent in older embedded views. The dark fallback stands.
|
||||
}
|
||||
|
||||
+152
-43
@@ -20,7 +20,9 @@ export interface ControlPlaneResource {
|
||||
updated_at?: string;
|
||||
}
|
||||
|
||||
interface ResourceResponse { resource: ControlPlaneResource }
|
||||
interface ResourceResponse {
|
||||
resource: ControlPlaneResource;
|
||||
}
|
||||
|
||||
/** One process's report about one resource kind, as the server derives it.
|
||||
* `state` is liveness only: the server knows whether a record went stale, not
|
||||
@@ -40,7 +42,8 @@ export interface ApplyStatusResponse {
|
||||
}
|
||||
|
||||
/** What the settings page says about one process for one resource. */
|
||||
export type ApplyState = "running" | "pending-restart" | "failed" | "unknown" | "not-reporting";
|
||||
export type ApplyState =
|
||||
"running" | "pending-restart" | "failed" | "unknown" | "not-reporting";
|
||||
|
||||
export interface ApplyStatusRow {
|
||||
process: string;
|
||||
@@ -61,12 +64,26 @@ export function applyStatusForKind(
|
||||
storedVersion: number,
|
||||
): ApplyStatusRow[] {
|
||||
return processes.map((process) => {
|
||||
const record = records.find((entry) => entry.process === process && entry.kind === kind);
|
||||
if (!record) return { process, state: "not-reporting" as const, version: 0, error: "" };
|
||||
const row = { process, version: record.applied_version, error: record.error ?? "" };
|
||||
if (record.state === "unknown") return { ...row, state: "unknown" as const };
|
||||
const record = records.find(
|
||||
(entry) => entry.process === process && entry.kind === kind,
|
||||
);
|
||||
if (!record)
|
||||
return {
|
||||
process,
|
||||
state: "not-reporting" as const,
|
||||
version: 0,
|
||||
error: "",
|
||||
};
|
||||
const row = {
|
||||
process,
|
||||
version: record.applied_version,
|
||||
error: record.error ?? "",
|
||||
};
|
||||
if (record.state === "unknown")
|
||||
return { ...row, state: "unknown" as const };
|
||||
if (record.state === "failed") return { ...row, state: "failed" as const };
|
||||
if (record.applied_version < storedVersion) return { ...row, state: "pending-restart" as const };
|
||||
if (record.applied_version < storedVersion)
|
||||
return { ...row, state: "pending-restart" as const };
|
||||
return { ...row, state: "running" as const };
|
||||
});
|
||||
}
|
||||
@@ -82,7 +99,9 @@ export interface ResourceRevision {
|
||||
at?: string;
|
||||
}
|
||||
|
||||
interface HistoryResponse { revisions?: ResourceRevision[] }
|
||||
interface HistoryResponse {
|
||||
revisions?: ResourceRevision[];
|
||||
}
|
||||
|
||||
export interface ResourceState {
|
||||
resource?: ControlPlaneResource;
|
||||
@@ -109,43 +128,83 @@ async function request<T>(path: string, init: RequestInit = {}): Promise<T> {
|
||||
headers.set("Content-Type", "application/json");
|
||||
headers.set("X-Archie-CSRF", "1");
|
||||
}
|
||||
const response = await fetch(path, { ...init, headers, signal: AbortSignal.timeout(15_000) });
|
||||
if (!response.ok) throw new ControlPlaneError((await response.text()).trim() || response.statusText, response.status);
|
||||
const response = await fetch(path, {
|
||||
...init,
|
||||
headers,
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
});
|
||||
if (!response.ok)
|
||||
throw new ControlPlaneError(
|
||||
(await response.text()).trim() || response.statusText,
|
||||
response.status,
|
||||
);
|
||||
return response.json() as Promise<T>;
|
||||
}
|
||||
|
||||
export type ControlPlanePage = "tasks" | "models" | "repositories" | "channels" | "advanced" | "workflows";
|
||||
export type ControlPlanePage =
|
||||
"tasks" | "models" | "repositories" | "channels" | "advanced" | "workflows";
|
||||
|
||||
const PAGE_RESOURCES: Record<ControlPlanePage, string[]> = {
|
||||
tasks: ["workflow-execution-settings"],
|
||||
models: ["provider-settings", "model-role-assignments"],
|
||||
repositories: ["repository-policies"],
|
||||
channels: ["channel-settings"],
|
||||
advanced: ["personas", "schedules", "scheduling-policy", "tool-settings", "plugin-settings", "container-runtime-policies"],
|
||||
advanced: [
|
||||
"personas",
|
||||
"schedules",
|
||||
"scheduling-policy",
|
||||
"tool-settings",
|
||||
"plugin-settings",
|
||||
"container-runtime-policies",
|
||||
],
|
||||
workflows: ["workflow-definitions"],
|
||||
};
|
||||
|
||||
export interface WorkflowDefinitionEntry { id: string; yaml: string }
|
||||
export interface WorkflowDefinitionCollection { definitions: WorkflowDefinitionEntry[] }
|
||||
export interface WorkflowDefinitionEntry {
|
||||
id: string;
|
||||
yaml: string;
|
||||
}
|
||||
export interface WorkflowDefinitionCollection {
|
||||
definitions: WorkflowDefinitionEntry[];
|
||||
}
|
||||
|
||||
export function cloneControlPlaneValue<T>(value: T): T {
|
||||
return JSON.parse(JSON.stringify(value)) as T;
|
||||
}
|
||||
|
||||
export function resourcesForPage(catalog: ResourceDescriptor[], page: ControlPlanePage): ResourceDescriptor[] {
|
||||
export function resourcesForPage(
|
||||
catalog: ResourceDescriptor[],
|
||||
page: ControlPlanePage,
|
||||
): ResourceDescriptor[] {
|
||||
const ownership = PAGE_RESOURCES[page];
|
||||
return ownership.flatMap((kind) => catalog.filter((descriptor) => descriptor.kind === kind));
|
||||
return ownership.flatMap((kind) =>
|
||||
catalog.filter((descriptor) => descriptor.kind === kind),
|
||||
);
|
||||
}
|
||||
|
||||
export function upsertWorkflowDefinition(collection: WorkflowDefinitionCollection, entry: WorkflowDefinitionEntry): WorkflowDefinitionCollection {
|
||||
export function upsertWorkflowDefinition(
|
||||
collection: WorkflowDefinitionCollection,
|
||||
entry: WorkflowDefinitionEntry,
|
||||
): WorkflowDefinitionCollection {
|
||||
const found = collection.definitions.some(({ id }) => id === entry.id);
|
||||
return { definitions: found
|
||||
? collection.definitions.map((definition) => definition.id === entry.id ? entry : definition)
|
||||
: [...collection.definitions, entry] };
|
||||
return {
|
||||
definitions: found
|
||||
? collection.definitions.map((definition) =>
|
||||
definition.id === entry.id ? entry : definition,
|
||||
)
|
||||
: [...collection.definitions, entry],
|
||||
};
|
||||
}
|
||||
|
||||
export function removeWorkflowDefinition(collection: WorkflowDefinitionCollection, id: string): WorkflowDefinitionCollection {
|
||||
return { definitions: collection.definitions.filter((definition) => definition.id !== id) };
|
||||
export function removeWorkflowDefinition(
|
||||
collection: WorkflowDefinitionCollection,
|
||||
id: string,
|
||||
): WorkflowDefinitionCollection {
|
||||
return {
|
||||
definitions: collection.definitions.filter(
|
||||
(definition) => definition.id !== id,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
export function commandBody(value: unknown, expectedVersion: number): string {
|
||||
@@ -159,18 +218,30 @@ export const useControlPlaneStore = defineStore("control-plane", () => {
|
||||
const streams = new Map<string, EventSource>();
|
||||
const applyStatus = ref<ApplyStatusResponse>({ records: [], processes: [] });
|
||||
|
||||
const genericResources = computed(() => catalog.value.filter((item) =>
|
||||
item.apply_mode !== "domain-managed" && !item.query_service && item.commands.includes("replace"),
|
||||
));
|
||||
const genericResources = computed(() =>
|
||||
catalog.value.filter(
|
||||
(item) =>
|
||||
item.apply_mode !== "domain-managed" &&
|
||||
!item.query_service &&
|
||||
item.commands.includes("replace"),
|
||||
),
|
||||
);
|
||||
|
||||
function stateFor(kind: string): ResourceState {
|
||||
if (!states[kind]) states[kind] = { loading: false, saving: false, conflict: false, stream: "connecting" };
|
||||
if (!states[kind])
|
||||
states[kind] = {
|
||||
loading: false,
|
||||
saving: false,
|
||||
conflict: false,
|
||||
stream: "connecting",
|
||||
};
|
||||
return states[kind];
|
||||
}
|
||||
|
||||
function apply(resource: ControlPlaneResource): void {
|
||||
const state = stateFor(resource.kind);
|
||||
if (!state.resource || resource.version >= state.resource.version) state.resource = resource;
|
||||
if (!state.resource || resource.version >= state.resource.version)
|
||||
state.resource = resource;
|
||||
state.error = undefined;
|
||||
state.conflict = false;
|
||||
}
|
||||
@@ -179,7 +250,13 @@ export const useControlPlaneStore = defineStore("control-plane", () => {
|
||||
const state = stateFor(kind);
|
||||
state.loading = true;
|
||||
try {
|
||||
apply((await request<ResourceResponse>(`/api/control-plane/resources/${encodeURIComponent(kind)}`)).resource);
|
||||
apply(
|
||||
(
|
||||
await request<ResourceResponse>(
|
||||
`/api/control-plane/resources/${encodeURIComponent(kind)}`,
|
||||
)
|
||||
).resource,
|
||||
);
|
||||
} catch (error) {
|
||||
state.error = String((error as Error).message || error);
|
||||
} finally {
|
||||
@@ -191,11 +268,21 @@ export const useControlPlaneStore = defineStore("control-plane", () => {
|
||||
if (streams.has(kind)) return;
|
||||
const state = stateFor(kind);
|
||||
const after = state.resource?.version ?? 0;
|
||||
const stream = new EventSource(`/api/control-plane/watch/${encodeURIComponent(kind)}?after=${after}`);
|
||||
stream.onopen = () => { state.stream = "live"; };
|
||||
stream.onerror = () => { state.stream = "reconnecting"; };
|
||||
const stream = new EventSource(
|
||||
`/api/control-plane/watch/${encodeURIComponent(kind)}?after=${after}`,
|
||||
);
|
||||
stream.onopen = () => {
|
||||
state.stream = "live";
|
||||
};
|
||||
stream.onerror = () => {
|
||||
state.stream = "reconnecting";
|
||||
};
|
||||
stream.onmessage = (event) => {
|
||||
try { apply((JSON.parse(event.data) as ResourceResponse).resource); } catch { state.error = "Invalid live update"; }
|
||||
try {
|
||||
apply((JSON.parse(event.data) as ResourceResponse).resource);
|
||||
} catch {
|
||||
state.error = "Invalid live update";
|
||||
}
|
||||
};
|
||||
streams.set(kind, stream);
|
||||
}
|
||||
@@ -206,7 +293,9 @@ export const useControlPlaneStore = defineStore("control-plane", () => {
|
||||
* take the settings page with it. */
|
||||
async function loadApplyStatus(): Promise<void> {
|
||||
try {
|
||||
applyStatus.value = await request<ApplyStatusResponse>("/api/control-plane/apply-status");
|
||||
applyStatus.value = await request<ApplyStatusResponse>(
|
||||
"/api/control-plane/apply-status",
|
||||
);
|
||||
} catch {
|
||||
// Keep whatever was last read.
|
||||
}
|
||||
@@ -215,20 +304,27 @@ export const useControlPlaneStore = defineStore("control-plane", () => {
|
||||
/** Rows for one resource, every known process included. */
|
||||
function applyStatusFor(kind: string): ApplyStatusRow[] {
|
||||
return applyStatusForKind(
|
||||
applyStatus.value.records, applyStatus.value.processes, kind, stateFor(kind).resource?.version ?? 0,
|
||||
applyStatus.value.records,
|
||||
applyStatus.value.processes,
|
||||
kind,
|
||||
stateFor(kind).resource?.version ?? 0,
|
||||
);
|
||||
}
|
||||
|
||||
async function load(): Promise<void> {
|
||||
try {
|
||||
const response = await request<{ resources?: ResourceDescriptor[] }>("/api/control-plane/catalog");
|
||||
const response = await request<{ resources?: ResourceDescriptor[] }>(
|
||||
"/api/control-plane/catalog",
|
||||
);
|
||||
catalog.value = response.resources ?? [];
|
||||
catalogError.value = "";
|
||||
await loadApplyStatus();
|
||||
await Promise.all(genericResources.value.map(async ({ kind }) => {
|
||||
await loadResource(kind);
|
||||
watchResource(kind);
|
||||
}));
|
||||
await Promise.all(
|
||||
genericResources.value.map(async ({ kind }) => {
|
||||
await loadResource(kind);
|
||||
watchResource(kind);
|
||||
}),
|
||||
);
|
||||
} catch (error) {
|
||||
catalogError.value = String((error as Error).message || error);
|
||||
}
|
||||
@@ -263,13 +359,17 @@ export const useControlPlaneStore = defineStore("control-plane", () => {
|
||||
}
|
||||
|
||||
async function history(kind: string): Promise<ResourceRevision[]> {
|
||||
const response = await request<HistoryResponse>(`/api/control-plane/resources/${encodeURIComponent(kind)}/history`);
|
||||
const response = await request<HistoryResponse>(
|
||||
`/api/control-plane/resources/${encodeURIComponent(kind)}/history`,
|
||||
);
|
||||
return response.revisions ?? [];
|
||||
}
|
||||
|
||||
/** The shipped definitions the catalog offers as "restore shipped". */
|
||||
function shippedWorkflows(): WorkflowDefinitionCollection {
|
||||
const defaults = catalog.value.find((resource) => resource.kind === "workflow-definitions")?.defaults_json;
|
||||
const defaults = catalog.value.find(
|
||||
(resource) => resource.kind === "workflow-definitions",
|
||||
)?.defaults_json;
|
||||
if (!defaults) return { definitions: [] };
|
||||
try {
|
||||
return JSON.parse(defaults) as WorkflowDefinitionCollection;
|
||||
@@ -279,7 +379,16 @@ export const useControlPlaneStore = defineStore("control-plane", () => {
|
||||
}
|
||||
|
||||
return {
|
||||
applyStatusFor, catalog, catalogError, states, genericResources,
|
||||
history, load, loadApplyStatus, replace, shippedWorkflows, stateFor,
|
||||
applyStatusFor,
|
||||
catalog,
|
||||
catalogError,
|
||||
states,
|
||||
genericResources,
|
||||
history,
|
||||
load,
|
||||
loadApplyStatus,
|
||||
replace,
|
||||
shippedWorkflows,
|
||||
stateFor,
|
||||
};
|
||||
});
|
||||
+80
-10
@@ -1,10 +1,24 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { onScopeDispose, ref } from "vue";
|
||||
|
||||
export interface Identity { id: string; kind: "system" | "bot" | "service_account" | "user"; display_name: string; lifecycle: "active" | "suspended" | "retired"; version: number }
|
||||
export interface Identity {
|
||||
id: string;
|
||||
kind: "system" | "bot" | "service_account" | "user";
|
||||
display_name: string;
|
||||
lifecycle: "active" | "suspended" | "retired";
|
||||
version: number;
|
||||
}
|
||||
|
||||
async function call<T>(path: string, init: RequestInit = {}): Promise<T> {
|
||||
const response = await fetch(path, { ...init, headers: { Accept: "application/json", "Content-Type": "application/json", "X-Archie-CSRF": "1", ...init.headers } });
|
||||
const response = await fetch(path, {
|
||||
...init,
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
"X-Archie-CSRF": "1",
|
||||
...init.headers,
|
||||
},
|
||||
});
|
||||
if (!response.ok) throw new Error((await response.text()).trim());
|
||||
return response.json() as Promise<T>;
|
||||
}
|
||||
@@ -15,20 +29,76 @@ export const useIdentitiesStore = defineStore("identities", () => {
|
||||
const busy = ref("");
|
||||
let stream: EventSource | undefined;
|
||||
|
||||
async function load(): Promise<void> { try { identities.value = (await call<{ identities: Identity[] }>("/api/identities")).identities; error.value = ""; } catch (cause) { error.value = String((cause as Error).message || cause); } }
|
||||
async function load(): Promise<void> {
|
||||
try {
|
||||
identities.value = (
|
||||
await call<{ identities: Identity[] }>("/api/identities")
|
||||
).identities;
|
||||
error.value = "";
|
||||
} catch (cause) {
|
||||
error.value = String((cause as Error).message || cause);
|
||||
}
|
||||
}
|
||||
function watch(): void {
|
||||
if (stream) return;
|
||||
void load();
|
||||
stream = new EventSource("/api/identities/watch");
|
||||
stream.onmessage = (event) => {
|
||||
try { identities.value = (JSON.parse(event.data) as { identities: Identity[] }).identities; error.value = ""; }
|
||||
catch { error.value = "Invalid live update"; }
|
||||
try {
|
||||
identities.value = (
|
||||
JSON.parse(event.data) as { identities: Identity[] }
|
||||
).identities;
|
||||
error.value = "";
|
||||
} catch {
|
||||
error.value = "Invalid live update";
|
||||
}
|
||||
};
|
||||
stream.onerror = () => { error.value = "Identity updates disconnected"; };
|
||||
onScopeDispose(() => { stream?.close(); stream = undefined; });
|
||||
stream.onerror = () => {
|
||||
error.value = "Identity updates disconnected";
|
||||
};
|
||||
onScopeDispose(() => {
|
||||
stream?.close();
|
||||
stream = undefined;
|
||||
});
|
||||
}
|
||||
async function create(
|
||||
display_name: string,
|
||||
kind: Identity["kind"],
|
||||
): Promise<boolean> {
|
||||
return mutate("create", () =>
|
||||
call("/api/identities", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ display_name, kind }),
|
||||
}),
|
||||
);
|
||||
}
|
||||
async function command(
|
||||
value: Identity,
|
||||
commandName: string,
|
||||
display_name = "",
|
||||
): Promise<boolean> {
|
||||
return mutate(value.id, () =>
|
||||
call(`/api/identities/${encodeURIComponent(value.id)}/${commandName}`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ expected_version: value.version, display_name }),
|
||||
}),
|
||||
);
|
||||
}
|
||||
async function mutate(
|
||||
key: string,
|
||||
action: () => Promise<unknown>,
|
||||
): Promise<boolean> {
|
||||
busy.value = key;
|
||||
try {
|
||||
await action();
|
||||
await load();
|
||||
return true;
|
||||
} catch (cause) {
|
||||
error.value = String((cause as Error).message || cause);
|
||||
return false;
|
||||
} finally {
|
||||
busy.value = "";
|
||||
}
|
||||
}
|
||||
async function create(display_name: string, kind: Identity["kind"]): Promise<boolean> { return mutate("create", () => call("/api/identities", { method: "POST", body: JSON.stringify({ display_name, kind }) })); }
|
||||
async function command(value: Identity, commandName: string, display_name = ""): Promise<boolean> { return mutate(value.id, () => call(`/api/identities/${encodeURIComponent(value.id)}/${commandName}`, { method: "POST", body: JSON.stringify({ expected_version: value.version, display_name }) })); }
|
||||
async function mutate(key: string, action: () => Promise<unknown>): Promise<boolean> { busy.value = key; try { await action(); await load(); return true; } catch (cause) { error.value = String((cause as Error).message || cause); return false; } finally { busy.value = ""; } }
|
||||
return { identities, error, busy, watch, create, command };
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
export type LiveResource = "tasks" | "captures" | "curators" | "skills" | "updates";
|
||||
export type LiveResource =
|
||||
"tasks" | "captures" | "curators" | "skills" | "updates";
|
||||
|
||||
export interface LiveEvent {
|
||||
id?: number;
|
||||
@@ -35,7 +36,8 @@ export function resourcesForEvent(raw: unknown): LiveResource[] {
|
||||
const event = raw as LiveEvent | null;
|
||||
const kind = event?.kind ?? "";
|
||||
const resources = new Set<LiveResource>();
|
||||
if (event?.task_id || kind.startsWith("task_") || taskKinds.has(kind)) resources.add("tasks");
|
||||
if (event?.task_id || kind.startsWith("task_") || taskKinds.has(kind))
|
||||
resources.add("tasks");
|
||||
if (kind === "capture") resources.add("captures");
|
||||
if (kind.startsWith("curator_")) resources.add("curators");
|
||||
if (kind === "curator_action") resources.add("skills");
|
||||
|
||||
@@ -1,10 +1,21 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { computed, onUnmounted, reactive, ref, watch, type ComputedRef } from "vue";
|
||||
import {
|
||||
computed,
|
||||
onUnmounted,
|
||||
reactive,
|
||||
ref,
|
||||
watch,
|
||||
type ComputedRef,
|
||||
} from "vue";
|
||||
|
||||
import { setAuthenticationStateHandler, subscribeEvents } from "@/lib/api";
|
||||
import type { StatusKind } from "@/lib/status";
|
||||
import type { StreamState } from "@/lib/stream-state";
|
||||
import { resourcesForEvent, type LiveEvent, type LiveResource } from "./live-events";
|
||||
import {
|
||||
resourcesForEvent,
|
||||
type LiveEvent,
|
||||
type LiveResource,
|
||||
} from "./live-events";
|
||||
|
||||
export const useLiveUpdatesStore = defineStore("live-updates", () => {
|
||||
const streamState = ref<StreamState | "connecting">("connecting");
|
||||
@@ -55,12 +66,17 @@ export const useLiveUpdatesStore = defineStore("live-updates", () => {
|
||||
});
|
||||
|
||||
/** Re-read one mounted projection when SSE says its backend resource changed. */
|
||||
export function useLiveResource(resource: LiveResource | null, load: () => void, debounceMs = 100): void {
|
||||
export function useLiveResource(
|
||||
resource: LiveResource | null,
|
||||
load: () => void,
|
||||
debounceMs = 100,
|
||||
): void {
|
||||
const live = useLiveUpdatesStore();
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
const revision = () => resource === null
|
||||
? live.connectionRevision
|
||||
: [live.revisions[resource], live.connectionRevision];
|
||||
const revision = () =>
|
||||
resource === null
|
||||
? live.connectionRevision
|
||||
: [live.revisions[resource], live.connectionRevision];
|
||||
const stop = watch(revision, () => {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(load, debounceMs);
|
||||
|
||||
Reference in new issue
Block a user