Skip to content

Commit

Permalink
Fix issue pointfreeco#3417: Add unique ID for independent timer opera…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
JunBook authored and JunBook committed Sep 30, 2024
1 parent 4c08664 commit 3043d10
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct CounterFeature {
var fact: String?
var isLoading = false
var isTimerRunning = false
let timerID = UUID()
}

enum Action {
Expand All @@ -19,7 +20,7 @@ struct CounterFeature {
case toggleTimerButtonTapped
}

enum CancelID { case timer }
enum CancelID: Hashable { case timer(UUID) }

var body: some ReducerOf<Self> {
Reduce { state, action in
Expand Down Expand Up @@ -56,16 +57,17 @@ struct CounterFeature {

case .toggleTimerButtonTapped:
state.isTimerRunning.toggle()
let cancelID = CancelID.timer(state.timerID)
if state.isTimerRunning {
return .run { send in
while true {
try await Task.sleep(for: .seconds(1))
await send(.timerTick)
}
}
.cancellable(id: CancelID.timer)
.cancellable(id: cancelID)
} else {
return .cancel(id: CancelID.timer)
return .cancel(id: cancelID)
}
}
}
Expand Down

0 comments on commit 3043d10

Please sign in to comment.