Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
fix: use a semaphore to avoid running axe concurrently (#33)
Browse files Browse the repository at this point in the history
A previous commit (1d4bd8d) removed our pseudo-semaphore which prevented us from running aXe concurrently. This patch adds back our "timer" semaphore and ensures we do not can `axe.run()` more than once on the same tick.

Closes #32.
  • Loading branch information
stephenmathieson authored Apr 5, 2018
1 parent 1893f39 commit 151c906
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var moderate = 'color:orange;font-weight:bold;';
var minor = 'color:orange;font-weight:normal;';
var defaultReset = 'font-color:black;font-weight:normal;';

var timer;
var timeout;
var _createElement;
var components = {};
var nodes = [];
Expand Down Expand Up @@ -92,16 +94,37 @@ function failureSummary(node, key) {
}
}

function checkAndReport(node) {
function createDeferred () {
var deferred = {};

deferred.promise = new Promise(function (resolve, reject) {
deferred.resolve= resolve
deferred.reject = reject
})

return deferred;
}

function checkAndReport(node, timeout) {
if (timer) {
clearTimeout(timer);
timer = undefined;
}

var deferred = createDeferred()

nodes.push(node);
return new Promise(function(res, rej) {
timer = setTimeout(function () {
var n = getCommonParent(nodes);
if (n.nodeName.toLowerCase() === 'html') {
// if the only common parent is the body, then analyze the whole page
n = document;
}
axeCore.run(n, { reporter: 'v2' }, function (error, results) {
if (error) { throw rej(error); }
if (error) {
return deferred.reject(error)
}

results.violations = results.violations.filter(function (result) {
result.nodes = result.nodes.filter(function (node) {
var key = node.target.toString() + result.id;
Expand Down Expand Up @@ -141,16 +164,19 @@ function checkAndReport(node) {
});
console.groupEnd();
}
res();

deferred.resolve()
});
});
}, timeout);

return deferred.promise;
}

function checkNode(component) {
var node = ReactDOM.findDOMNode(component);

if (node) {
checkAndReport(node);
checkAndReport(node, timeout);
}
}

Expand All @@ -166,9 +192,10 @@ function addComponent(component) {
}
}

var reactAxe = function reactAxe(_React, _ReactDOM, conf) {
var reactAxe = function reactAxe(_React, _ReactDOM, _timeout, conf) {
React = _React;
ReactDOM = _ReactDOM;
timeout = _timeout;

if (conf) {
axeCore.configure(conf);
Expand Down Expand Up @@ -198,7 +225,7 @@ var reactAxe = function reactAxe(_React, _ReactDOM, conf) {
};
}

return checkAndReport(document.body);
return checkAndReport(document.body, timeout);
};

module.exports = reactAxe;

0 comments on commit 151c906

Please sign in to comment.