File size: 1,077 Bytes
fa9c65f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | export interface HydrationTask {
name: string;
task: () => Promise<void>;
}
interface RunHydrationTierOptions {
tasks: HydrationTask[];
maxConcurrency: number;
yieldToMain: () => Promise<void>;
onFailure: (name: string, reason: unknown) => void;
}
/**
* Run one priority tier without letting mobile panel loaders merge into the
* same main-thread task. The caller controls the concurrency policy so desktop
* force-all hydration retains its existing parallelism. (#5165)
*/
export async function runHydrationTier({
tasks,
maxConcurrency,
yieldToMain,
onFailure,
}: RunHydrationTierOptions): Promise<void> {
let cursor = 0;
while (cursor < tasks.length) {
const batch = tasks.slice(cursor, cursor + maxConcurrency);
const results = await Promise.allSettled(batch.map(item => item.task()));
results.forEach((result, idx) => {
if (result.status === 'rejected') {
onFailure(batch[idx]?.name ?? 'unknown', result.reason);
}
});
cursor += batch.length;
if (cursor < tasks.length) await yieldToMain();
}
}
|