Skip to content

Commit e828aeb

Browse files
authored
Effect.cancel(ids:) prefer sequence of ids over variadic list. (#1009)
* Disfavor variadic cancellation * Changed ids paramter from array to sequence * Small typo
1 parent 12ae834 commit e828aeb

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

Sources/ComposableArchitecture/Effects/Cancellation.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,17 @@ extension Effect {
8989
/// - Parameter ids: A variadic list of effect identifiers.
9090
/// - Returns: A new effect that will cancel any currently in-flight effects with the given
9191
/// identifiers.
92+
@_disfavoredOverload
9293
public static func cancel(ids: AnyHashable...) -> Effect {
9394
.cancel(ids: ids)
9495
}
9596

9697
/// An effect that will cancel multiple currently in-flight effects with the given identifiers.
9798
///
98-
/// - Parameter ids: An array of effect identifiers.
99+
/// - Parameter ids: A sequence of effect identifiers.
99100
/// - Returns: A new effect that will cancel any currently in-flight effects with the given
100101
/// identifiers.
101-
public static func cancel(ids: [AnyHashable]) -> Effect {
102+
public static func cancel<S: Sequence>(ids: S) -> Effect where S.Element == AnyHashable {
102103
.merge(ids.map(Effect.cancel(id:)))
103104
}
104105
}

Tests/ComposableArchitectureTests/EffectCancellationTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,34 @@ final class EffectCancellationTests: XCTestCase {
302302

303303
XCTAssertEqual(output, [1, 2])
304304
}
305+
306+
func testMultipleCancellations() {
307+
let scheduler = DispatchQueue.test
308+
var values: [Int] = []
309+
310+
struct CancellationId: Hashable {
311+
var id: Int
312+
}
313+
314+
let effects = [1, 2, 3, 4, 5].map { id in
315+
Just(id)
316+
.delay(for: 1, scheduler: scheduler)
317+
.eraseToEffect()
318+
.cancellable(id: CancellationId(id: id))
319+
}
320+
321+
Effect<Int, Never>.merge(effects)
322+
.sink { values.append($0) }
323+
.store(in: &self.cancellables)
324+
325+
Effect<Int, Never>.merge(
326+
.cancel(ids: CancellationId(id: 1), CancellationId(id: 2)),
327+
.cancel(ids: [CancellationId(id: 4), CancellationId(id: 5)][...])
328+
)
329+
.sink { _ in }
330+
.store(in: &self.cancellables)
331+
332+
scheduler.advance(by: 1)
333+
XCTAssertEqual(values, [3])
334+
}
305335
}

0 commit comments

Comments
 (0)