Skip to content
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

Add presence to RealtimeChannelProtocol #212

Closed

Conversation

lawrence-forooghian
Copy link
Collaborator

@lawrence-forooghian lawrence-forooghian commented Jan 16, 2025

I'm going to revert ably-cocoa's commit 26d9bf7, which added the presence member to ARTRealtimeChannelProtocol, since it broke backwards compatibility.

Summary by CodeRabbit

  • New Features

    • Added support for more flexible channel presence management
    • Introduced a new RealtimePresenceProtocol to enhance presence-related operations
  • Refactor

    • Updated multiple protocols and classes to use existential types for channel properties
    • Improved type flexibility for channel and presence management
  • Chores

    • Added mock implementations for presence-related functionality
    • Enhanced test infrastructure with new mock classes and methods

I'm going to revert ably-cocoa's commit 26d9bf7, which added the
`presence` member to ARTRealtimeChannelProtocol, since it broke
backwards compatibility.
Copy link

coderabbitai bot commented Jan 16, 2025

Walkthrough

This pull request introduces a series of modifications across multiple files to enhance type flexibility by changing the channel property from a concrete RealtimeChannelProtocol to an existential type any RealtimeChannelProtocol. Additionally, the changes include the introduction of a new RealtimePresence protocol and mock implementations, which provide a more structured approach to presence management in the Ably Chat library.

Changes

File Change Summary
Example/AblyChatExample/Mocks/MockClients.swift Updated channel property type to any RealtimeChannelProtocol in multiple mock actors
Example/AblyChatExample/Mocks/MockRealtime.swift Added new RealtimePresence class with comprehensive presence management methods
Sources/AblyChat/Dependencies.swift Introduced RealtimePresenceProtocol and updated RealtimeChannelProtocol with an associated Presence type
Multiple Source Files Changed channel property type to any RealtimeChannelProtocol in various protocols and classes
Tests/AblyChatTests/Mocks/MockRealtimePresence.swift Added MockRealtimePresence class with placeholder implementations

Sequence Diagram

sequenceDiagram
    participant Channel
    participant Presence
    
    Channel->>Presence: Initialize presence
    Presence-->>Channel: Presence instance created
    
    Channel->>Presence: Perform presence operations
    Presence-->>Channel: Handle presence actions
Loading

Possibly related PRs

Suggested reviewers

  • umair-ably
  • maratal

Poem

🐰 A Rabbit's Type Adventure 🌈

Existential types now dance with glee,
any brings flexibility, can't you see?
Channels and presence, a protocol's delight,
Mocking and testing, everything's just right!
Code hops forward with elegant grace! 🚀


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
🪧 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. (Beta)
  • @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 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

@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: 2

🧹 Nitpick comments (3)
Example/AblyChatExample/Mocks/MockRealtime.swift (2)

108-108: Provide mock implementations for presence property to prevent runtime crashes during testing.

Currently, the presence property in Channel is instantiated with RealtimePresence(), whose methods raise fatalError("Not implemented"). This could lead to unexpected crashes during testing if any presence methods are called. Consider providing mock implementations or stubs for these methods to enhance test stability.


251-345: Implement default or mocked methods in RealtimePresence to improve test reliability.

All methods in RealtimePresence currently use fatalError("Not implemented"), which will cause a crash if these methods are invoked during testing. Providing minimal implementations or mock responses can prevent runtime exceptions and allow tests to proceed smoothly.

Sources/AblyChat/Dependencies.swift (1)

32-33: Consider adding documentation for protocol requirements.

The protocol lacks documentation explaining the expected behavior and requirements for conforming types. Consider adding documentation similar to other protocols in the file.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f2024f3 and d1d06b7.

📒 Files selected for processing (13)
  • Example/AblyChatExample/Mocks/MockClients.swift (4 hunks)
  • Example/AblyChatExample/Mocks/MockRealtime.swift (2 hunks)
  • Sources/AblyChat/AblyCocoaExtensions/Ably+Dependencies.swift (1 hunks)
  • Sources/AblyChat/Dependencies.swift (1 hunks)
  • Sources/AblyChat/Messages.swift (1 hunks)
  • Sources/AblyChat/Occupancy.swift (1 hunks)
  • Sources/AblyChat/Room.swift (1 hunks)
  • Sources/AblyChat/RoomFeature.swift (2 hunks)
  • Sources/AblyChat/RoomReactions.swift (1 hunks)
  • Sources/AblyChat/Typing.swift (1 hunks)
  • Tests/AblyChatTests/Mocks/MockFeatureChannel.swift (1 hunks)
  • Tests/AblyChatTests/Mocks/MockRealtimeChannel.swift (1 hunks)
  • Tests/AblyChatTests/Mocks/MockRealtimePresence.swift (1 hunks)
🧰 Additional context used
🪛 GitHub Actions: Check
Sources/AblyChat/Dependencies.swift

[error] 27-27: unable to infer associated type 'Presence' for protocol 'RealtimeChannelProtocol'

Sources/AblyChat/AblyCocoaExtensions/Ably+Dependencies.swift

[error] 7-7: type 'ARTRealtimeChannel' does not conform to protocol 'RealtimeChannelProtocol'

🔇 Additional comments (11)
Tests/AblyChatTests/Mocks/MockFeatureChannel.swift (2)

5-5: Use of any RealtimeChannelProtocol enhances flexibility.

Changing the type of channel to the existential type any RealtimeChannelProtocol increases the flexibility of the mock and aligns with similar changes across the codebase.


10-13: Initializer update is consistent with the type change.

Updating the initializer parameter to accept channel: any RealtimeChannelProtocol ensures consistency with the property's type change and allows for a wider range of protocol conforming types to be used.

Sources/AblyChat/Dependencies.swift (1)

26-30: ⚠️ Potential issue

Verify alignment with PR objectives.

The PR objectives mention reverting the addition of the presence member, but these changes are adding it. Please clarify if this is intentional or if the PR objectives need to be updated.

Additionally, there's a type inference error. Consider providing a default associated type to resolve the inference issue:

 public protocol RealtimeChannelProtocol: ARTRealtimeChannelProtocol, Sendable {
-    associatedtype Presence: RealtimePresenceProtocol
+    associatedtype Presence: RealtimePresenceProtocol = Self.DefaultPresence
+    typealias DefaultPresence = ARTRealtimePresence // or your default type
 
     var presence: Presence { get }
 }
🧰 Tools
🪛 GitHub Actions: Check

[error] 27-27: unable to infer associated type 'Presence' for protocol 'RealtimeChannelProtocol'

Sources/AblyChat/Typing.swift (1)

57-57: LGTM! Good use of existential type.

The change to use any RealtimeChannelProtocol aligns with modern Swift practices and provides more implementation flexibility.

Sources/AblyChat/RoomReactions.swift (1)

25-25: LGTM! Consistent use of existential type.

The change to use any RealtimeChannelProtocol is consistent with other files and maintains good documentation.

Sources/AblyChat/RoomFeature.swift (1)

49-49: LGTM! Explicit existential type usage improves type system clarity.

The change from RealtimeChannelProtocol to any RealtimeChannelProtocol aligns with Swift's move towards explicit existential types, making it clearer when we're working with type-erased wrappers.

Also applies to: 65-65

Tests/AblyChatTests/Mocks/MockRealtimeChannel.swift (1)

5-5: LGTM! Improved testability with concrete mock implementation.

Replacing the computed property that raised fatalError with a concrete MockRealtimePresence instance improves testability and aligns with the PR's objective of handling presence in RealtimeChannelProtocol.

Sources/AblyChat/Messages.swift (1)

54-54: LGTM! Consistent use of explicit existential type.

The change to any RealtimeChannelProtocol maintains consistency with similar changes across the codebase and improves type system clarity.

Example/AblyChatExample/Mocks/MockClients.swift (1)

91-91: LGTM! Systematic update to explicit existential types.

The consistent change from RealtimeChannelProtocol to any RealtimeChannelProtocol across all mock implementations (MockMessages, MockRoomReactions, MockTyping, MockOccupancy) maintains codebase consistency and type system clarity.

Also applies to: 149-149, 196-196, 359-359

Sources/AblyChat/Room.swift (1)

303-303: LGTM! Good use of explicit existential type.

The change to any RealtimeChannelProtocol follows Swift's best practices for protocol conformance and improves type safety by making the existential nature of the type explicit.

Sources/AblyChat/Occupancy.swift (1)

37-37: LGTM! Consistent use of existential type across the codebase.

The change to any RealtimeChannelProtocol maintains API compatibility while improving type safety, and aligns with similar changes across the codebase.

@@ -6,4 +6,6 @@ extension ARTRealtimeChannels: RealtimeChannelsProtocol {}

extension ARTRealtimeChannel: RealtimeChannelProtocol {}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix protocol conformance for ARTRealtimeChannel.

