Repository navigation
Conversation
Hosts and components only receive the `() => Promise<void>` closure for an action prop, so they cannot tell an event action from a function call action before invoking it (to show a spinner only for server events, or for analytics). Attach the unresolved action to the closure as `rawAction`. Fixes a2ui-project#3780
There was a problem hiding this comment.
Code Review
This pull request exposes the unresolved action on action closures as a rawAction property in GenericBinder, allowing hosts to inspect the action before invoking it. A corresponding unit test and changelog entry have been added. The feedback suggests using Object.assign to attach the property to the closure in a cleaner, more type-safe manner instead of using a double type assertion.
| }; | ||
| // Lets hosts inspect the unresolved action (event or function call) | ||
| // before invoking it, for example to show a spinner only for server events. | ||
| (closure as unknown as Record<string, unknown>)['rawAction'] = value; |
There was a problem hiding this comment.
Using a double type assertion (as unknown as Record<string, unknown>) and string-based property access is less idiomatic in TypeScript. We can use Object.assign to attach the property to the closure in a cleaner, more type-safe manner.
| (closure as unknown as Record<string, unknown>)['rawAction'] = value; | |
| Object.assign(closure, {rawAction: value}); |
# Conflicts: # typescript/core/web_core/CHANGELOG.md
|
Hi @jacobsimionato, this attaches the raw action to the action closures as |
# Conflicts: # typescript/core/web_core/CHANGELOG.md
|
From triage bot, analysis: For humansTL;DR Summary: Targeted issues: Why flagged for triage: Suggestion:
To adjust the analysis and apply its suggestion, run in your a2ui checkout: tools/internal/triage/triage_ui.sh https://github.com/a2ui-project/a2ui/pull/4055For softwareThis analysis is auto-created to help humans to triage this item. Please, do not treat it as a team decision. 🤖 Agent-generated |
|
Thanks for the PR. Assigning it to |
# Conflicts: # typescript/core/web_core/CHANGELOG.md
Declare rawAction in the type of an action property instead of leaving it reachable only through a cast. ActionClosure is the callable the binder already returned plus an optional rawAction. It is optional so a plain function, such as a test double, is still assignable to an action property.
|
Thanks @jacobsimionato. On the question of whether I made
|
# Conflicts: # typescript/core/web_core/CHANGELOG.md
|
Thank you for your contribution! I'm afraid I looked at #3780 again and I now don't think it's a good idea, for reasons I posted on the issue! So I'll close that issue and this PR. Sorry to mess you around here! |
Description
GenericBinderturns an action prop into an(options?) => Promise<void>closure, and that closure is all a host or component receives. It cannot tell an event action from a function call action before invoking it, for example to show a loading state only for a server event, or to record analytics.As proposed in the issue,
bindActionnow attaches the unresolved action to the closure asrawAction. The value is the action as the component declared it (bindings such as{path: '/user/name'}are not resolved). Cached closures are only reused when the action isjsonEqualsto the cached one, sorawActionalways matches the current action. The action prop is now typed as a new exportedActionClosure: the same callable as before plus an optionalrawAction, so reading it needs no cast. It is optional in the type so a plain function, such as a test double, is still assignable to an action property;GenericBinderalways sets it.Verification:
generic-binder.test.tsbinds an event action with a data binding in its context and assertsrawActiondeep-equals the declared action.yarn testintypescript/core/web_core: 1659 passed, 0 failed.@a2ui/reactand@a2ui/litstill build.Fixes #3780
Pre-launch Checklist
One time:
For this PR: