Skip to content

Commit 3043d10

Browse files
JunBookJunBook
JunBook
authored and
JunBook
committed
Fix issue pointfreeco#3417: Add unique ID for independent timer operation
1 parent 4c08664 commit 3043d10

File tree

1 file changed

+5
-3
lines changed
  • Sources/ComposableArchitecture/Documentation.docc/Tutorials/MeetTheComposableArchitecture/01-Essentials/02-AddingSideEffects

1 file changed

+5
-3
lines changed

Sources/ComposableArchitecture/Documentation.docc/Tutorials/MeetTheComposableArchitecture/01-Essentials/02-AddingSideEffects/01-02-03-code-0005.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct CounterFeature {
88
var fact: String?
99
var isLoading = false
1010
var isTimerRunning = false
11+
let timerID = UUID()
1112
}
1213

1314
enum Action {
@@ -19,7 +20,7 @@ struct CounterFeature {
1920
case toggleTimerButtonTapped
2021
}
2122

22-
enum CancelID { case timer }
23+
enum CancelID: Hashable { case timer(UUID) }
2324

2425
var body: some ReducerOf<Self> {
2526
Reduce { state, action in
@@ -56,16 +57,17 @@ struct CounterFeature {
5657

5758
case .toggleTimerButtonTapped:
5859
state.isTimerRunning.toggle()
60+
let cancelID = CancelID.timer(state.timerID)
5961
if state.isTimerRunning {
6062
return .run { send in
6163
while true {
6264
try await Task.sleep(for: .seconds(1))
6365
await send(.timerTick)
6466
}
6567
}
66-
.cancellable(id: CancelID.timer)
68+
.cancellable(id: cancelID)
6769
} else {
68-
return .cancel(id: CancelID.timer)
70+
return .cancel(id: cancelID)
6971
}
7072
}
7173
}

0 commit comments

Comments
 (0)