Commit a084b58
Support FunctionVar handlers in EventChain rendering (#6188)
* Support FunctionVar handlers in EventChain rendering
Allow EventChain to accept frontend FunctionVar handlers alongside
EventSpec, EventVar, and EventCallback values.
When a chain contains FunctionVars, keep backend events grouped through
addEvents(...) and invoke frontend functions inline with the trigger
arguments so mixed chains preserve execution order and DOM event
actions like preventDefault and stopPropagation.
Wrap inline arrow functions before emitting VarOperationCall JS so
direct invocation renders valid JavaScript, add unit coverage for
pure/mixed event-chain formatting and creation, and move upload
exception docs to the helper that actually raises them to satisfy
darglint.
* fix: forward non-DOM event actions to queued events and improve arrow function detection
Event actions like `throttle` were dropped when rendering mixed EventChains
containing both backend handlers and FunctionVars. Non-DOM actions are now
correctly forwarded to the queueEvents call. Also fixes false-positive arrow
function detection for expressions like `factory(() => 1)` that contain `=>`
but are not themselves arrow functions, and adds warnings when event_chain_kwargs
are silently ignored for EventChainVar/FunctionVar values.
* refactor: extract applyEventActions helper and simplify EventChain rendering
Move event action handling (preventDefault, stopPropagation, throttle,
debounce, temporal) into a shared applyEventActions JS function used by
both addEvents and Python-generated event chains. This eliminates the
complex queueable-group batching logic in LiteralEventChainVar and the
arrow function detection heuristic in FunctionVar.
* fix: avoid wrapping JS operators as callable expressions
Restore selective arrow-function wrapping in FunctionVar calls so
operator-like expressions such as `typeof` keep compiling correctly.
This fixes the generated JSX regression that broke form integration tests
by emitting invalid code like `((typeof)(value))`.
Also update unit expectations for the corrected rendering behavior.
* test: fix test regression
* perf: fast-path backend-only EventChains to skip applyEventActions wrapper
* test: simplify test
* fix: allow EventChain-typed FunctionVars to compose with event_chain_kwargs
* fix: remove EventChain fast path to preserve per-spec event actions
The fast path grouped backend-only EventSpecs into a single addEvents
call, which lost per-spec event actions like individual debounce values.
Each EventSpec now renders its own addEvents call, and chain-level actions
use applyEventActions consistently.
* use Var.equals for var equality checking
Without this, using `==` in an assertion with Var values ends up creating a new
truthy Var instead of actually checking python-side equality. Var equality
assertions have to be made explicitly with `.equals` to actually determine if
the values are the same.
* Handle args_spec partial args for FunctionVar
When a FunctionVar is passed to an event trigger with a given args_spec,
transform it into a partial function that applies the transformed arguments
when called. This allows FunctionVar handlers to work with on_blur and
on_submit, which, by default, use the args_spec to transform the value before
passing it off to the handler.
Some escape hatches:
* The behavior for EventChainVar is unchanged, so previous code that was
explicitly casting functions to EventChain will continue to work without
modification.
* If the FunctionVar is returned through a lambda, no partial application is
applied, because that happens at the point the lambda is called, so the
return value of the lambda is responsible for mapping the arguments if
desired.
This change also allows event handler lambda functions to return a
heterogeneous mix of EventSpec/EventHandler/FunctionVar (and EventChain
returned from lambda are treated as FunctionVar, allowing arbitrary nesting).
Update FunctionVar.partial such that passing no args does NOT create a new
useless function.
* treat outer lambda and lambda in list equivalently
* test: add count
* add EventVar to allowed event lambda return types
this allows rx.cond to continue to be used for conditional events
* fix: hoist empty event literals and drop redundant _get_all_var_data in statement block
Cache empty event/action literals as module-level constants to avoid
repeated allocations. Remove the manual _get_all_var_data merge on
the statement block Var so nested var data propagates naturally through
the f-string interpolation.
* test: Addded more tests
---------
Co-authored-by: Masen Furer <m_github@0x26.net>1 parent 06e733a commit a084b58
File tree
10 files changed
+724
-142
lines changed- reflex
- .templates/web/utils
- constants
- vars
- tests/units
- components
- utils
10 files changed
+724
-142
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
726 | 726 | | |
727 | 727 | | |
728 | 728 | | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
729 | 776 | | |
730 | 777 | | |
731 | 778 | | |
| |||
898 | 945 | | |
899 | 946 | | |
900 | 947 | | |
901 | | - | |
902 | | - | |
903 | | - | |
904 | | - | |
905 | | - | |
906 | | - | |
907 | | - | |
908 | | - | |
909 | 948 | | |
910 | 949 | | |
911 | 950 | | |
912 | 951 | | |
913 | 952 | | |
914 | 953 | | |
915 | | - | |
916 | | - | |
917 | | - | |
918 | | - | |
919 | | - | |
920 | | - | |
921 | | - | |
922 | | - | |
923 | | - | |
924 | | - | |
925 | | - | |
926 | | - | |
927 | | - | |
928 | | - | |
929 | | - | |
930 | | - | |
931 | | - | |
932 | | - | |
933 | | - | |
934 | | - | |
935 | | - | |
936 | | - | |
937 | | - | |
938 | | - | |
939 | | - | |
940 | | - | |
941 | | - | |
942 | | - | |
943 | | - | |
944 | | - | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
945 | 957 | | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
946 | 966 | | |
947 | 967 | | |
948 | 968 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1936 | 1936 | | |
1937 | 1937 | | |
1938 | 1938 | | |
1939 | | - | |
1940 | | - | |
1941 | | - | |
1942 | | - | |
1943 | | - | |
1944 | 1939 | | |
1945 | 1940 | | |
1946 | 1941 | | |
| |||
1965 | 1960 | | |
1966 | 1961 | | |
1967 | 1962 | | |
| 1963 | + | |
| 1964 | + | |
| 1965 | + | |
| 1966 | + | |
| 1967 | + | |
1968 | 1968 | | |
1969 | 1969 | | |
1970 | 1970 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| 64 | + | |
| 65 | + | |
64 | 66 | | |
65 | 67 | | |
66 | 68 | | |
| |||
128 | 130 | | |
129 | 131 | | |
130 | 132 | | |
131 | | - | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
132 | 137 | | |
133 | 138 | | |
134 | 139 | | |
| |||
0 commit comments