diff --git a/Examples/Counter/Counter/Counter.swift b/Examples/Counter/Counter/Counter.swift index 390c831..85d066a 100644 --- a/Examples/Counter/Counter/Counter.swift +++ b/Examples/Counter/Counter/Counter.swift @@ -19,6 +19,7 @@ struct Counter: Dripper { @Observable final class State { var counter: Int = .zero + var text = "" } enum Action { @@ -40,13 +41,14 @@ struct Counter: Dripper { case .resetCounter: state.counter = .zero case .randomNumber: - return .run { pour in + return .run { _ in func randomNumber() async throws -> Int { try await Task.sleep(for: .seconds(1)) return Int.random(in: 0...10) } let randomNumber = try await randomNumber() - await pour(.decreaseCounter) + // FIXME: Data Race +// await pour(.decreaseCounter) state.counter = randomNumber } } @@ -103,6 +105,14 @@ struct CounterView: View { .padding() .background(.regularMaterial) .clipShape(.rect(cornerRadius: 10.0)) + + TextField(text: station.bind(\.text)) { + Text("Enter you name here") + } + .textFieldStyle(.roundedBorder) + .padding() + + Text(station.text) } .font(.headline) } diff --git a/Sources/Dripper/Station.swift b/Sources/Dripper/Station.swift index 33731fe..721268d 100644 --- a/Sources/Dripper/Station.swift +++ b/Sources/Dripper/Station.swift @@ -6,6 +6,7 @@ // import Foundation +import SwiftUI public typealias StationOf = Station @@ -70,3 +71,16 @@ public final class Station { } } } + +extension Station where State: AnyObject { + public func bind(_ dynamicMember: WritableKeyPath) -> Binding { + Binding( + get: { + self.state[keyPath: dynamicMember] + }, + set: { newValue in + self.state[keyPath: dynamicMember] = newValue + } + ) + } +}