Skip to content

Commit 3fe6416

Browse files
committed
Return stream as a sequence
1 parent ac13e18 commit 3fe6416

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

FirebaseAuth/Sources/Swift/Auth/Auth+Async.swift

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,18 @@
1414

1515
import Foundation
1616

17-
#if swift(>=5.5.2)
18-
@available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *)
19-
public extension Auth {
20-
/// An asynchronous stream of authentication state changes.
17+
public extension Auth {
18+
/// An asynchronous sequence of authentication state changes.
2119
///
22-
/// This stream provides a modern, `async/await`-compatible way to monitor the authentication
20+
/// This sequence provides a modern, `async/await`-compatible way to monitor the authentication
2321
/// state of the current user. It emits a new `User?` value whenever the user signs in or
2422
/// out.
2523
///
26-
/// The stream's underlying listener is automatically managed. It is added to the `Auth`
27-
/// instance when you begin iterating over the stream and is removed when the iteration
24+
/// The sequence's underlying listener is automatically managed. It is added to the `Auth`
25+
/// instance when you begin iterating over the sequence and is removed when the iteration
2826
/// is cancelled or terminates.
2927
///
30-
/// - Important: The first value emitted by this stream is always the *current* authentication
28+
/// - Important: The first value emitted by this sequence is always the *current* authentication
3129
/// state, which may be `nil` if no user is signed in.
3230
///
3331
/// ### Example Usage
@@ -47,7 +45,8 @@ import Foundation
4745
/// }
4846
/// }
4947
/// ```
50-
var authStateChanges: AsyncStream<User?> {
48+
@available(iOS 18.0, *)
49+
var authStateChanges: some AsyncSequence<User?, Never> {
5150
AsyncStream { continuation in
5251
let listenerHandle = addStateDidChangeListener { _, user in
5352
continuation.yield(user)
@@ -59,16 +58,16 @@ import Foundation
5958
}
6059
}
6160

62-
/// An asynchronous stream of ID token changes.
61+
/// An asynchronous sequence of ID token changes.
6362
///
64-
/// This stream provides a modern, `async/await`-compatible way to monitor changes to the
63+
/// This sequence provides a modern, `async/await`-compatible way to monitor changes to the
6564
/// current user's ID token. It emits a new `User?` value whenever the ID token changes.
6665
///
67-
/// The stream's underlying listener is automatically managed. It is added to the `Auth`
68-
/// instance when you begin iterating over the stream and is removed when the iteration
66+
/// The sequence's underlying listener is automatically managed. It is added to the `Auth`
67+
/// instance when you begin iterating over the sequence and is removed when the iteration
6968
/// is cancelled or terminates.
7069
///
71-
/// - Important: The first value emitted by this stream is always the *current* authentication
70+
/// - Important: The first value emitted by this sequence is always the *current* authentication
7271
/// state, which may be `nil` if no user is signed in.
7372
///
7473
/// ### Example Usage
@@ -88,16 +87,16 @@ import Foundation
8887
/// }
8988
/// }
9089
/// ```
91-
var idTokenChanges: AsyncStream<User?> {
90+
@available(iOS 18.0, *)
91+
var idTokenChanges: some AsyncSequence<User?, Never> {
9292
AsyncStream { continuation in
9393
let listenerHandle = addIDTokenDidChangeListener { _, user in
9494
continuation.yield(user)
9595
}
9696

9797
continuation.onTermination = { @Sendable _ in
98-
self.removeStateDidChangeListener(listenerHandle)
98+
self.removeIDTokenDidChangeListener(listenerHandle)
9999
}
100100
}
101101
}
102102
}
103-
#endif // swift(>=5.5.2)

FirebaseAuth/Tests/Unit/AuthStateChangesAsyncTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class AuthStateChangesAsyncTests: RPCBaseTests {
6262
_ = workerSemaphore.wait(timeout: DispatchTime.distantFuture)
6363
}
6464

65+
@available(iOS 18.0, *)
6566
func testAuthStateChangesStreamYieldsUserOnSignIn() async throws {
6667
// Given
6768
let initialNilExpectation = expectation(description: "Stream should emit initial nil user")
@@ -103,6 +104,7 @@ class AuthStateChangesAsyncTests: RPCBaseTests {
103104
task.cancel()
104105
}
105106

107+
@available(iOS 18.0, *)
106108
func testAuthStateChangesStreamIsCancelled() async throws {
107109
// Given: An inverted expectation that will fail the test if it's fulfilled.
108110
let streamCancelledExpectation =
@@ -145,6 +147,7 @@ class AuthStateChangesAsyncTests: RPCBaseTests {
145147
XCTAssertEqual(iteration, 1, "The stream should have only emitted its initial value.")
146148
}
147149

150+
@available(iOS 18.0, *)
148151
func testAuthStateChangesStreamYieldsNilOnSignOut() async throws {
149152
// Given
150153
let initialNilExpectation = expectation(description: "Stream should emit initial nil user")

FirebaseAuth/Tests/Unit/IdTokenChangesAsyncTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class IdTokenChangesAsyncTests: RPCBaseTests {
6262
_ = workerSemaphore.wait(timeout: DispatchTime.distantFuture)
6363
}
6464

65+
@available(iOS 18.0, *)
6566
func testIdTokenChangesStreamYieldsUserOnSignIn() async throws {
6667
// Given
6768
let initialNilExpectation = expectation(description: "Stream should emit initial nil user")
@@ -103,6 +104,7 @@ class IdTokenChangesAsyncTests: RPCBaseTests {
103104
task.cancel()
104105
}
105106

107+
@available(iOS 18.0, *)
106108
func testIdTokenChangesStreamIsCancelled() async throws {
107109
// Given: An inverted expectation that will fail the test if it's fulfilled.
108110
let streamCancelledExpectation =
@@ -145,6 +147,7 @@ class IdTokenChangesAsyncTests: RPCBaseTests {
145147
XCTAssertEqual(iteration, 1, "The stream should have only emitted its initial value.")
146148
}
147149

150+
@available(iOS 18.0, *)
148151
func testIdTokenChangesStreamYieldsNilOnSignOut() async throws {
149152
// Given
150153
let initialNilExpectation = expectation(description: "Stream should emit initial nil user")

0 commit comments

Comments
 (0)