@@ -207,16 +207,18 @@ public actor AuthClient {
207207 }
208208
209209 /// 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.
210212 ///
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.
212216 @discardableResult
213217 public func onAuthStateChange(
214218 _ listener: @escaping AuthStateChangeListener
215- ) -> AuthStateChangeListenerHandle {
219+ ) async -> AuthStateChangeListenerHandle {
216220 let handle = eventEmitter. attachListener ( listener)
217- Task {
218- await emitInitialSession ( forHandle: handle)
219- }
221+ await emitInitialSession ( forHandle: handle)
220222 return handle
221223 }
222224
@@ -232,12 +234,14 @@ public actor AuthClient {
232234 session: Session?
233235 ) > . makeStream( )
234236
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+ }
238241
239- continuation. onTermination = { _ in
240- handle. cancel ( )
242+ continuation. onTermination = { _ in
243+ handle. cancel ( )
244+ }
241245 }
242246
243247 return stream
0 commit comments