Skip to content

Commit

Permalink
Merge pull request #65 from ably-labs/create-sendable-PresenceQuery
Browse files Browse the repository at this point in the history
Remove use of `ARTRealtimePresence` query in public API
  • Loading branch information
lawrence-forooghian authored Sep 26, 2024
2 parents 83a3542 + 8184232 commit b35b114
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion Sources/AblyChat/Presence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public typealias PresenceData = any Sendable

public protocol Presence: AnyObject, Sendable, EmitsDiscontinuities {
func get() async throws -> [PresenceMember]
func get(params: ARTRealtimePresenceQuery?) async throws -> [PresenceMember]
func get(params: PresenceQuery?) async throws -> [PresenceMember]
func isUserPresent(clientID: String) async throws -> Bool
func enter() async throws
func enter(data: PresenceData) async throws
Expand Down Expand Up @@ -61,3 +61,24 @@ public struct PresenceEvent: Sendable {
self.data = data
}
}

// This is a Sendable equivalent of ably-cocoa’s ARTRealtimePresenceQuery type.
//
// Originally, ``Presence.get(params:)`` accepted an ARTRealtimePresenceQuery object, but I’ve changed it to accept this type, because else when you try and write an actor that implements ``Presence``, you get a compiler error like "Non-sendable type 'ARTRealtimePresenceQuery' in parameter of the protocol requirement satisfied by actor-isolated instance method 'get(params:)' cannot cross actor boundary; this is an error in the Swift 6 language mode".
//
// Now, based on my limited understanding, you _should_ be able to send non-Sendable values from one isolation domain to another (the purpose of the "region-based isolation" and "`sending` parameters" features added in Swift 6), but to get this to work I had to mark ``Presence`` as requiring conformance to the `Actor` protocol, and since I didn’t understand _why_ I had to do that, I didn’t want to put it in the public API.
//
// So, for now, let’s just accept this copy (which I don’t think is a big problem anyway); we can always revisit it with more Swift concurrency knowledge in the future. Created https://github.com/ably-labs/ably-chat-swift/issues/64 to revisit.
public struct PresenceQuery: Sendable {
public var limit = 100
public var clientID: String?
public var connectionID: String?
public var waitForSync = true

internal init(limit: Int = 100, clientID: String? = nil, connectionID: String? = nil, waitForSync: Bool = true) {
self.limit = limit
self.clientID = clientID
self.connectionID = connectionID
self.waitForSync = waitForSync
}
}

0 comments on commit b35b114

Please sign in to comment.