Improve application responsiveness and background processing
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
/** Load only the requested history, respecting the API's per-request page bound. */
|
||||
export async function fetchCampaigns<T>(filters: Record<string, string>, count: number, signal?: AbortSignal): Promise<{ deployments: T[]; total: number }> {
|
||||
const deployments: T[] = [];
|
||||
let total = Infinity;
|
||||
while (deployments.length < Math.min(count, total)) {
|
||||
const params = new URLSearchParams({ ...filters, limit: String(Math.min(500, count - deployments.length)), offset: String(deployments.length) });
|
||||
const response = await fetch(`/api/campaigns?${params}`, { signal });
|
||||
if (!response.ok) throw new Error('Could not refresh campaigns.');
|
||||
const page = await response.json() as { deployments: T[]; total: number };
|
||||
total = page.total;
|
||||
deployments.push(...page.deployments);
|
||||
if (!page.deployments.length) break;
|
||||
}
|
||||
return { deployments, total: Number.isFinite(total) ? total : 0 };
|
||||
}
|
||||
Reference in New Issue
Block a user