Skip to content

Commit b0e5594

Browse files
authored
docs: add doc comments for PresenceAction type (#226)
1 parent 56ca0fd commit b0e5594

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

Sources/Realtime/V2/PresenceAction.swift

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import Foundation
99
@_spi(Internal) import _Helpers
1010

1111
public struct PresenceV2: Hashable, Sendable {
12+
/// The presence reference of the object.
1213
public let ref: String
14+
15+
/// The object the other client is tracking. Can be done via the
16+
/// ``RealtimeChannelV2/track(state:)`` method.
1317
public let state: JSONObject
1418
}
1519

@@ -72,34 +76,63 @@ extension PresenceV2: Codable {
7276
try container.encode(ref, forKey: _StringCodingKey("phx_ref"))
7377
try container.encode(state, forKey: _StringCodingKey("state"))
7478
}
79+
80+
/// Decode ``state``.
81+
///
82+
/// - Note: You can also receive your own presence, but without your state so be aware of
83+
/// exceptions.
84+
public func decodeState<T: Decodable>(
85+
as _: T.Type = T.self,
86+
decoder: JSONDecoder = AnyJSON.decoder
87+
) throws -> T {
88+
try state.decode(as: T.self, decoder: decoder)
89+
}
7590
}
7691

92+
/// Represents a presence action.
7793
public protocol PresenceAction: Sendable, HasRawMessage {
94+
/// Represents a map of ``PresenceV2`` objects indexed by their key.
95+
///
96+
/// Your own key can be customized when creating the channel within the presence config.
7897
var joins: [String: PresenceV2] { get }
98+
99+
/// Represents a map of ``PresenceV2`` objects indexed by their key.
100+
///
101+
/// Your own key can be customized when creating the channel within the presence config.
79102
var leaves: [String: PresenceV2] { get }
80103
}
81104

82105
extension PresenceAction {
106+
/// Decode all ``PresenceAction/joins`` values.
107+
/// - Parameters:
108+
/// - ignoreOtherTypes: Whether to ignore presences which cannot be decoded such as your own
109+
/// presence.
83110
public func decodeJoins<T: Decodable>(
84111
as _: T.Type = T.self,
85-
ignoreOtherTypes: Bool = true
112+
ignoreOtherTypes: Bool = true,
113+
decoder: JSONDecoder = AnyJSON.decoder
86114
) throws -> [T] {
87115
if ignoreOtherTypes {
88-
return joins.values.compactMap { try? $0.state.decode(as: T.self) }
116+
return joins.values.compactMap { try? $0.decodeState(as: T.self, decoder: decoder) }
89117
}
90118

91-
return try joins.values.map { try $0.state.decode(as: T.self) }
119+
return try joins.values.map { try $0.decodeState(as: T.self, decoder: decoder) }
92120
}
93121

122+
/// Decode all ``PresenceAction/leaves`` values.
123+
/// - Parameters:
124+
/// - ignoreOtherTypes: Whether to ignore presences which cannot be decoded such as your own
125+
/// presence.
94126
public func decodeLeaves<T: Decodable>(
95127
as _: T.Type = T.self,
96-
ignoreOtherTypes: Bool = true
128+
ignoreOtherTypes: Bool = true,
129+
decoder: JSONDecoder = AnyJSON.decoder
97130
) throws -> [T] {
98131
if ignoreOtherTypes {
99-
return leaves.values.compactMap { try? $0.state.decode(as: T.self) }
132+
return leaves.values.compactMap { try? $0.decodeState(as: T.self, decoder: decoder) }
100133
}
101134

102-
return try leaves.values.map { try $0.state.decode(as: T.self) }
135+
return try leaves.values.map { try $0.decodeState(as: T.self, decoder: decoder) }
103136
}
104137
}
105138

0 commit comments

Comments
 (0)