This file contains 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 | |
case increaseRight | |
case decreaseRight | |
} | |
// the side effects of the feedback loop | |
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() | |
} | |
} | |
func rightEffect(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 right level | |
if inputLevels.right < inputLevels.left { | |
return Just(.increaseRight).eraseToAnyPublisher() | |
} else { | |
return Just(.decreaseRight).eraseToAnyPublisher() | |
} | |
} | |
// the reducer of the feedback loop | |
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: | |
return Levels(left: currentLevels.left, right: currentLevels.right-1) | |
case .increaseRight: | |
return Levels(left: currentLevels.left, right: currentLevels.right+1) | |
} | |
} | |
// The builder way to build the Spin | |
let levelsSpin = Spinner | |
.initialState(Levels(left: 10, right: 20)) | |
.feedback(Feedback(effect: leftEffect)) | |
.feedback(Feedback(effect: rightEffect)) | |
.reducer(Reducer(levelsReducer)) | |
// The declarative way to build the Spin | |
let levelsSpin = CombineSpin(initialState: Levels(left: 10, right: 20), | |
reducer: Reducer(levelsReducer)) { | |
Feedback(effect: leftEffect) | |
Feedback(effect: rightEffect) | |
} | |
// to make a reactive stream from the Spin | |
AnyPublisher.stream(from: levelsSpin) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment