Skip to content

Commit

Permalink
Add and improve logging for Messages and Reactions
Browse files Browse the repository at this point in the history
  • Loading branch information
umair-ably committed Nov 18, 2024
1 parent 234cc2e commit 91ab15c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Sources/AblyChat/DefaultOccupancy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal final class DefaultOccupancy: Occupancy, EmitsDiscontinuities {
internal func subscribe(bufferingPolicy: BufferingPolicy) async -> Subscription<OccupancyEvent> {
logger.log(message: "Subscribing to occupancy events", level: .debug)
let subscription = Subscription<OccupancyEvent>(bufferingPolicy: bufferingPolicy)
channel.subscribe("[meta]occupancy") { [logger] message in
channel.subscribe(OccupancyEvents.meta.rawValue) { [logger] message in
logger.log(message: "Received occupancy message: \(message)", level: .debug)
guard let data = message.data as? [String: Any],
let metrics = data["metrics"] as? [String: Any]
Expand Down
14 changes: 7 additions & 7 deletions Sources/AblyChat/DefaultPresence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ internal final class DefaultPresence: Presence, EmitsDiscontinuities {
return try await withCheckedThrowingContinuation { continuation in
channel.presence.get { [processPresenceGet] members, error in
do {
try processPresenceGet(continuation, members, error)
// No need to resume continuation here as it's handled inside processPresenceGet
let presenceMembers = try processPresenceGet(members, error)
continuation.resume(returning: presenceMembers)
} catch {
continuation.resume(throwing: error)
// processPresenceGet will log any errors
Expand All @@ -39,8 +39,8 @@ internal final class DefaultPresence: Presence, EmitsDiscontinuities {
return try await withCheckedThrowingContinuation { continuation in
channel.presence.get(params.asARTRealtimePresenceQuery()) { [processPresenceGet] members, error in
do {
try processPresenceGet(continuation, members, error)
// No need to resume continuation here as it's handled inside processPresenceGet
let presenceMembers = try processPresenceGet(members, error)
continuation.resume(returning: presenceMembers)
} catch {
continuation.resume(throwing: error)
// processPresenceGet will log any errors
Expand Down Expand Up @@ -147,7 +147,7 @@ internal final class DefaultPresence: Presence, EmitsDiscontinuities {
}

private func decodePresenceData(from data: Any?) -> PresenceData? {
guard let userData = (data as? [String: Any]) else {
guard let userData = data as? [String: Any] else {
return nil
}

Expand All @@ -162,7 +162,7 @@ internal final class DefaultPresence: Presence, EmitsDiscontinuities {
}
}

private func processPresenceGet(continuation: CheckedContinuation<[PresenceMember], any Error>, members: [ARTPresenceMessage]?, error: ARTErrorInfo?) throws {
private func processPresenceGet(members: [ARTPresenceMessage]?, error: ARTErrorInfo?) throws -> [PresenceMember] {
guard let members else {
let error = error ?? ARTErrorInfo.create(withCode: 50000, status: 500, message: "Received incoming message without data or text")
logger.log(message: error.message, level: .error)
Expand Down Expand Up @@ -203,7 +203,7 @@ internal final class DefaultPresence: Presence, EmitsDiscontinuities {
logger.log(message: "Returning presence member: \(presenceMember)", level: .debug)
return presenceMember
}
continuation.resume(returning: presenceMembers)
return presenceMembers
}

private func processPresenceSubscribe(_ message: ARTPresenceMessage, for event: PresenceEventType) throws -> PresenceEvent {
Expand Down
4 changes: 4 additions & 0 deletions Sources/AblyChat/Events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ internal enum MessageEvent: String {
internal enum RoomReactionEvents: String {
case reaction = "roomReaction"
}

internal enum OccupancyEvents: String {
case meta = "[meta]occupancy"
}
1 change: 1 addition & 0 deletions Sources/AblyChat/Presence.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Ably

// TODO: (https://github.com/ably-labs/ably-chat-swift/issues/13): try to improve this type
public enum PresenceCustomData: Sendable, Codable, Equatable {
case string(String)
case number(Int) // Changed from NSNumber to Int to conform to Codable. Address in linked issue above.
Expand Down

0 comments on commit 91ab15c

Please sign in to comment.