Skip to content

Commit 4de8597

Browse files
committed
Add cancel overloads that take types (#1078)
* Add cancel overloads that take types * wip
1 parent 2af8f6c commit 4de8597

28 files changed

+179
-85
lines changed

Examples/CaseStudies/SwiftUICaseStudies/02-Effects-Cancellation.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ let effectsCancellationReducer = Reducer<
4040
EffectsCancellationState, EffectsCancellationAction, EffectsCancellationEnvironment
4141
> { state, action, environment in
4242

43-
struct TriviaRequestId: Hashable {}
43+
enum TriviaRequestId {}
4444

4545
switch action {
4646
case .cancelButtonTapped:
4747
state.isTriviaRequestInFlight = false
48-
return .cancel(id: TriviaRequestId())
48+
return .cancel(id: TriviaRequestId.self)
4949

5050
case let .stepperChanged(value):
5151
state.count = value
5252
state.currentTrivia = nil
5353
state.isTriviaRequestInFlight = false
54-
return .cancel(id: TriviaRequestId())
54+
return .cancel(id: TriviaRequestId.self)
5555

5656
case .triviaButtonTapped:
5757
state.currentTrivia = nil
@@ -60,7 +60,7 @@ let effectsCancellationReducer = Reducer<
6060
return environment.fact.fetch(state.count)
6161
.observe(on: environment.mainQueue)
6262
.catchToEffect(EffectsCancellationAction.triviaResponse)
63-
.cancellable(id: TriviaRequestId())
63+
.cancellable(id: TriviaRequestId.self)
6464

6565
case let .triviaResponse(.success(response)):
6666
state.isTriviaRequestInFlight = false

Examples/CaseStudies/SwiftUICaseStudies/02-Effects-LongLiving.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ let longLivingEffectsReducer = Reducer<
3939
LongLivingEffectsState, LongLivingEffectsAction, LongLivingEffectsEnvironment
4040
> { state, action, environment in
4141

42-
struct UserDidTakeScreenshotNotificationId: Hashable {}
42+
enum UserDidTakeScreenshotNotificationId {}
4343

4444
switch action {
4545
case .userDidTakeScreenshotNotification:
@@ -50,11 +50,11 @@ let longLivingEffectsReducer = Reducer<
5050
// When the view appears, start the effect that emits when screenshots are taken.
5151
return environment.userDidTakeScreenshot
5252
.map { LongLivingEffectsAction.userDidTakeScreenshotNotification }
53-
.cancellable(id: UserDidTakeScreenshotNotificationId())
53+
.cancellable(id: UserDidTakeScreenshotNotificationId.self)
5454

5555
case .onDisappear:
5656
// When view disappears, stop the effect.
57-
return .cancel(id: UserDidTakeScreenshotNotificationId())
57+
return .cancel(id: UserDidTakeScreenshotNotificationId.self)
5858
}
5959
}
6060

Examples/CaseStudies/SwiftUICaseStudies/02-Effects-Refreshable.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ let refreshableReducer = Reducer<
3737
RefreshableEnvironment
3838
> { state, action, environment in
3939

40-
struct CancelId: Hashable {}
40+
enum CancelId {}
4141

4242
switch action {
4343
case .cancelButtonTapped:
4444
state.isLoading = false
45-
return .cancel(id: CancelId())
45+
return .cancel(id: CancelId.self)
4646

4747
case .decrementButtonTapped:
4848
state.count -= 1
@@ -68,7 +68,7 @@ let refreshableReducer = Reducer<
6868
return environment.fact.fetch(state.count)
6969
.delay(2, on: environment.mainQueue.animation())
7070
.catchToEffect(RefreshableAction.factResponse)
71-
.cancellable(id: CancelId())
71+
.cancellable(id: CancelId.self)
7272
}
7373
}
7474

Examples/CaseStudies/SwiftUICaseStudies/02-Effects-Timers.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ struct TimersEnvironment {
2929

3030
let timersReducer = Reducer<TimersState, TimersAction, TimersEnvironment> {
3131
state, action, environment in
32-
struct TimerId: Hashable {}
32+
33+
enum TimerId {}
3334

3435
switch action {
3536
case .timerTicked:
@@ -40,13 +41,13 @@ let timersReducer = Reducer<TimersState, TimersAction, TimersEnvironment> {
4041
state.isTimerActive.toggle()
4142
return state.isTimerActive
4243
? Effect.timer(
43-
id: TimerId(),
44+
id: TimerId.self,
4445
every: .seconds(1),
4546
tolerance: .seconds(0),
4647
on: environment.mainQueue.animation(.interpolatingSpring(stiffness: 3000, damping: 40))
4748
)
4849
.map { _ in TimersAction.timerTicked }
49-
: Effect.cancel(id: TimerId())
50+
: .cancel(id: TimerId.self)
5051
}
5152
}
5253

Examples/CaseStudies/SwiftUICaseStudies/02-Effects-WebSocket.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct WebSocketEnvironment {
4141

4242
let webSocketReducer = Reducer<WebSocketState, WebSocketAction, WebSocketEnvironment> {
4343
state, action, environment in
44+
4445
struct WebSocketId: Hashable {}
4546

4647
var receiveSocketMessageEffect: Effect<WebSocketAction, Never> {

Examples/CaseStudies/SwiftUICaseStudies/03-Navigation-Lists-LoadThenNavigate.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ let loadThenNavigateListReducer =
5353
with: Reducer<
5454
LoadThenNavigateListState, LoadThenNavigateListAction, LoadThenNavigateListEnvironment
5555
> { state, action, environment in
56-
struct CancelId: Hashable {}
56+
57+
enum CancelId {}
5758

5859
switch action {
5960
case .counter:
6061
return .none
6162

6263
case .onDisappear:
63-
return .cancel(id: CancelId())
64+
return .cancel(id: CancelId.self)
6465

6566
case let .setNavigation(selection: .some(navigatedId)):
6667
for row in state.rows {
@@ -69,14 +70,14 @@ let loadThenNavigateListReducer =
6970

7071
return Effect(value: .setNavigationSelectionDelayCompleted(navigatedId))
7172
.delay(1, on: environment.mainQueue)
72-
.cancellable(id: CancelId(), cancelInFlight: true)
73+
.cancellable(id: CancelId.self, cancelInFlight: true)
7374

7475
case .setNavigation(selection: .none):
7576
if let selection = state.selection {
7677
state.rows[id: selection.id]?.count = selection.count
7778
}
7879
state.selection = nil
79-
return .cancel(id: CancelId())
80+
return .cancel(id: CancelId.self)
8081

8182
case let .setNavigationSelectionDelayCompleted(id):
8283
state.rows[id: id]?.isActivityIndicatorVisible = false

Examples/CaseStudies/SwiftUICaseStudies/03-Navigation-Lists-NavigateAndLoad.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ let navigateAndLoadListReducer =
4848
NavigateAndLoadListState, NavigateAndLoadListAction, NavigateAndLoadListEnvironment
4949
> { state, action, environment in
5050

51-
struct CancelId: Hashable {}
51+
enum CancelId {}
5252

5353
switch action {
5454
case .counter:
@@ -59,14 +59,14 @@ let navigateAndLoadListReducer =
5959

6060
return Effect(value: .setNavigationSelectionDelayCompleted)
6161
.delay(1, on: environment.mainQueue)
62-
.cancellable(id: CancelId())
62+
.cancellable(id: CancelId.self)
6363

6464
case .setNavigation(selection: .none):
6565
if let selection = state.selection, let count = selection.value?.count {
6666
state.rows[id: selection.id]?.count = count
6767
}
6868
state.selection = nil
69-
return .cancel(id: CancelId())
69+
return .cancel(id: CancelId.self)
7070

7171
case .setNavigationSelectionDelayCompleted:
7272
guard let id = state.selection?.id else { return .none }

Examples/CaseStudies/SwiftUICaseStudies/03-Navigation-LoadThenNavigate.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,17 @@ let loadThenNavigateReducer =
4141
LoadThenNavigateState, LoadThenNavigateAction, LoadThenNavigateEnvironment
4242
> { state, action, environment in
4343

44-
struct CancelId: Hashable {}
44+
enum CancelId {}
4545

4646
switch action {
47-
4847
case .onDisappear:
49-
return .cancel(id: CancelId())
48+
return .cancel(id: CancelId.self)
5049

5150
case .setNavigation(isActive: true):
5251
state.isActivityIndicatorVisible = true
5352
return Effect(value: .setNavigationIsActiveDelayCompleted)
5453
.delay(1, on: environment.mainQueue)
55-
.cancellable(id: CancelId())
54+
.cancellable(id: CancelId.self)
5655

5756
case .setNavigation(isActive: false):
5857
state.optionalCounter = nil

Examples/CaseStudies/SwiftUICaseStudies/03-Navigation-NavigateAndLoad.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,20 @@ let navigateAndLoadReducer =
3838
with: Reducer<
3939
NavigateAndLoadState, NavigateAndLoadAction, NavigateAndLoadEnvironment
4040
> { state, action, environment in
41-
struct CancelId: Hashable {}
41+
42+
enum CancelId {}
43+
4244
switch action {
4345
case .setNavigation(isActive: true):
4446
state.isNavigationActive = true
4547
return Effect(value: .setNavigationIsActiveDelayCompleted)
4648
.delay(1, on: environment.mainQueue)
47-
.cancellable(id: CancelId())
49+
.cancellable(id: CancelId.self)
4850

4951
case .setNavigation(isActive: false):
5052
state.isNavigationActive = false
5153
state.optionalCounter = nil
52-
return .cancel(id: CancelId())
54+
return .cancel(id: CancelId.self)
5355

5456
case .setNavigationIsActiveDelayCompleted:
5557
state.optionalCounter = CounterState()

Examples/CaseStudies/SwiftUICaseStudies/03-Navigation-Sheet-LoadThenPresent.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,17 @@ let loadThenPresentReducer =
4141
LoadThenPresentState, LoadThenPresentAction, LoadThenPresentEnvironment
4242
> { state, action, environment in
4343

44-
struct CancelId: Hashable {}
44+
enum CancelId {}
4545

4646
switch action {
47-
4847
case .onDisappear:
49-
return .cancel(id: CancelId())
48+
return .cancel(id: CancelId.self)
5049

5150
case .setSheet(isPresented: true):
5251
state.isActivityIndicatorVisible = true
5352
return Effect(value: .setSheetIsPresentedDelayCompleted)
5453
.delay(1, on: environment.mainQueue)
55-
.cancellable(id: CancelId())
54+
.cancellable(id: CancelId.self)
5655

5756
case .setSheet(isPresented: false):
5857
state.optionalCounter = nil

0 commit comments

Comments
 (0)