### FeatureModel Example Source: https://github.com/pointfreeco/swift-clocks/blob/main/README.md An example model demonstrating how to use a clock for timer-based operations. It allows injecting a clock for testability. ```swift @MainActor class FeatureModel: ObservableObject { @Published var count = 0 let clock: any Clock var timerTask: Task? init(clock: any Clock) { self.clock = clock } func startTimerButtonTapped() { self.timerTask = Task { while true { try await self.clock.sleep(for: .seconds(1)) self.count += 1 } } } func stopTimerButtonTapped() { self.timerTask?.cancel() self.timerTask = nil } } ``` -------------------------------- ### Test AsyncSequence Timer with TestClock Source: https://github.com/pointfreeco/swift-clocks/blob/main/README.md This example demonstrates how to test the timer functionality using TestClock. It advances the clock to simulate time passing and asserts the expected count. ```swift func testTimer() async { let clock = TestClock() let model = FeatureModel(clock: clock) XCTAssertEqual(model.count, 0) model.startTimerButtonTapped() await clock.advance(by: .seconds(1)) XCTAssertEqual(model.count, 1) await clock.advance(by: .seconds(4)) XCTAssertEqual(model.count, 5) model.stopTimerButtonTapped() await clock.run() } ``` -------------------------------- ### ObservableObject with Clock Dependency Source: https://github.com/pointfreeco/swift-clocks/blob/main/README.md An example of an ObservableObject that uses a clock for timer-based operations. It includes methods to start and stop a timer that increments a count. ```swift @MainActor class FeatureModel: ObservableObject { @Published var count = 0 let clock: any Clock var timerTask: Task? init(clock: some Clock) { self.clock = clock } func incrementButtonTapped() { self.count += 1 } func decrementButtonTapped() { self.count -= 1 } func startTimerButtonTapped() { self.timerTask = Task { for await _ in self.clock.timer(interval: .seconds(1)) { self.count += 1 } } } func stopTimerButtonTapped() { self.timerTask?.cancel() self.timerTask = nil } } ``` -------------------------------- ### SwiftUI Feature with Task.sleep Source: https://github.com/pointfreeco/swift-clocks/blob/main/README.md This example shows a basic SwiftUI feature that uses Task.sleep for a delayed action. It's useful for understanding the need for controllable clocks. ```swift struct Feature: View { @State var message: String? var body: some View { VStack { if let message = self.message { Text(self.message) } } .task { do { try await Task.sleep(for: .seconds(5)) self.message = "Welcome!" } catch {} } } } ``` -------------------------------- ### SwiftUI Preview with ImmediateClock Source: https://github.com/pointfreeco/swift-clocks/blob/main/README.md Demonstrates how to use ImmediateClock in a SwiftUI preview to make time-based operations execute instantly. This speeds up iteration during development. ```swift struct Feature_Previews: PreviewProvider { static var previews: some View { Feature(clock: ImmediateClock()) } } ``` -------------------------------- ### TestClock Usage in Tests Source: https://github.com/pointfreeco/swift-clocks/blob/main/README.md Demonstrates how to use TestClock to control time and test asynchronous code. Advance the clock to simulate time passing and assert on model state changes. ```swift func testTimer() async { let clock = TestClock() let model = FeatureModel(clock: clock) XCTAssertEqual(model.count, 0) model.startTimerButtonTapped() // Advance the clock 1 second and prove that the model's // count incremented by one. await clock.advance(by: .seconds(1)) XCTAssertEqual(model.count, 1) // Advance the clock 4 seconds and prove that the model's // count incremented by 4. await clock.advance(by: .seconds(4)) XCTAssertEqual(model.count, 5) // Stop the timer, run the clock until there is no more // suspensions, and prove that the count did not increment. model.stopTimerButtonTapped() await clock.run() XCTAssertEqual(model.count, 5) } ``` -------------------------------- ### SwiftUI Feature with Environment Clock Source: https://github.com/pointfreeco/swift-clocks/blob/main/README.md Shows how to inject a clock into a SwiftUI view using environment values. This approach is useful for managing clock dependencies across a view hierarchy. ```swift struct Feature: View { @State var message: String? @Environment(\.continuousClock) var clock var body: some View { VStack { if let message = self.message { Text(self.message) } } .task { do { try await self.clock.sleep(for: .seconds(5)) self.message = "Welcome!" } catch {} } } } struct Feature_Previews: PreviewProvider { static var previews: some View { Feature() .environment(\.continuousClock, ImmediateClock()) } } ``` -------------------------------- ### SwiftUI Feature with Injectable Clock Source: https://github.com/pointfreeco/swift-clocks/blob/main/README.md This version of the SwiftUI feature accepts a clock dependency, allowing for controllable time-based operations. This is key for testing and previews. ```swift struct Feature: View { @State var message: String? let clock: any Clock var body: some View { VStack { if let message = self.message { Text(self.message) } } .task { do { try await self.clock.sleep(for: .seconds(5)) self.message = "Welcome!" } catch {} } } } ``` -------------------------------- ### Workaround Clock Existential Limitation with AnyClock Source: https://github.com/pointfreeco/swift-clocks/blob/main/README.md This snippet shows how to use AnyClock to pass clock existentials to APIs that cannot accept them directly, such as those in the Async Algorithms package. ```swift // ✅ for await _ in stream.debounce(for: .seconds(1), clock: AnyClock(self.clock)) { // ... } ``` -------------------------------- ### Create AsyncSequence Timer in Observable Object Source: https://github.com/pointfreeco/swift-clocks/blob/main/README.md Use this snippet to create an observable object that increments a value every second using an AsyncSequence-based timer. Requires a Clock conforming to Clock. ```swift @MainActor class FeatureModel: ObservableObject { @Published var count = 0 let clock: any Clock var timerTask: Task? init(clock: any Clock) { self.clock = clock } func startTimerButtonTapped() { self.timerTask = Task { for await _ in self.clock.timer(interval: .seconds(1)) { self.count += 1 } } } func stopTimerButtonTapped() { self.timerTask?.cancel() self.timerTask = nil } } ``` -------------------------------- ### XCTest with UnimplementedClock Source: https://github.com/pointfreeco/swift-clocks/blob/main/README.md This test demonstrates using UnimplementedClock to ensure that time-based operations are not unexpectedly invoked during specific test flows. It asserts that the clock is not used. ```swift func testIncrementDecrement() { let model = FeatureModel(clock: UnimplementedClock()) XCTAssertEqual(model.count, 0) self.model.incrementButtonTapped() XCTAssertEqual(model.count, 1) self.model.decrementButtonTapped() XCTAssertEqual(model.count, 0) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.