satoren / y_ex

Yjs port for Elixir
MIT License
56 stars 2 forks source link

fix: When unobserve in shared_doc, remove awareness of the same origin #100

Closed satoren closed 1 week ago

satoren commented 1 week ago

Summary by CodeRabbit

coderabbitai[bot] commented 1 week ago

Walkthrough

The changes in this pull request introduce a new state management feature to the Yex.Sync.SharedDoc module, specifically through the addition of an origin_client_map to track clients based on their origin. This update affects the handling of client awareness, with modifications to various handle_call functions to manage this new state. The Yex.DocServer.Worker module has also been updated to process awareness messages synchronously. Additionally, the Yex.Sync.SharedDocTest module has been enhanced to include new tests for awareness functionality and persistence behavior.

Changes

File Change Summary
lib/protocols/shared_doc.ex - Added origin_client_map in init/2
- Introduced remove_awareness_clients_by_origin/2
- Introduced update_origin_client_map/3
lib/server/doc_server_worker.ex - Updated handle_cast for :document_update and :awareness_update for immediate processing
- Added new handle_info clauses for awareness and document updates
- Introduced handle_update_v1_immediately/1 and handle_awareness_change_immediately/1
test/protocols/shared_doc_test.exs - Added Awareness alias
- Restructured tests for observing and unobserving documents
- Introduced new persistence tests with PersistenceTest and PersistenceFileWriteTest

Possibly related PRs

🐰 In the meadow where we hop and play,
New maps of clients lead the way.
With awareness bright, we track with glee,
Each change and update, as clear as can be.
So let’s celebrate with a joyful cheer,
For shared documents now hold us near! πŸ‡βœ¨

[!WARNING]

Rate limit exceeded

@satoren has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 1 minutes and 27 seconds before requesting another review.

βŒ› How to resolve this issue? After the wait time has elapsed, a review can be triggered using the `@coderabbitai review` command as a PR comment. Alternatively, push new commits to this PR. We recommend that you space out your commits to avoid hitting the rate limit.
🚦 How do rate limits work? CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our [FAQ](https://docs.coderabbit.ai/faq) for further information.
πŸ“₯ Commits Reviewing files that changed from the base of the PR and between 541ef5ebf66de14d08f5ecdc2ec43b1027abe190 and 8a285fde13ab7b6dce06687fabeaa16001143a3a.

πŸ“œ Recent review details **Configuration used: CodeRabbit UI** **Review profile: CHILL**
πŸ“₯ Commits Reviewing files that changed from the base of the PR and between 541ef5ebf66de14d08f5ecdc2ec43b1027abe190 and 8a285fde13ab7b6dce06687fabeaa16001143a3a.
πŸ“’ Files selected for processing (1) * `lib/protocols/shared_doc.ex` (3 hunks)
πŸ”‡ Additional comments (5)
lib/protocols/shared_doc.ex (5) `110-111`: **LGTM: State initialization is correct** The new state fields are properly initialized as empty maps within the assign call. --- `139-145`: **LGTM: Clean awareness state removal implementation** The awareness state cleanup is properly implemented when an observer is removed. The code correctly: 1. Removes awareness states for all clients from the given origin 2. Updates the origin_client_map by removing the origin entry --- `179-186`: **LGTM: Improved error handling and state management** Good improvements to the awareness change handling: 1. Better error handling with proper logging 2. Clean state management using the pipe operator --- `190-212`: **LGTM: Correct implementation of origin client map updates** The implementation properly handles: 1. Special case for nil origin 2. Combining new and existing clients 3. Removing specified clients 4. Maintaining uniqueness of client IDs The previous logical error has been fixed by using `Enum.reject` instead of `Enum.filter`. --- Line range hint `139-212`: **Verify integration with awareness system** The changes look good, but let's verify the integration with the rest of the awareness system.
βœ… Verification successful Let me gather more specific information about the awareness system integration. --- **Integration with awareness system is properly implemented** The verification shows that the awareness cleanup implementation is well-integrated: - The `remove_awareness_clients_by_origin` function correctly uses `Awareness.remove_states` which is properly implemented in the awareness module - The awareness state removal is handled at the NIF level through `awareness_remove_states` - The cleanup is tied to the origin-client mapping, ensuring proper client state cleanup when origins are removed - The implementation follows the same pattern as other awareness-related operations in the codebase
🏁 Scripts executed The following scripts were executed for the analysis: Script: ```shell #!/bin/bash # Description: Check for potential integration points with the awareness system # Look for other files that might interact with awareness states echo "Files potentially interacting with awareness:" rg -l "awareness" --type elixir # Look for tests covering the new awareness cleanup functionality echo -e "\nTests covering awareness cleanup:" rg -l "test.*awareness.*cleanup|test.*cleanup.*awareness" --type elixir # Look for other origin-based operations echo -e "\nOther origin-based operations:" rg "def.*origin" --type elixir ``` Length of output: 2697 --- Script: ```shell #!/bin/bash # Check awareness-related function implementations and their interactions # Look for awareness state handling in the awareness module echo "Awareness state handling implementation:" ast-grep --pattern 'def apply_update($$$)' lib/protocols/awareness.ex # Check for awareness cleanup tests echo -e "\nAwareness cleanup tests:" rg -B2 -A5 "test.*awareness.*cleanup|describe.*awareness.*cleanup" test/protocols/shared_doc_test.exs test/protocols/awareness_test.exs # Check for other awareness state removal handling echo -e "\nOther awareness state removal handling:" rg -B2 -A2 "remove_states|remove_awareness" --type elixir ``` Length of output: 2697

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?

❀️ Share - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
πŸͺ§ Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit , 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 resolve` resolve all the CodeRabbit review comments. - `@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](https://docs.coderabbit.ai/guides/configure-coderabbit) 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](https://docs.coderabbit.ai) for detailed information on how to use CodeRabbit. - Join our [Discord Community](http://discord.gg/coderabbit) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.
coveralls commented 1 week ago

Coverage Status

coverage: 94.493% (+0.01%) from 94.479% when pulling 8a285fde13ab7b6dce06687fabeaa16001143a3a on cleanup_awareness_when_down_or_unobserve into a23ce60c66bcfbe80b6a3accfd3aae92275cecbd on main.