fix(ui): keep unchanged rows across live reloads on the dashboard and workflows (#1798)
This commit is contained in:
5 files changed
+74
-47
No files matched your search
@@ -0,0 +1,38 @@
|
|||||||
|
// Live events re-read whole lists, and most of what comes back is unchanged.
|
||||||
|
// Keeping the previous object for an unchanged value lets Vue skip it: an
|
||||||
|
// unchanged row keeps its component, and a reload that changed nothing
|
||||||
|
// triggers nothing at all.
|
||||||
|
|
||||||
|
const serializedCache = new WeakMap<object, string>();
|
||||||
|
|
||||||
|
function serialized(value: object): string {
|
||||||
|
let json = serializedCache.get(value);
|
||||||
|
if (json === undefined) {
|
||||||
|
json = JSON.stringify(value);
|
||||||
|
serializedCache.set(value, json);
|
||||||
|
}
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The previous value when the incoming one has the same content. */
|
||||||
|
export function keepValue<T>(previous: T, incoming: T): T {
|
||||||
|
if (previous === incoming) return previous;
|
||||||
|
if (previous === null || incoming === null || typeof previous !== "object" || typeof incoming !== "object")
|
||||||
|
return incoming;
|
||||||
|
return serialized(previous) === serialized(incoming) ? previous : incoming;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The incoming rows, each replaced by its previous object when unchanged, and
|
||||||
|
* the previous array itself when nothing changed. */
|
||||||
|
export function keepRows<T extends { id: number | string }>(previous: readonly T[] | null, incoming: T[]): T[] {
|
||||||
|
if (!previous) return incoming;
|
||||||
|
const byId = new Map(previous.map((row) => [row.id, row]));
|
||||||
|
let changed = previous.length !== incoming.length;
|
||||||
|
const merged = incoming.map((row, index) => {
|
||||||
|
const old = byId.get(row.id);
|
||||||
|
const kept = old && serialized(old) === serialized(row) ? old : row;
|
||||||
|
if (kept !== previous[index]) changed = true;
|
||||||
|
return kept;
|
||||||
|
});
|
||||||
|
return changed ? merged : (previous as T[]);
|
||||||
|
}
|
||||||
+15
-10
@@ -1,9 +1,10 @@
|
|||||||
import { defineStore } from "pinia";
|
import { defineStore } from "pinia";
|
||||||
import { ref } from "vue";
|
import { shallowRef } from "vue";
|
||||||
|
|
||||||
import type { ActivityDetailInput } from "../dashboard/activity-detail.ts";
|
import type { ActivityDetailInput } from "../dashboard/activity-detail.ts";
|
||||||
import type { Setup } from "../dashboard/setup-preference.ts";
|
import type { Setup } from "../dashboard/setup-preference.ts";
|
||||||
import { api } from "../lib/api.ts";
|
import { api } from "../lib/api.ts";
|
||||||
|
import { keepRows, keepValue } from "../lib/keep-unchanged.ts";
|
||||||
|
|
||||||
export interface Summary {
|
export interface Summary {
|
||||||
statuses?: Record<string, number>;
|
statuses?: Record<string, number>;
|
||||||
@@ -40,12 +41,14 @@ export function taskIDForEvent(event: ActivityEvent, bySource: Map<string, numbe
|
|||||||
/** The board as the dashboard reads it: counts, setup progress and the
|
/** The board as the dashboard reads it: counts, setup progress and the
|
||||||
* recent tasks. The Tasks page pages its own list. */
|
* recent tasks. The Tasks page pages its own list. */
|
||||||
export const useTasksStore = defineStore("tasks", () => {
|
export const useTasksStore = defineStore("tasks", () => {
|
||||||
const summary = ref<Summary | null>(null);
|
// Shallow and replaced only on change: every task event reloads the board,
|
||||||
const setup = ref<Setup | null>(null);
|
// and a reload that changed nothing must not re-render it.
|
||||||
|
const summary = shallowRef<Summary | null>(null);
|
||||||
|
const setup = shallowRef<Setup | null>(null);
|
||||||
// null means loading, [] means loaded.
|
// null means loading, [] means loaded.
|
||||||
const tasks = ref<DashboardTask[] | null>(null);
|
const tasks = shallowRef<DashboardTask[] | null>(null);
|
||||||
const error = ref<string | null>(null);
|
const error = shallowRef<string | null>(null);
|
||||||
const taskIDsBySource = ref(new Map<string, number>());
|
const taskIDsBySource = shallowRef(new Map<string, number>());
|
||||||
|
|
||||||
async function load(): Promise<void> {
|
async function load(): Promise<void> {
|
||||||
error.value = null;
|
error.value = null;
|
||||||
@@ -57,16 +60,18 @@ export const useTasksStore = defineStore("tasks", () => {
|
|||||||
// which a failed fetch must never claim.
|
// which a failed fetch must never claim.
|
||||||
api.tasks<DashboardTask[]>(),
|
api.tasks<DashboardTask[]>(),
|
||||||
]);
|
]);
|
||||||
|
const kept = keepRows(tasks.value, nextTasks);
|
||||||
|
summary.value = keepValue(summary.value, nextSummary);
|
||||||
|
setup.value = keepValue(setup.value, nextSetup);
|
||||||
|
if (kept === tasks.value) return;
|
||||||
const map = new Map<string, number>();
|
const map = new Map<string, number>();
|
||||||
for (const task of nextTasks) {
|
for (const task of kept) {
|
||||||
// A run that handed off yields its work item to its successor.
|
// A run that handed off yields its work item to its successor.
|
||||||
if (task.owner && task.repo && task.issue_number && !task.handoff_to_task_id)
|
if (task.owner && task.repo && task.issue_number && !task.handoff_to_task_id)
|
||||||
map.set(`${task.owner}/${task.repo}#${task.issue_number}`, Number(task.id));
|
map.set(`${task.owner}/${task.repo}#${task.issue_number}`, Number(task.id));
|
||||||
}
|
}
|
||||||
taskIDsBySource.value = map;
|
taskIDsBySource.value = map;
|
||||||
summary.value = nextSummary;
|
tasks.value = kept;
|
||||||
setup.value = nextSetup;
|
|
||||||
tasks.value = nextTasks;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
error.value = String((err as Error).message || err);
|
error.value = String((err as Error).message || err);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import { computed, nextTick, onMounted, ref, shallowRef, watch } from "vue";
|
|||||||
import { useRoute, useRouter } from "vue-router";
|
import { useRoute, useRouter } from "vue-router";
|
||||||
|
|
||||||
import PageHeader from "@/base/PageHeader.vue";
|
import PageHeader from "@/base/PageHeader.vue";
|
||||||
|
import { keepRows } from "@/lib/keep-unchanged";
|
||||||
import { Card, CardContent } from "@/components/ui/card";
|
import { Card, CardContent } from "@/components/ui/card";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { api } from "@/lib/api";
|
import { api } from "@/lib/api";
|
||||||
@@ -102,31 +103,6 @@ const state = computed<"loading" | "error" | "empty" | "no-match">(() => {
|
|||||||
|
|
||||||
const PAGE = 100;
|
const PAGE = 100;
|
||||||
|
|
||||||
// Each live event re-reads the board, and most rows come back unchanged. A row
|
|
||||||
// that is unchanged keeps its object, so the table skips it; a reload that
|
|
||||||
// changed nothing keeps the whole array, so nothing re-renders.
|
|
||||||
const rowJSON = new WeakMap<Task, string>();
|
|
||||||
function serialized(task: Task): string {
|
|
||||||
let json = rowJSON.get(task);
|
|
||||||
if (json === undefined) {
|
|
||||||
json = JSON.stringify(task);
|
|
||||||
rowJSON.set(task, json);
|
|
||||||
}
|
|
||||||
return json;
|
|
||||||
}
|
|
||||||
function mergeRows(previous: Task[] | null, incoming: Task[]): Task[] {
|
|
||||||
if (!previous) return incoming;
|
|
||||||
const byId = new Map(previous.map((task) => [task.id, task]));
|
|
||||||
let changed = previous.length !== incoming.length;
|
|
||||||
const merged = incoming.map((task, index) => {
|
|
||||||
const old = byId.get(task.id);
|
|
||||||
const kept = old && serialized(old) === serialized(task) ? old : task;
|
|
||||||
if (kept !== previous[index]) changed = true;
|
|
||||||
return kept;
|
|
||||||
});
|
|
||||||
return changed ? merged : previous;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Every read bumps the generation; a response that settles after a newer read
|
// Every read bumps the generation; a response that settles after a newer read
|
||||||
// started is dropped, so the previous filter's rows or next cursor never land
|
// started is dropped, so the previous filter's rows or next cursor never land
|
||||||
// under the current filter.
|
// under the current filter.
|
||||||
@@ -141,7 +117,7 @@ async function load() {
|
|||||||
try {
|
try {
|
||||||
const page = await api.taskPage<Task>({ status: status.value || undefined, limit });
|
const page = await api.taskPage<Task>({ status: status.value || undefined, limit });
|
||||||
if (current !== loadGeneration) return;
|
if (current !== loadGeneration) return;
|
||||||
tasks.value = mergeRows(tasks.value, page.tasks);
|
tasks.value = keepRows(tasks.value, page.tasks);
|
||||||
next.value = page.next;
|
next.value = page.next;
|
||||||
moreError.value = null;
|
moreError.value = null;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { Spinner } from "@/components/ui/spinner";
|
import { Spinner } from "@/components/ui/spinner";
|
||||||
import { computed, onMounted, ref, watch } from "vue";
|
import { computed, onMounted, ref, shallowRef, watch } from "vue";
|
||||||
import { useRoute } from "vue-router";
|
import { useRoute } from "vue-router";
|
||||||
import { Plus } from "@lucide/vue";
|
import { Plus } from "@lucide/vue";
|
||||||
|
|
||||||
@@ -19,6 +19,7 @@ import { StatusDot } from "@/components/ui/status-dot";
|
|||||||
import { Table, TableBody, TableCell, TableRow } from "@/components/ui/table";
|
import { Table, TableBody, TableCell, TableRow } from "@/components/ui/table";
|
||||||
import { Switch } from "@/components/ui/switch";
|
import { Switch } from "@/components/ui/switch";
|
||||||
import { api } from "@/lib/api";
|
import { api } from "@/lib/api";
|
||||||
|
import { keepRows, keepValue } from "@/lib/keep-unchanged";
|
||||||
import { delivered } from "@/lib/delivered";
|
import { delivered } from "@/lib/delivered";
|
||||||
import { compact } from "@/lib/format";
|
import { compact } from "@/lib/format";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
@@ -50,7 +51,7 @@ interface RunTask {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const store = useWorkflowsStore();
|
const store = useWorkflowsStore();
|
||||||
const data = ref<WorkflowsResponse>({});
|
const data = shallowRef<WorkflowsResponse>({});
|
||||||
const error = ref<string | null>(null);
|
const error = ref<string | null>(null);
|
||||||
const loading = ref(true);
|
const loading = ref(true);
|
||||||
|
|
||||||
@@ -59,7 +60,7 @@ async function load() {
|
|||||||
// The sidebar's running count and last status come from the per-workflow
|
// The sidebar's running count and last status come from the per-workflow
|
||||||
// statistics: a page of the newest tasks across every workflow would show
|
// statistics: a page of the newest tasks across every workflow would show
|
||||||
// nothing for a workflow whose runs are older than that page.
|
// nothing for a workflow whose runs are older than that page.
|
||||||
data.value = (await api.workflows<WorkflowsResponse>()) ?? {};
|
data.value = keepValue(data.value, (await api.workflows<WorkflowsResponse>()) ?? {});
|
||||||
error.value = null;
|
error.value = null;
|
||||||
void loadWorkflowRuns();
|
void loadWorkflowRuns();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -122,19 +123,25 @@ async function setEnabled(value: boolean) {
|
|||||||
const stages = computed(() => (data.value.stages ?? []).filter((s) => s.workflow === selected.value));
|
const stages = computed(() => (data.value.stages ?? []).filter((s) => s.workflow === selected.value));
|
||||||
// The selected workflow's runs come from a workflow-filtered page: the global
|
// The selected workflow's runs come from a workflow-filtered page: the global
|
||||||
// list above holds only the newest tasks of every workflow.
|
// list above holds only the newest tasks of every workflow.
|
||||||
const workflowRuns = ref<RunTask[]>([]);
|
const workflowRuns = shallowRef<RunTask[]>([]);
|
||||||
const runsLoading = ref(false);
|
const runsLoading = ref(false);
|
||||||
const runsError = ref("");
|
const runsError = ref("");
|
||||||
let runsRequest = 0;
|
let runsRequest = 0;
|
||||||
|
// A live reload keeps the rows on screen; only a new selection clears them.
|
||||||
|
let runsFor = "";
|
||||||
async function loadWorkflowRuns() {
|
async function loadWorkflowRuns() {
|
||||||
const request = ++runsRequest;
|
const request = ++runsRequest;
|
||||||
const requested = selected.value;
|
const requested = selected.value;
|
||||||
runsLoading.value = true;
|
if (requested !== runsFor) {
|
||||||
runsError.value = "";
|
runsFor = requested;
|
||||||
workflowRuns.value = [];
|
workflowRuns.value = [];
|
||||||
|
runsLoading.value = true;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const page = requested ? await api.taskPage<RunTask>({ workflow: requested }) : null;
|
const page = requested ? await api.taskPage<RunTask>({ workflow: requested }) : null;
|
||||||
if (request === runsRequest) workflowRuns.value = page?.tasks ?? [];
|
if (request !== runsRequest) return;
|
||||||
|
workflowRuns.value = keepRows(workflowRuns.value, page?.tasks ?? []);
|
||||||
|
runsError.value = "";
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (request === runsRequest) runsError.value = String((err as Error).message || err);
|
if (request === runsRequest) runsError.value = String((err as Error).message || err);
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { computed, ref, watch, type Ref } from "vue";
|
import { computed, ref, shallowRef, watch, type Ref } from "vue";
|
||||||
|
|
||||||
import { api } from "@/lib/api";
|
import { api } from "@/lib/api";
|
||||||
|
import { keepRows } from "@/lib/keep-unchanged";
|
||||||
import { useLiveResource } from "@/stores/session";
|
import { useLiveResource } from "@/stores/session";
|
||||||
import type { StageRun } from "./workflow-graph";
|
import type { StageRun } from "./workflow-graph";
|
||||||
|
|
||||||
@@ -24,7 +25,7 @@ const RECENT_RUNS = 20;
|
|||||||
* until the operator picks another.
|
* until the operator picks another.
|
||||||
*/
|
*/
|
||||||
export function useWorkflowRuns(workflow: Ref<string>) {
|
export function useWorkflowRuns(workflow: Ref<string>) {
|
||||||
const runs = ref<WorkflowRun[]>([]);
|
const runs = shallowRef<WorkflowRun[]>([]);
|
||||||
const watched = ref("");
|
const watched = ref("");
|
||||||
const stages = ref<StageRun[]>([]);
|
const stages = ref<StageRun[]>([]);
|
||||||
const attempt = ref(0);
|
const attempt = ref(0);
|
||||||
@@ -45,7 +46,7 @@ export function useWorkflowRuns(workflow: Ref<string>) {
|
|||||||
try {
|
try {
|
||||||
const page = requested ? await api.taskPage<WorkflowRun>({ workflow: requested, limit: RECENT_RUNS }) : null;
|
const page = requested ? await api.taskPage<WorkflowRun>({ workflow: requested, limit: RECENT_RUNS }) : null;
|
||||||
if (workflow.value !== requested || request !== requestNumber) return;
|
if (workflow.value !== requested || request !== requestNumber) return;
|
||||||
runs.value = (page?.tasks ?? []).sort((a, b) => Number(b.id) - Number(a.id));
|
runs.value = keepRows(runs.value, (page?.tasks ?? []).sort((a, b) => Number(b.id) - Number(a.id)));
|
||||||
error.value = "";
|
error.value = "";
|
||||||
if (!picked || (watched.value && !runs.value.some((run) => String(run.id) === watched.value)))
|
if (!picked || (watched.value && !runs.value.some((run) => String(run.id) === watched.value)))
|
||||||
watched.value = runs.value[0] ? String(runs.value[0].id) : "";
|
watched.value = runs.value[0] ? String(runs.value[0].id) : "";
|
||||||
|
|||||||
Reference in new issue
Block a user