Skip to content

AJAX Requests to loop over checks to run#114

Merged
jjgrainger merged 9 commits into
trunkfrom
feature/ajax-run-checks-loop
Mar 31, 2023
Merged

AJAX Requests to loop over checks to run#114
jjgrainger merged 9 commits into
trunkfrom
feature/ajax-run-checks-loop

Conversation

@jjgrainger
Copy link
Copy Markdown
Contributor

Addresses acceptance criteria in #84

Closes #84

@jjgrainger
Copy link
Copy Markdown
Contributor Author

Set up as a draft so once #104 is merged I can update with the JS formatting here

Comment thread assets/js/plugin-check-admin.js Outdated
Comment on lines +155 to +158
for (var i = 0; i < data.checks.length; i++) {
let result = await runCheck( data.plugin, data.checks[ i ] );
results.push( result );
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a rule, it is a not good to do a series of promises in a loop like this. Do these NEED to run it order. What if one of the promises fail?

Copy link
Copy Markdown
Member

@spacedmonkey spacedmonkey Mar 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend using Promise.all here.

Try something like this.

const promises = [];
for (var i = 0; i < data.checks.length; i++) {
	promises.push(  runCheck( data.plugin, data.checks[ i ] ) );
}
const results = await Promise.all( promises );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, not sure about that one here. Normally I would agree with you, but I think it's a better user experience to show the check results in a consistent order, so circling through the checks makes sense to me in this context.

If we use Promise.all, they will be handled in a random order. That would only be a good idea if we wanted to output all results together at the end, but I think it's a good idea to output results one by one as it gives the user feedback already as soon as there are results.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @spacedmonkey @felixarntz

Another point to raise here is that we would want these to run sequentially. We wouldn't want to have 2 runtime checks running in parallel, where both their preparations are implemented at the same time, as this may cause incorrect results returned by checks.

I have updated this to log the results as they're returned.

Comment thread includes/Admin/Admin_AJAX.php Outdated
// If there are any files left with only warnings, print those next.
foreach ( $warnings as $file_name => $file_warnings ) {
$file_results = $this->flatten_file_results( $file_name, array(), $file_warnings );
$all_results = array_merge( $all_results, $file_results );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be expensive to call array_merge. Maybe just add $file_results to an array and then fatten it at the end.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @spacedmonkey I agree here, although the updates to handle the formatting on the JS side now has meant this code has been removed.

Comment thread includes/Admin/Admin_AJAX.php Outdated
Copy link
Copy Markdown
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jjgrainger, mostly looks good. My main feedback is that I don't think we should do all the formatting in the AJAX endpoint as it seems unnecessary here and could happen in JS depending on the template usage.

Comment thread includes/Admin/Admin_AJAX.php Outdated
Comment thread assets/js/plugin-check-admin.js Outdated
Comment on lines +155 to +158
for (var i = 0; i < data.checks.length; i++) {
let result = await runCheck( data.plugin, data.checks[ i ] );
results.push( result );
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, not sure about that one here. Normally I would agree with you, but I think it's a better user experience to show the check results in a consistent order, so circling through the checks makes sense to me in this context.

If we use Promise.all, they will be handled in a random order. That would only be a good idea if we wanted to output all results together at the end, but I think it's a good idea to output results one by one as it gives the user feedback already as soon as there are results.

Comment thread assets/js/plugin-check-admin.js Outdated
Comment thread includes/Admin/Admin_AJAX.php Outdated
Comment thread includes/Admin/Admin_AJAX.php Outdated
Comment thread includes/Admin/Admin_AJAX.php Outdated
@jjgrainger jjgrainger marked this pull request as ready for review March 30, 2023 12:48
@jjgrainger
Copy link
Copy Markdown
Contributor Author

Thanks @spacedmonkey & @felixarntz

I've made updates based on the feedback and is ready for you to review.

Copy link
Copy Markdown
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jjgrainger Thank you for the updates, LGTM!

One tiny thing unrelated to the actual PR.

Comment thread package-lock.json
Comment thread assets/js/plugin-check-admin.js Outdated
Copy link
Copy Markdown
Member

@spacedmonkey spacedmonkey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested one change and I will approve. We are nearly there.

@jjgrainger jjgrainger requested a review from spacedmonkey March 31, 2023 12:59
Copy link
Copy Markdown
Member

@spacedmonkey spacedmonkey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good to me.

@jjgrainger jjgrainger merged commit 190050a into trunk Mar 31, 2023
@jjgrainger jjgrainger deleted the feature/ajax-run-checks-loop branch March 31, 2023 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle running checks in loop and return output

3 participants