-
Notifications
You must be signed in to change notification settings - Fork 1.7k
ENG-8034: fix window events inside of memoization leaf #5899
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
ENG-8034: fix window events inside of memoization leaf #5899
Conversation
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.
Greptile Overview
Summary
Fixed memoization issue where WindowEventListener event handlers weren't properly memoized when used inside memoized components.
Key Changes:
- Added
hooksfield to store memoized event trigger hooks fromStatefulComponent._fix_event_triggers() - Overrode
create()method to call_fix_event_triggers()and store resulting hooks - Modified
add_hooks()to merge stored memoized hooks with generated useEffect hooks - Changed event handler wrapping in useEffect:
const fn = {Var.create(event_trigger)}ensures proper variable creation - Removed
_var_type="str"from Var creation in hooks (now inferred correctly)
How It Works:
When WindowEventListener is created, _fix_event_triggers() processes all event handlers through StatefulComponent's memoization logic, generating useCallback hooks. These are stored and then merged with the useEffect hooks that register window event listeners, ensuring stable function references across re-renders.
Confidence Score: 4/5
- Safe to merge with minimal risk - fix follows established patterns
- Implementation correctly applies the memoization pattern used elsewhere in the codebase (Upload, Match components). The fix properly wraps event triggers using Var.create() and stores memoized hooks. Minor concern is the change from explicit _var_type to inferred typing, but this aligns with the framework's typing system.
- No files require special attention
Important Files Changed
File Analysis
| Filename | Score | Overview |
|---|---|---|
| reflex/components/core/window_events.py | 4/5 | Adds memoization support for window event listeners by storing memoized event trigger hooks and properly wrapping event handlers in useCallback |
| pyi_hashes.json | 5/5 | Updates hash for window_events.pyi to reflect changes to the module's type definitions |
Sequence Diagram
sequenceDiagram
participant User as Developer
participant WEL as WindowEventListener
participant SC as StatefulComponent
participant Compiler as Compiler
participant Browser as Runtime
User->>WEL: create with event handler
WEL->>SC: _fix_event_triggers
SC->>SC: Generate memoized triggers
SC-->>WEL: Return hooks list
WEL->>WEL: Store hooks
WEL-->>User: Component ready
Compiler->>WEL: add_hooks
WEL->>WEL: Merge stored hooks
WEL->>WEL: Generate useEffect for events
WEL-->>Compiler: Return hooks
Compiler->>Browser: Emit React code
Browser->>Browser: useCallback executed
Browser->>Browser: useEffect executed
Browser->>Browser: addEventListener
2 files reviewed, no comments
CodSpeed Performance ReportMerging #5899 will create unknown performance changesComparing Summary
Footnotes
|
lilpomm
left a comment
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.
Gracias por su comprensión
No description provided.