Skip to content

Commit a9106a8

Browse files
committed
Use makeStream for creating AsyncStreams
1 parent a502eae commit a9106a8

File tree

7 files changed

+18
-53
lines changed

7 files changed

+18
-53
lines changed

Sources/NetworkingClient/Tasks/DownloadTask.swift

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ open class DownloadTask: NetworkTask<URL>, @unchecked Sendable {
2424

2525
/// A stream that emits progress updates throughout the download lifecycle.
2626
public var progressUpdates: AsyncStream<Double> {
27-
get async {
28-
return await progressTracker.progressStream
29-
}
27+
return progressTracker.progressStream.stream
3028
}
3129

3230
/// The current progress value.
@@ -136,33 +134,22 @@ extension DownloadTask {
136134
/// - Important: This type is internal and intended for use within the networking framework only.
137135
internal actor ProgressTracker {
138136
// MARK: - Properties
139-
/// The stream continuation for emitting progress values.
140-
private var continuation: AsyncStream<Double>.Continuation? {
141-
didSet {
142-
continuation?.yield(progress)
143-
}
144-
}
145-
146137
/// A stream that emits progress updates throughout the download lifecycle.
147-
///
148-
/// The stream completes automatically when ``finish()`` is called.
149-
internal private(set) lazy var progressStream: AsyncStream<Double> = {
150-
return AsyncStream(
151-
bufferingPolicy: .bufferingNewest(1)
152-
) { continuation in
153-
self.continuation = continuation
154-
}
155-
}()
138+
internal let progressStream = AsyncStream<Double>.makeStream(bufferingPolicy: .bufferingNewest(1))
156139

157140
/// The current progress value.
158141
///
159142
/// Updating this property emits the new value to the progress stream.
160143
internal private(set) var progress = Double.zero {
161144
didSet {
162-
continuation?.yield(progress)
145+
progressStream.continuation.yield(progress)
163146
}
164147
}
165148

149+
internal init() {
150+
progressStream.continuation.yield(progress)
151+
}
152+
166153
// MARK: - Functions
167154
/// Sets the progress to the given normalized value.
168155
///
@@ -180,18 +167,13 @@ internal actor ProgressTracker {
180167
/// - offset: The number of bytes downloaded.
181168
/// - total: The total number of bytes expected.
182169
internal func setProgress(_ offset: Double, total: Double) {
183-
guard total > 0 else {
184-
setProgress(0)
185-
return
186-
}
187-
setProgress(offset / total)
170+
setProgress(total > 0 ? offset / total : 0)
188171
}
189172

190173
/// Completes the progress stream and clears the continuation.
191174
///
192175
/// Call this when the download has finished or been cancelled.
193176
internal func finish() {
194-
continuation?.finish()
195-
continuation = nil
177+
progressStream.continuation.finish()
196178
}
197179
}

Sources/NetworkingClient/Tasks/NetworkTask.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,7 @@ extension NetworkTask: NetworkingTask {
261261

262262
/// A stream that emits state updates throughout the task lifecycle.
263263
public var stateUpdates: AsyncStream<TaskState> {
264-
get async {
265-
return await values.stateStream
266-
}
264+
return values.stateStream.stream
267265
}
268266

269267
/// Called when a task has finished collecting metrics.

Sources/NetworkingClient/Tasks/NetworkTaskValues.swift

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,13 @@ internal actor NetworkTaskValues<T: Sendable> {
4141
/// The current ``URLSessionTask`` instance, if any.
4242
internal var sessionTask: URLSessionTask?
4343

44-
/// The stream continuation for emitting progress values.
45-
private var stateStreamContinuation: AsyncStream<TaskState>.Continuation? {
46-
didSet {
47-
stateStreamContinuation?.yield(state)
48-
}
49-
}
50-
5144
/// A stream that emits state updates throughout the task lifecycle.
52-
internal private(set) lazy var stateStream: AsyncStream<TaskState> = {
53-
return AsyncStream(
54-
bufferingPolicy: .bufferingNewest(1)
55-
) { continuation in
56-
self.stateStreamContinuation = continuation
57-
}
58-
}()
45+
internal let stateStream = AsyncStream<TaskState>.makeStream(bufferingPolicy: .bufferingNewest(1))
5946

6047
/// The current execution state of a task.
6148
internal private(set) var state = TaskState.created {
6249
didSet {
63-
stateStreamContinuation?.yield(state)
50+
stateStream.continuation.yield(state)
6451
}
6552
}
6653

@@ -70,6 +57,7 @@ internal actor NetworkTaskValues<T: Sendable> {
7057
/// - Parameter request: The type-erased request associated with the task.
7158
internal init(request: AnyRequest) {
7259
self.request = request
60+
stateStream.continuation.yield(state)
7361
}
7462

7563
// MARK: - Functions
@@ -139,8 +127,7 @@ internal actor NetworkTaskValues<T: Sendable> {
139127
///
140128
/// Call this when the task has finished or been cancelled.
141129
internal func finish() {
142-
stateStreamContinuation?.finish()
143-
stateStreamContinuation = nil
130+
stateStream.continuation.finish()
144131
}
145132
}
146133

Sources/NetworkingCore/RequestModifiers/Parameters/Parameter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ extension Request {
182182
/// struct GoogleRequest {
183183
/// var request: some Request {
184184
/// HTTPRequest()
185-
/// .appendingParameter("language", value: "en")
185+
/// .appendingParameter("language", values: ["en"])
186186
/// }
187187
/// }
188188
/// ```

Sources/NetworkingCore/RequestModifiers/Parameters/RequestParameter.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ public protocol RequestParameter: RequestModifier, CustomStringConvertible {
1414
var parameters: [URLQueryItem] {get}
1515
}
1616

17-
// TODO: - Add support for parameter encoding strategies (ie: arrays)
18-
1917
// MARK: - RequestModifier
2018
extension RequestParameter {
2119
/// Modifies the given ``URLRequest`` by appending query parameters.

Tests/NetworkingClientTests/Tasks/DonwloadTaskTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ struct DownloadTaskTests {
8787
}
8888

8989
var progressUpdates = [Double]()
90-
for await progress in await task.progressUpdates {
90+
for await progress in task.progressUpdates {
9191
try await #require(task.progress == progress)
9292
progressUpdates.append(progress)
9393
}
@@ -109,7 +109,7 @@ struct DownloadTaskTests {
109109
}
110110

111111
var progressUpdate = 0.0
112-
for await progress in await task.progressUpdates {
112+
for await progress in task.progressUpdates {
113113
try await #require(task.progress == progress)
114114
progressUpdate = progress
115115
}

Tests/NetworkingClientTests/Tasks/NetworkTaskStateTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ struct NetworkTaskStateTests {
180180
}
181181

182182
var updates = [TaskState]()
183-
for await state in await task.stateUpdates {
183+
for await state in task.stateUpdates {
184184
try await #require(task.state == state)
185185
updates.append(state)
186186
}

0 commit comments

Comments
 (0)