This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let levelsSpin = Spinner | |
| .initialState(Levels(left: 10, right: 20)) | |
| .feedback(Feedback(effect: leftEffect)) | |
| .feedback(Feedback(effect: rightEffect)) | |
| .reducer(Reducer(levelsReducer)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func leftEffect(inputLevels: Levels) -> AnyPublisher<Event, Never> { | |
| // this is the stop condition to our Spin | |
| guard inputLevels.left != inputLevels.right else { return Empty().eraseToAnyPublisher() } | |
| // this is the regulation for the left level | |
| if inputLevels.left < inputLevels.right { | |
| return Just(.increaseLeft).eraseToAnyPublisher() | |
| } else { | |
| return Just(.decreaseLeft).eraseToAnyPublisher() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func leftEffect(inputLevels: Levels) -> SignalProducer<Event, Never> { | |
| // this is the stop condition to our Spin | |
| guard inputLevels.left != inputLevels.right else { return .empty } | |
| // this is the regulation for the left level | |
| if inputLevels.left < inputLevels.right { | |
| return SignalProducer(value: .increaseLeft) | |
| } else { | |
| return SignalProducer(value: .decreaseLeft) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func leftEffect(inputLevels: Levels) -> Observable<Event> { | |
| // this is the stop condition to our Spin | |
| guard inputLevels.left != inputLevels.right else { return .empty() } | |
| // this is the regulation for the left level | |
| if inputLevels.left < inputLevels.right { | |
| return .just(.increaseLeft) | |
| } else { | |
| return .just(.decreaseLeft) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func levelsReducer(currentLevels: Levels, event: Event) -> Levels { | |
| guard currentLevels.left != currentLevels.right else { return currentLevels } | |
| switch event { | |
| case .decreaseLeft: | |
| return Levels(left: currentLevels.left-1, right: currentLevels.right) | |
| case .increaseLeft: | |
| return Levels(left: currentLevels.left+1, right: currentLevels.right) | |
| case .decreaseRight: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum Event { | |
| case increaseLeft | |
| case decreaseLeft | |
| case increaseRight | |
| case decreaseRight | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Levels { | |
| let left: Int | |
| let right: Int | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // the State of the feedback loop | |
| struct Levels { | |
| let left: Int | |
| let right: Int | |
| } | |
| // the transition requests of the feedback loop | |
| enum Event { | |
| case increaseLeft | |
| case decreaseLeft |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // the State of the feedback loop | |
| struct Levels { | |
| let left: Int | |
| let right: Int | |
| } | |
| // the transition requests of the feedback loop | |
| enum Event { | |
| case increaseLeft | |
| case decreaseLeft |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Combine | |
| // When wrapping a value, we take advantage of the setter | |
| // to feed a PassthroughSubject | |
| @propertyWrapper | |
| struct Published<T> { | |
| private var innerValue: T | |
| private let innerSubject = PassthroughSubject<T, Never>() | |