-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use a queue of pending audits to run on idle
- Loading branch information
Juliette Pretot
committed
Nov 29, 2019
1 parent
2289f19
commit 2928661
Showing
4 changed files
with
70 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { rIC as requestIdleCallback } from 'idlize/idle-callback-polyfills.mjs' | ||
|
||
export default class AuditQueue { | ||
constructor() { | ||
this._pendingAudits = [] | ||
this._isRunning = false | ||
|
||
this.run = this.run.bind(this) | ||
this._scheduleAudits = this._scheduleAudits.bind(this) | ||
} | ||
run(auditCallback) { | ||
// Returns a promise that resolves to the result of the auditCallback. | ||
const resultPromise = new Promise() | ||
|
||
this._pendingAudits.push({ auditCallback, resultPromise }) | ||
if (!this._isRunning) this._scheduleAudits() | ||
|
||
return resultPromise | ||
} | ||
_scheduleAudits() { | ||
this._isRunning = true | ||
requestIdleCallback(async IdleDeadline => { | ||
// Run pending audits as long as they exist & we have time. | ||
while ( | ||
this._pendingAudits.length > 0 && | ||
IdleDeadline.timeRemaining() > 0 | ||
) { | ||
// Only run one audit as a time, as axe-core does not allow for | ||
// concurrent runs. | ||
// Ref: https://github.com/storybookjs/storybook/pull/4086 | ||
const { auditCallback, resultPromise } = this._pendingAudits[0] | ||
// Run the audit and resolve the associated promise. | ||
const result = await auditCallback() | ||
resultPromise.resolve(result) | ||
|
||
// Once an audit has run, remove it from the queue. | ||
this._pendingAudits.shift() | ||
} | ||
|
||
if (this._pendingAudits.length > 0) { | ||
// If pending audits remain, schedule them for the next idle phase. | ||
this._scheduleAudits() | ||
} else { | ||
// The queue is empty, we're no longer running | ||
this._isRunning = false | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,6 @@ | |
}, | ||
"dependencies": { | ||
"axe-core": "^3.4.0", | ||
"p-queue": "^6.2.1" | ||
"idlize": "^0.1.1" | ||
} | ||
} |