-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: propagate VarData.module_code into page module code #6456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
FarhanAliRaza
wants to merge
9
commits into
reflex-dev:main
Choose a base branch
from
FarhanAliRaza:app-module-code-vardata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b5e0f9d
feat: propagate VarData.module_code into page module code
FarhanAliRaza cbd6cf8
Update tests/integration/tests_playwright/test_var_module_code.py
FarhanAliRaza 03be1ef
Update tests/integration/tests_playwright/test_var_module_code.py
FarhanAliRaza c5b13ec
Merge branch 'main' into app-module-code-vardata
masenf 11744e6
Merge remote-tracking branch 'upstream/main' into app-module-code-var…
FarhanAliRaza 152eedc
feat: add VarData.module_code so Vars can emit page module-level JS
FarhanAliRaza 3d49dac
perf: reuse hook fetches and cache Var module_code during compile
FarhanAliRaza bc7ff54
Merge branch 'main' into app-module-code-vardata
FarhanAliRaza b39fb38
Merge branch 'app-module-code-vardata' of https://github.com/FarhanAl…
FarhanAliRaza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| The page compiler now propagates `VarData.module_code` into the compiled page module, emitting the module-level JS snippets contributed by a page's Vars (and hook `VarData`) alongside its components' custom code. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Added a `module_code` field to `VarData`, letting a Var carry module-level JS snippets (top-of-file helpers and constants). Snippets are de-duplicated when var data is merged and exposed through `Component._iter_var_module_code` for the compiler to emit into the page module. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -192,8 +192,18 @@ def leave_component( | |
|
|
||
| self._collect_component_custom_code(page_context.module_code, comp) | ||
|
|
||
| # Fetch once and reuse for both the module_code scan and page-hook | ||
| # aggregation; _get_added_hooks is uncached, so a second call recomputes. | ||
| hooks_internal = comp._get_hooks_internal() | ||
| added_hooks = comp._get_added_hooks() | ||
| self._collect_var_module_code( | ||
| page_context.module_code, comp, hooks_internal, added_hooks | ||
| ) | ||
|
|
||
| if not in_prop_tree: | ||
| self._collect_component_hooks(page_context.hooks, comp) | ||
| self._collect_component_hooks( | ||
| page_context.hooks, comp, hooks_internal, added_hooks | ||
| ) | ||
|
|
||
| if ( | ||
| type(comp)._get_app_wrap_components | ||
|
|
@@ -252,6 +262,7 @@ def _compiler_bind_leave_component( | |
| extend_imports = self._extend_imports | ||
| collect_component_hooks = self._collect_component_hooks | ||
| collect_component_custom_code = self._collect_component_custom_code | ||
| collect_var_module_code = self._collect_var_module_code | ||
| collect_app_wrap_components = self._collect_app_wrap_components | ||
| base_get_app_wrap_components = Component._get_app_wrap_components | ||
| seen_app_wrap_methods: set[object] = set() | ||
|
|
@@ -270,8 +281,14 @@ def leave_component( | |
|
|
||
| collect_component_custom_code(module_code, comp) | ||
|
|
||
| # Fetch once and reuse for both the module_code scan and page-hook | ||
| # aggregation; _get_added_hooks is uncached. | ||
| hooks_internal = comp._get_hooks_internal() | ||
| added_hooks = comp._get_added_hooks() | ||
| collect_var_module_code(module_code, comp, hooks_internal, added_hooks) | ||
|
|
||
| if not in_prop_tree: | ||
| collect_component_hooks(hooks, comp) | ||
| collect_component_hooks(hooks, comp, hooks_internal, added_hooks) | ||
|
|
||
| app_wrap_method = type(comp)._get_app_wrap_components | ||
| if ( | ||
|
|
@@ -295,12 +312,19 @@ def leave_component( | |
| def _collect_component_hooks( | ||
| page_hooks: dict[str, VarData | None], | ||
| component: Component, | ||
| hooks_internal: dict[str, VarData | None], | ||
| added_hooks: dict[str, VarData | None], | ||
| ) -> None: | ||
| """Collect hooks for one structural-tree component in legacy order.""" | ||
| page_hooks.update(component._get_hooks_internal()) | ||
| """Add one structural-tree component's hooks in legacy order. | ||
|
|
||
| ``hooks_internal`` and ``added_hooks`` are passed in (already fetched for | ||
| the module_code scan) rather than re-fetched, so each component pays a | ||
| single uncached ``_get_added_hooks``. | ||
| """ | ||
| page_hooks.update(hooks_internal) | ||
| if (user_hooks := component._get_hooks()) is not None: | ||
| page_hooks[user_hooks] = None | ||
| page_hooks.update(component._get_added_hooks()) | ||
| page_hooks.update(added_hooks) | ||
|
|
||
| @staticmethod | ||
| def _extend_imports( | ||
|
|
@@ -329,6 +353,34 @@ def _collect_component_custom_code( | |
| for item in clz.add_custom_code(component): | ||
| module_code[item] = None | ||
|
|
||
| @staticmethod | ||
| def _collect_var_module_code( | ||
| module_code: dict[str, None], | ||
| component: Component, | ||
| hooks_internal: dict[str, VarData | None], | ||
| added_hooks: dict[str, VarData | None], | ||
| ) -> None: | ||
| """Collect module_code from this component's Vars and pre-fetched hooks. | ||
|
|
||
| ``hooks_internal`` and ``added_hooks`` are the dicts already fetched for | ||
| page-hook aggregation, reused here so each component avoids a second | ||
| uncached ``_get_added_hooks``. Per-component contract — the walker | ||
| re-enters each prop subtree with ``in_prop_tree=True`` so this helper | ||
| does not recurse, mirroring :meth:`_collect_component_custom_code`. | ||
| """ | ||
| for snippet in component._iter_var_only_module_code(): | ||
| module_code.setdefault(snippet, None) | ||
| for hook_var_data in hooks_internal.values(): | ||
| if hook_var_data is None: | ||
| continue | ||
| for snippet in hook_var_data.module_code: | ||
| module_code.setdefault(snippet, None) | ||
| for hook_var_data in added_hooks.values(): | ||
| if hook_var_data is None: | ||
| continue | ||
| for snippet in hook_var_data.module_code: | ||
| module_code.setdefault(snippet, None) | ||
|
Comment on lines
+371
to
+382
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. very similar code in Component too, i don't think we want this kind of thing in both places. i'd rather have this combining stuff in the compiler and get most of the weird recursive collection stuff out of the Component so it can be a very simple class. |
||
|
|
||
| def _collect_app_wrap_components( | ||
| self, | ||
| page_app_wrap_components: dict[tuple[int, str], Component], | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this feels like it should be a "
_get_all_var_data()" because now that it also collects the module code.i think ultimately we want to simplify how this stuff is stored, combined, and passed around by leveraging the var system.
during the component walk we could grab all of a component's var's data, cache that, and then reuse it in a Var-wrapped literal component or vice versa.
but more specifically, this function has a yucky side effect of populating just a module cache now. if we're going to cache, we should cache a more reusable and standard VarData