Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions apps/web/src/state/usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,37 @@ describe("usage environment scope", () => {
expect(scope.environments).toEqual(options);
});
});

describe("usage refresh settling", () => {
it("settles a refresh only after every environment answers the new request", () => {
// Retained summaries from the previous request, both refreshing.
const macRefreshing = {
phase: "connected" as const,
isPending: true,
summary: {},
error: null,
};
const linuxRefreshing = {
phase: "connected" as const,
isPending: true,
summary: {},
error: null,
};
expect(getEnvironmentUsageLoadingState([macRefreshing, linuxRefreshing])).toEqual({
isPending: true,
isPartial: false,
});

const macAnswered = { ...macRefreshing, isPending: false };
expect(getEnvironmentUsageLoadingState([macAnswered, linuxRefreshing])).toEqual({
isPending: false,
isPartial: true,
});

const linuxAnswered = { ...linuxRefreshing, isPending: false };
expect(getEnvironmentUsageLoadingState([macAnswered, linuxAnswered])).toEqual({
isPending: false,
isPartial: false,
});
});
});
11 changes: 9 additions & 2 deletions apps/web/src/state/usageEnvironmentScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,20 @@ interface EnvironmentUsageLoadingEntry {
export function isEnvironmentUsageStillReporting(
environment: EnvironmentUsageLoadingEntry,
): boolean {
return environment.isPending && environment.summary === null && environment.error === null;
// A retained summary does not mean this environment has reported: it is the
// previous request's answer, still on screen while the refresh runs.
return environment.isPending && environment.error === null;
}

export function getEnvironmentUsageLoadingState(
environments: readonly EnvironmentUsageLoadingEntry[],
): { readonly isPending: boolean; readonly isPartial: boolean } {
const answeredCount = environments.filter((environment) => environment.summary !== null).length;
// SWR keeps the previous summary on screen while a refresh is in flight, so
// a retained value belongs to the *previous* request. Counting it as an
// answer settled a refresh before any environment had reported new numbers.
const answeredCount = environments.filter(
(environment) => environment.summary !== null && !environment.isPending,
).length;
const stillReporting = environments.filter(isEnvironmentUsageStillReporting).length;

return {
Expand Down
157 changes: 0 additions & 157 deletions apps/web/src/state/usageStatus.test.ts

This file was deleted.

81 changes: 0 additions & 81 deletions apps/web/src/state/usageStatus.ts

This file was deleted.

Loading