@@ -207,16 +207,18 @@ public actor AuthClient {
207
207
}
208
208
209
209
/// Listen for auth state changes.
210
+ /// - Parameter listener: Block that executes when a new event is emitted.
211
+ /// - Returns: A handle that can be used to manually unsubscribe.
210
212
///
211
- /// An `.initialSession` is always emitted when this method is called.
213
+ /// - Note: This method blocks execution until the ``AuthChangeEvent/initialSession`` event is
214
+ /// emitted. Although this operation is usually fast, in case of the current stored session being
215
+ /// invalid, a call to the endpoint is necessary for refreshing the session.
212
216
@discardableResult
213
217
public func onAuthStateChange(
214
218
_ listener: @escaping AuthStateChangeListener
215
- ) -> AuthStateChangeListenerHandle {
219
+ ) async -> AuthStateChangeListenerHandle {
216
220
let handle = eventEmitter. attachListener ( listener)
217
- Task {
218
- await emitInitialSession ( forHandle: handle)
219
- }
221
+ await emitInitialSession ( forHandle: handle)
220
222
return handle
221
223
}
222
224
@@ -232,12 +234,14 @@ public actor AuthClient {
232
234
session: Session?
233
235
) > . makeStream( )
234
236
235
- let handle = onAuthStateChange { event, session in
236
- continuation. yield ( ( event, session) )
237
- }
237
+ Task {
238
+ let handle = await onAuthStateChange { event, session in
239
+ continuation. yield ( ( event, session) )
240
+ }
238
241
239
- continuation. onTermination = { _ in
240
- handle. cancel ( )
242
+ continuation. onTermination = { _ in
243
+ handle. cancel ( )
244
+ }
241
245
}
242
246
243
247
return stream
0 commit comments