Skip to content
This repository was archived by the owner on Apr 16, 2026. It is now read-only.

Commit 886c3cf

Browse files
committed
Use chunked audit to keep main-thread tasks under 12ms
Replace synchronous runAudit() with createChunkedAudit() from @accesslint/core, yielding to the main thread between 12ms time-boxed chunks to avoid long tasks during story evaluation.
1 parent e8ff48e commit 886c3cf

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

src/preview.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { runAudit, getRuleById, configureRules } from "@accesslint/core";
1+
import { createChunkedAudit, getActiveRules, getRuleById, configureRules } from "@accesslint/core";
22

33
// Defined by the accesslintTest() Vite plugin when tags.skip is configured
44
declare const __ACCESSLINT_SKIP_TAGS__: string[];
@@ -8,7 +8,13 @@ configureRules({
88
disabledRules: ["accesslint-045"],
99
});
1010

11-
function scopeViolations(violations: ReturnType<typeof runAudit>["violations"]) {
11+
const BUDGET_MS = 12;
12+
13+
function yieldToMain(): Promise<void> {
14+
return new Promise((resolve) => setTimeout(resolve, 0));
15+
}
16+
17+
function scopeViolations<T extends { selector: string }>(violations: T[]): T[] {
1218
const root = document.getElementById("storybook-root");
1319
if (!root) return violations;
1420
return violations.filter((v) => {
@@ -22,7 +28,7 @@ function scopeViolations(violations: ReturnType<typeof runAudit>["violations"])
2228
});
2329
}
2430

25-
function enrichViolations(violations: ReturnType<typeof runAudit>["violations"]) {
31+
function enrichViolations(violations: { ruleId: string; selector: string; html: string; impact: string; message: string; context?: string; element?: Element }[]) {
2632
return violations.map((v) => {
2733
const rule = getRuleById(v.ruleId);
2834
return {
@@ -62,8 +68,13 @@ export const afterEach = async ({
6268
typeof __ACCESSLINT_SKIP_TAGS__ !== "undefined" ? __ACCESSLINT_SKIP_TAGS__ : [];
6369
if (skipTags.length > 0 && tags?.some((t) => skipTags.includes(t))) return;
6470

65-
const result = runAudit(document);
66-
const scoped = scopeViolations(result.violations);
71+
const audit = createChunkedAudit(document);
72+
while (audit.processChunk(BUDGET_MS)) {
73+
await yieldToMain();
74+
}
75+
76+
const violations = audit.getViolations();
77+
const scoped = scopeViolations(violations);
6778
const enriched = enrichViolations(scoped);
6879

6980
const hasViolations = enriched.length > 0;
@@ -74,7 +85,7 @@ export const afterEach = async ({
7485
version: 1,
7586
result: {
7687
violations: enriched,
77-
ruleCount: result.ruleCount,
88+
ruleCount: getActiveRules().length,
7889
},
7990
status: hasViolations ? mode : "passed",
8091
});

0 commit comments

Comments
 (0)