Skip to content

Improved perspective handling in Vue useModel hook#592

Merged
lucksus merged 9 commits intodevfrom
vue-usemodel-hook-updates
Apr 8, 2025
Merged

Improved perspective handling in Vue useModel hook#592
lucksus merged 9 commits intodevfrom
vue-usemodel-hook-updates

Conversation

@jhweir
Copy link
Contributor

@jhweir jhweir commented Apr 4, 2025

The hook now accepts ComputedRef<PerspectiveProxy | null> for the perspective property so that we can pass in computed references instead of functions, enabling reactivity without triggering unnecessary new subscriptions.

The new use of the hook in a Vue component looks like this:

const { entries: channels } = useModel({
  perspective: computed(() => data.value.perspective),
  model: Channel,
});

Instead of the old pattern:

const { entries: channels } = useModel({
  perspective: () => data.value.perspective,
  model: Channel,
});

Additionally, the perspective and subject ensured watchers in the previous implementation have been merged

Summary by CodeRabbit

  • Refactor
    • Enhanced dynamic update logic for the perspective property to ensure prompt and reliable reflections of changes.
    • Improved management of the model query lifecycle, ensuring proper cleanup of existing queries before new ones are created.
    • Streamlined error handling and state management for a smoother, more stable user experience.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 4, 2025

Walkthrough

The changes update the handling of the perspective property in useModel.ts for both Vue and React. In Vue, the type signature for perspective is modified to support computed references, and the logic is refined to ensure proper value extraction and synchronization. A watcher is implemented to dynamically update the perspectiveRef. In React, a new reference, modelQueryRef, is introduced to manage the lifecycle of the ModelQueryBuilder instance, ensuring proper cleanup before creating new queries.

Changes

File Changes Summary
ad4m-hooks/vue/src/useModel.ts Updated perspective type from `PerspectiveProxy
ad4m-hooks/react/src/useModel.ts Introduced modelQueryRef using useRef to manage ModelQueryBuilder lifecycle, replaced modelQuery with modelQueryRef.current, and updated subscription logic for query management.

Sequence Diagram(s)

sequenceDiagram
    participant C as Component
    participant W as Watcher Logic
    participant P as PerspectiveRef
    participant S as Subscription

    C->>W: Provide perspective (static or computed)
    W->>P: Check & extract perspective value
    P-->>W: Return valid perspective value
    W->>S: Subscribe/update collection if value is valid
Loading

Possibly related PRs

Poem

I'm a little rabbit in the code land,
Hopping through changes so nimbly and grand.
With computed leaps and watchers in sight,
My perspective is clear, my jumps are light.
Cheers to updates, where logic stands true! 🐇✨

Happy coding, in fields anew!

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

🧹 Nitpick comments (3)
ad4m-hooks/react/src/useModel.ts (1)

50-80: Consider adding an effect cleanup for component unmount

While the code now properly disposes of queries before creating new ones, it would be good to also dispose of the query when the component unmounts.

 useEffect(() => {
   if (subjectEnsured) subscribeToCollection();
+  return () => {
+    if (modelQueryRef.current) {
+      modelQueryRef.current.dispose();
+      modelQueryRef.current = null;
+    }
+  };
 }, [subjectEnsured, model, JSON.stringify(query), pageNumber]);
ad4m-hooks/vue/src/useModel.ts (2)

56-56: Fix typo in function name

The function name "handleNewEntires" has a typo - it should be "handleNewEntries".

-function handleNewEntires(newEntries: T[]) {
+function handleNewEntries(newEntries: T[]) {

Don't forget to update all calls to this function as well.


27-27: Consider using a ref for better lifecycle management

Unlike the React version, this implementation uses a regular variable for the modelQuery instead of a Vue ref. Consider using a ref for better reactivity and lifecycle management.

-let modelQuery: ModelQueryBuilder<T|Ad4mModel> | null = null;
+const modelQuery = ref<ModelQueryBuilder<T|Ad4mModel> | null>(null);

Then update the usage:

-if (modelQuery) modelQuery.dispose();
+if (modelQuery.value) modelQuery.value.dispose();

-modelQuery =
+modelQuery.value =
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6eb5764 and 00ac9a3.

📒 Files selected for processing (2)
  • ad4m-hooks/react/src/useModel.ts (4 hunks)
  • ad4m-hooks/vue/src/useModel.ts (4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
ad4m-hooks/react/src/useModel.ts (1)
core/src/model/Ad4mModel.ts (2)
  • ModelQueryBuilder (876-1231)
  • Ad4mModel (273-854)
ad4m-hooks/vue/src/useModel.ts (2)
core/src/model/Ad4mModel.ts (2)
  • Ad4mModel (273-854)
  • ModelQueryBuilder (876-1231)
core/src/perspectives/PerspectiveProxy.ts (1)
  • PerspectiveProxy (282-1179)
🔇 Additional comments (10)
ad4m-hooks/react/src/useModel.ts (4)

28-28: Good addition of a ref for query lifecycle management

Adding a ref to track the ModelQueryBuilder instance is a good practice for properly managing its lifecycle across renders.


52-53: Properly cleaning up resources before creating new queries

This ensures proper disposal of the previous ModelQueryBuilder before creating a new one, which prevents memory leaks by cleaning up subscriptions.


54-58: Clean assignment of the query instance to the ref

The assignment of the ModelQueryBuilder to the ref is clear and maintains the same logic for creating either a class-based or string-based query.


62-66: Consistent use of ref for subscription operations

The code now consistently uses modelQueryRef.current for both paginated and non-paginated subscriptions, ensuring proper management of the query instance.

Also applies to: 71-72

ad4m-hooks/vue/src/useModel.ts (6)

5-5: Improved type signature for perspective property

The updated type signature now properly supports computed references, aligning with the PR objective of allowing direct use of computed properties for better reactivity handling.


29-43: Clean handling of perspective as a ref/computed or direct value

This implementation elegantly handles both direct perspective values and computed references, with a watcher that keeps perspectiveRef in sync with changes to the computed value.


69-73: Added guard clause for missing perspective

Good defensive programming practice to check for a valid perspective and return early if it's not available, preventing unnecessary operations and potential errors.


75-80: Proper cleanup of previous query resources

Similar to the React version, this code properly disposes of existing queries before creating new ones, preventing memory leaks from orphaned subscriptions.


112-134: Streamlined perspective watcher implementation

The revised watcher logic effectively merges the previous perspective and subject watchers, with improved handling of perspective changes and proper ensureSDNASubjectClass calls.


136-141: Refined query/page watcher with perspective check

The watcher now checks for a valid perspective before attempting to subscribe, preventing potential errors when perspective is null.

@lucksus lucksus merged commit dfc01dd into dev Apr 8, 2025
1 of 3 checks passed
@lucksus lucksus deleted the vue-usemodel-hook-updates branch August 22, 2025 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants