Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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