-
Notifications
You must be signed in to change notification settings - Fork 587
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
Spotlight cleanup #5015
Spotlight cleanup #5015
Conversation
WalkthroughThe pull request introduces several modifications across multiple files in the Changes
Possibly related PRs
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
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.
Actionable comments posted: 4
🧹 Outside diff range and nitpick comments (5)
app/packages/spotlight/src/types.ts (1)
60-60
: Consider adding JSDoc documentation for the destroy method.Adding documentation would help clarify:
- When the destroy method is called
- Whether it should be idempotent
- What resources it's expected to clean up
Example documentation:
+/** + * Cleanup resources associated with an item + * @param id The ID of the item to cleanup + */ destroy?: (id: ID) => void;app/packages/spotlight/src/row.ts (1)
Line range hint
51-63
: Add proper TypeScript event typingThe event parameter should be properly typed for better type safety.
- const handler = (event) => { + const handler = (event: MouseEvent) => {app/packages/spotlight/src/section.ts (1)
111-115
: Consider adding safeguards against post-destruction usage.The current destroy implementation could benefit from additional architectural safeguards:
- Add a destroyed flag to prevent reuse of destroyed instances:
class Section<K, V> { + #destroyed = false; + destroy() { + if (this.#destroyed) return; + this.#destroyed = true; this.#section.remove(); // ... rest of destroy implementation } + + #assertNotDestroyed() { + if (this.#destroyed) { + throw new Error('Section has been destroyed'); + } + } }
- Add checks in key methods:
render(...) { this.#assertNotDestroyed(); // ... existing implementation } next(...) { this.#assertNotDestroyed(); // ... existing implementation }This ensures:
- Safe cleanup by preventing multiple destroy calls
- Early detection of usage after destruction
- Clear error messages for debugging
app/packages/spotlight/src/index.ts (2)
111-112
: LGTM: Improved cleanup with proper null checks.The change from
remove()
todestroy()
aligns with the PR's cleanup objectives. The optional chaining operator (?.
) properly handles potential null references.Consider adding a comment explaining the cleanup sequence, especially since this is a critical cleanup method:
destroy(): void { if (!this.attached) { console.error("spotlight is not attached"); return; } + // Clean up sections before removing the element this.#backward?.destroy(); this.#forward?.destroy();
275-275
: LGTM: Consistent cleanup in backward navigation.The change to
destroy()
maintains consistency with the new cleanup pattern while properly handling section swapping during backward navigation.Consider adding error handling for the destroy operation:
- this.#forward.destroy(); + try { + this.#forward.destroy(); + } catch (error) { + console.error('Failed to destroy forward section:', error); + // Continue with section swap despite cleanup failure + }
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
📒 Files selected for processing (5)
app/packages/spotlight/src/createScrollReader.ts
(2 hunks)app/packages/spotlight/src/index.ts
(4 hunks)app/packages/spotlight/src/row.ts
(5 hunks)app/packages/spotlight/src/section.ts
(1 hunks)app/packages/spotlight/src/types.ts
(1 hunks)
🧰 Additional context used
📓 Path-based instructions (5)
app/packages/spotlight/src/createScrollReader.ts (1)
Pattern **/*.{ts,tsx}
: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.
app/packages/spotlight/src/index.ts (1)
Pattern **/*.{ts,tsx}
: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.
app/packages/spotlight/src/row.ts (1)
Pattern **/*.{ts,tsx}
: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.
app/packages/spotlight/src/section.ts (1)
Pattern **/*.{ts,tsx}
: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.
app/packages/spotlight/src/types.ts (1)
Pattern **/*.{ts,tsx}
: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.
🔇 Additional comments (9)
app/packages/spotlight/src/types.ts (1)
60-61
: LGTM! The destroy method is a good addition for resource cleanup.
The optional destroy method is well-typed and integrates cleanly with the existing interface structure.
app/packages/spotlight/src/createScrollReader.ts (4)
18-20
: LGTM! Clean extraction of scroll handler.
The named function improves code readability and maintains the same functionality.
23-26
: LGTM! Well-structured scrollEnd handler.
The named function improves maintainability while correctly handling the scroll end state and render scheduling.
74-75
: LGTM! Comprehensive event listener cleanup.
The destroy method now properly removes both event listeners using the named function references, preventing potential memory leaks.
77-77
: LGTM! Proper TypeScript style.
The addition of a trailing comma follows TypeScript best practices and improves git diff readability for future changes.
app/packages/spotlight/src/row.ts (2)
16-16
: LGTM: Good use of AbortController for event listener cleanup
The addition of #aborter
follows TypeScript best practices for managing event listener lifecycles.
65-69
: Verify shared handler for click and contextmenu events
The same handler is used for both click and contextmenu events. Please verify if this is intentional and if both events should trigger the same onItemClick
behavior.
✅ Verification successful
Let me check the handler implementation and its usage to better understand the behavior.
Let me check the implementation in the flashlight package for comparison.
Shared handler for click and contextmenu events is intentional and correct
The shared handler implementation is consistent across both packages (spotlight and flashlight). In both cases:
- Both events trigger the same item click behavior
- Both handlers prevent default event behavior when needed
- Both implementations check for modifier keys (meta/shift) or call the click handler directly
- This pattern ensures consistent behavior for both left-click and right-click interactions
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Search for other contextmenu handlers in the codebase to verify consistent behavior
rg -A 5 "addEventListener.*contextmenu"
Length of output: 829
Script:
#!/bin/bash
# Get the full context of the row.ts file to understand the handler implementation
cat app/packages/spotlight/src/row.ts
Length of output: 5493
Script:
#!/bin/bash
# Get the full context of the section.ts file to understand its handler implementation
cat app/packages/flashlight/src/section.ts
Length of output: 6666
app/packages/spotlight/src/section.ts (1)
111-115
: Verify event listener cleanup.
The destroy method removes DOM elements but doesn't explicitly handle event listener cleanup. Let's verify if there are any event listeners that need cleanup:
If event listeners are found, they should be properly cleaned up in the destroy method.
app/packages/spotlight/src/index.ts (1)
21-21
: LGTM: Constant import addition is correct.
The ZOOMING_COEFFICIENT constant is properly imported and utilized in the zoom calculation logic.
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.
lgtm 🚀
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.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (1)
app/packages/spotlight/src/row.ts (1)
Line range hint
51-69
: Consider adding TypeScript types for the event parameter.The event handler implementation looks good with proper key checks and prevention of default behavior. However, the event parameter could benefit from explicit typing.
- const handler = (event) => { + const handler = (event: MouseEvent) => {
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
📒 Files selected for processing (1)
app/packages/spotlight/src/row.ts
(6 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
app/packages/spotlight/src/row.ts (1)
Pattern **/*.{ts,tsx}
: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.
📓 Learnings (1)
app/packages/spotlight/src/row.ts (1)
Learnt from: tom-vx51
PR: voxel51/fiftyone#5015
File: app/packages/spotlight/src/row.ts:122-125
Timestamp: 2024-10-31T16:22:03.523Z
Learning: In the `Row` class of `app/packages/spotlight/src/row.ts`, ensure that the `destroy` method includes error handling to prevent memory leaks due to cleanup errors.
🔇 Additional comments (4)
app/packages/spotlight/src/row.ts (4)
16-16
: LGTM: Good use of AbortController for event listener cleanup.
The addition of a readonly AbortController field provides a clean way to manage event listener lifecycles.
122-125
: LGTM: Clean implementation of the destroy method.
The method properly handles cleanup by first destroying items and then aborting event listeners.
142-142
: Duplicate cleanup concern already addressed in previous review.
231-249
: LGTM: Robust implementation with proper error handling.
The implementation includes:
- Null check for optional destroy method
- Try-catch blocks to prevent cleanup interruption
- Error aggregation and logging
This aligns well with the previous feedback about preventing memory leaks.
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.
Tooltip is not being displayed. It is on dev.fiftyone.ai.
What changes are proposed in this pull request?
Adds event listener cleanup to
Spotlight
. Also addsdestroy
as an optional config for item cleanup, e.g. a Looker instanceWhat areas of FiftyOne does this PR affect?
fiftyone
Python library changesSummary by CodeRabbit
New Features
destroy
method in theRow
andSection
classes for improved resource management.destroy
method to theSpotlightConfig
interface.Improvements
Spotlight
class.Section
instances to ensure proper resource release.