Skip to content

feat: await promises in WebView JS eval via token-based poll loop#261

Merged
gmegidish merged 5 commits into
mainfrom
feat/webview-async-eval-promise
Jun 9, 2026
Merged

feat: await promises in WebView JS eval via token-based poll loop#261
gmegidish merged 5 commits into
mainfrom
feat/webview-async-eval-promise

Conversation

@gmegidish

Copy link
Copy Markdown
Member

No description provided.

@coderabbitai

coderabbitai Bot commented Jun 8, 2026

Copy link
Copy Markdown

Walkthrough

WebViewAgent's JavaScript evaluation method is refactored to support async expressions. A per-process evaluation token counter is introduced. The evaluateExpression method now injects JavaScript that wraps the expression in promise-aware code and stores the resolved result as JSON into a window slot identified by the token. The Java side then polls for this stored result with a 10-second deadline. The helper methods buildEvalScript and buildPollScript are added to construct the injected scripts and handle result retrieval and clearing.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive No pull request description was provided by the author, making it impossible to assess relatedness to the changeset. Add a description explaining the motivation for async promise support, how it improves the WebView evaluation flow, and any relevant implementation details or testing notes.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: implementing async JavaScript promise support in WebView evaluation through token-based polling.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/webview-async-eval-promise

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@agents/android/java/WebViewAgent.java`:
- Around line 91-97: evaluateExpression currently only treats
JSONTokener(raw).nextValue() as valid when it's a String, which causes timeouts
if the bridge returns an unquoted object/array or if JSON.stringify inside
buildEvalScript throws; update evaluateExpression to accept JSONObject/JSONArray
(and any non-String tokenized result) by converting tokenized.toString() (or raw
when tokenized is not a String) into outcomeJson so the poll succeeds, and also
harden buildEvalScript so its Promise then/catch handlers each wrap
JSON.stringify in try/catch and always assign a string value to
window.__mwEval[token] (on both success and failure) to avoid leaving the slot
unset; reference evaluateExpression, buildPollScript, buildEvalScript,
AndroidBridge.evalJs, and JSONTokener in the change so you locate the spots to
modify.
- Around line 123-136: In WebViewAgent.buildEvalScript the injected body can
fail to parse before the outer try/catch and JSON.stringify errors inside the
then() success handler can escape the rejection handler, leaving
window.__mwEval[token] unwritten; to fix, compile the user body inside the
script try block (e.g., create the function via new Function(...)/Function.apply
inside the try so SyntaxError is caught) and make setting window.__mwEval[token]
resilient by wrapping JSON.stringify (or the assignment) in its own try/catch in
both fulfillment and rejection paths, and/or replace .then(success, failure)
with .then(success).catch(failure) so exceptions in the success handler are
forwarded to the common failure handler; update references to token and
window.__mwEval in buildEvalScript accordingly so every code path writes a JSON
string into window.__mwEval[token].
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 11f28dda-5589-4bba-ac77-a15f79ed2546

📥 Commits

Reviewing files that changed from the base of the PR and between e61ff5a and 367e707.

📒 Files selected for processing (1)
  • agents/android/java/WebViewAgent.java

Comment thread agents/android/java/WebViewAgent.java
Comment thread agents/android/java/WebViewAgent.java

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
agents/ios/bridge.m (1)

132-132: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Update timeout message to reflect the actual API in use.

The error message still references "evaluateJavaScript" but the code now uses callAsyncJavaScript.

📝 Proposed fix
-        return @{@"__error": @"evaluateJavaScript timed out"};
+        return @{@"__error": @"JavaScript evaluation timed out"};
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@agents/ios/bridge.m` at line 132, The timeout error message is stale — update
the returned error string (the @{@"__error": ...} return) so it references the
current API callAsyncJavaScript instead of evaluateJavaScript; locate the return
in the bridge.m function that returns @{@"__error": @"evaluateJavaScript timed
out"} and change the message to something like @"callAsyncJavaScript timed out"
(or a similarly accurate phrasing).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@agents/ios/bridge.m`:
- Line 132: The timeout error message is stale — update the returned error
string (the @{@"__error": ...} return) so it references the current API
callAsyncJavaScript instead of evaluateJavaScript; locate the return in the
bridge.m function that returns @{@"__error": @"evaluateJavaScript timed out"}
and change the message to something like @"callAsyncJavaScript timed out" (or a
similarly accurate phrasing).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 60564526-3d1c-4469-9eb8-f69d01977c8b

📥 Commits

Reviewing files that changed from the base of the PR and between 367e707 and 3df9efe.

📒 Files selected for processing (3)
  • agents/android/java/WebViewAgent.java
  • agents/ios/bridge.m
  • go.mod
🚧 Files skipped from review as they are similar to previous changes (1)
  • agents/android/java/WebViewAgent.java

@gmegidish gmegidish merged commit 0344e5d into main Jun 9, 2026
19 checks passed
@gmegidish gmegidish deleted the feat/webview-async-eval-promise branch June 9, 2026 11:09
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.

1 participant