Skip to content

Conversation

@dcalhoun
Copy link
Member

What?

Add type checks before wrapping wp.ajax.send and wp.ajax.post methods in the AJAX configuration.

Why?

This addresses PR review feedback about a potential race condition. If window.wp.ajax.send or window.wp.ajax.post are undefined when configureAjax() executes, the wrapped function would throw a TypeError when invoked (calling .call() on undefined).

References: #181

How?

  • Added typeof window.wp.ajax.send === 'function' check before wrapping the send method
  • Added typeof window.wp.ajax.post === 'function' check before wrapping the post method
  • Methods are only wrapped if they exist as functions, preventing runtime errors
  • Updated tests to verify that missing methods remain undefined rather than being wrapped with an undefined reference

Testing Instructions

  1. Run the test suite: npm run test:unit
  2. Verify all 20 tests in ajax.test.js pass
  3. Specifically check the "should handle missing wp.ajax.send method" and "should handle missing wp.ajax.post method" tests

Accessibility Testing Instructions

N/A - This is a JavaScript utility change with no UI impact.

Screenshots or screencast

N/A - No visual changes.

Address PR feedback about potential race condition. The code now checks
if `window.wp.ajax.send` and `window.wp.ajax.post` are functions before
wrapping them. This prevents TypeError when calling the wrapped function
if the original method was undefined during configuration.

Update tests to verify that missing methods remain undefined rather than
being wrapped with an undefined reference.
@dcalhoun dcalhoun marked this pull request as ready for review January 14, 2026 20:03
@dcalhoun dcalhoun merged commit 394bce2 into feat/authorize-ajax-with-application-passwords Jan 14, 2026
8 checks passed
@dcalhoun dcalhoun deleted the claude/pr181-race-condition-fix-EoBab branch January 14, 2026 20:03
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.

3 participants