The pipeline is failing with the error: "type 'ARTRealtimeChannel' does not conform to protocol 'RealtimeChannelProtocol'". This is likely because ARTRealtimeChannel is missing required members of RealtimeChannelProtocol, specifically the presence property of type any RealtimePresenceProtocol. To resolve this, you need to ensure that ARTRealtimeChannel provides an implementation for the presence property that conforms to RealtimePresenceProtocol.

Apply this diff to fix the issue:

 extension ARTRealtimeChannel: RealtimeChannelProtocol {
+    public var presence: any RealtimePresenceProtocol {
+        return self.presence as any RealtimePresenceProtocol
+    }
 }

Ensure that self.presence correctly returns an instance conforming to RealtimePresenceProtocol. This should satisfy the protocol requirements and fix the pipeline failure.

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 GitHub Actions: Check

[error] 7-7: type 'ARTRealtimeChannel' does not conform to protocol 'RealtimeChannelProtocol'

Comment on lines +4 to +98
final class MockRealtimePresence: RealtimePresenceProtocol {
var syncComplete: Bool {
fatalError("Not implemented")
}

func get(_: @escaping ARTPresenceMessagesCallback) {
fatalError("Not implemented")
}

func get(_: ARTRealtimePresenceQuery, callback _: @escaping ARTPresenceMessagesCallback) {
fatalError("Not implemented")
}

func enter(_: Any?) {
fatalError("Not implemented")
}

func enter(_: Any?, callback _: ARTCallback? = nil) {
fatalError("Not implemented")
}

func update(_: Any?) {
fatalError("Not implemented")
}

func update(_: Any?, callback _: ARTCallback? = nil) {
fatalError("Not implemented")
}

func leave(_: Any?) {
fatalError("Not implemented")
}

func leave(_: Any?, callback _: ARTCallback? = nil) {
fatalError("Not implemented")
}

func enterClient(_: String, data _: Any?) {
fatalError("Not implemented")
}

func enterClient(_: String, data _: Any?, callback _: ARTCallback? = nil) {
fatalError("Not implemented")
}

func updateClient(_: String, data _: Any?) {
fatalError("Not implemented")
}

func updateClient(_: String, data _: Any?, callback _: ARTCallback? = nil) {
fatalError("Not implemented")
}

func leaveClient(_: String, data _: Any?) {
fatalError("Not implemented")
}

func leaveClient(_: String, data _: Any?, callback _: ARTCallback? = nil) {
fatalError("Not implemented")
}

func subscribe(_: @escaping ARTPresenceMessageCallback) -> ARTEventListener? {
fatalError("Not implemented")
}

func subscribe(attachCallback _: ARTCallback?, callback _: @escaping ARTPresenceMessageCallback) -> ARTEventListener? {
fatalError("Not implemented")
}

func subscribe(_: ARTPresenceAction, callback _: @escaping ARTPresenceMessageCallback) -> ARTEventListener? {
fatalError("Not implemented")
}

func subscribe(_: ARTPresenceAction, onAttach _: ARTCallback?, callback _: @escaping ARTPresenceMessageCallback) -> ARTEventListener? {
fatalError("Not implemented")
}

func unsubscribe() {
fatalError("Not implemented")
}

func unsubscribe(_: ARTEventListener) {
fatalError("Not implemented")
}

func unsubscribe(_: ARTPresenceAction, listener _: ARTEventListener) {
fatalError("Not implemented")
}

func history(_: @escaping ARTPaginatedPresenceCallback) {}

func history(_: ARTRealtimeHistoryQuery?, callback _: @escaping ARTPaginatedPresenceCallback) throws {
fatalError("Not implemented")
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve mock implementation consistency and documentation.

Issues found:

  1. The history(_:) method has an empty implementation while all other methods throw fatalError. This inconsistency could lead to confusion.
  2. Missing documentation explaining the mock's intended use and which methods are expected to be implemented.
  3. Consider using a more descriptive error message in fatalError calls.

Consider these improvements:

  1. Add documentation:
/// A mock implementation of RealtimePresenceProtocol for testing purposes.
/// This mock currently throws fatalError for all methods as they are not yet implemented.
final class MockRealtimePresence: RealtimePresenceProtocol {
  1. Make implementation consistent:
-    func history(_: @escaping ARTPaginatedPresenceCallback) {}
+    func history(_: @escaping ARTPaginatedPresenceCallback) {
+        fatalError("Not implemented")
+    }
  1. Add more descriptive error messages:
-        fatalError("Not implemented")
+        fatalError("MockRealtimePresence.syncComplete is not implemented")

@lawrence-forooghian lawrence-forooghian deleted the handle-presence-removed-from-protocol branch January 16, 2025 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant