@@ -9,7 +9,11 @@ import Foundation
9
9
@_spi ( Internal) import _Helpers
10
10
11
11
public struct PresenceV2 : Hashable , Sendable {
12
+ /// The presence reference of the object.
12
13
public let ref : String
14
+
15
+ /// The object the other client is tracking. Can be done via the
16
+ /// ``RealtimeChannelV2/track(state:)`` method.
13
17
public let state : JSONObject
14
18
}
15
19
@@ -72,34 +76,63 @@ extension PresenceV2: Codable {
72
76
try container. encode ( ref, forKey: _StringCodingKey ( " phx_ref " ) )
73
77
try container. encode ( state, forKey: _StringCodingKey ( " state " ) )
74
78
}
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
+ }
75
90
}
76
91
92
+ /// Represents a presence action.
77
93
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.
78
97
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.
79
102
var leaves : [ String : PresenceV2 ] { get }
80
103
}
81
104
82
105
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.
83
110
public func decodeJoins< T: Decodable > (
84
111
as _: T . Type = T . self,
85
- ignoreOtherTypes: Bool = true
112
+ ignoreOtherTypes: Bool = true ,
113
+ decoder: JSONDecoder = AnyJSON . decoder
86
114
) throws -> [ T ] {
87
115
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 ) }
89
117
}
90
118
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 ) }
92
120
}
93
121
122
+ /// Decode all ``PresenceAction/leaves`` values.
123
+ /// - Parameters:
124
+ /// - ignoreOtherTypes: Whether to ignore presences which cannot be decoded such as your own
125
+ /// presence.
94
126
public func decodeLeaves< T: Decodable > (
95
127
as _: T . Type = T . self,
96
- ignoreOtherTypes: Bool = true
128
+ ignoreOtherTypes: Bool = true ,
129
+ decoder: JSONDecoder = AnyJSON . decoder
97
130
) throws -> [ T ] {
98
131
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 ) }
100
133
}
101
134
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 ) }
103
136
}
104
137
}
105
138
0 commit comments