/**
* @module pete/lib/reporters/callback
*/
module.exports = createCaller;
/**
* Create and return report function that will call back once all reports are gathered.
*
* @alias module:pete/lib/reporters/callback
* @param {object} [options]
* @param {Function} [options.out] function to call back with array of reports
* @return {module:pete/lib/getReporters~report}
*/
function createCaller (options) {
var out = (options && options.out) || console.log;
var index = 0;
var reports = [];
if (typeof out !== 'function') {
out = console.log;
}
return function caller (err, report) {
if (index < 0) {
console.error(new Error('Callback reporter called after it was closed'));
return;
}
if (!err && !report) {
out(reports);
reports = [];
index = -1;
return;
}
index++;
reports.push(report);
};
}