Created
January 9, 2017 21:52
-
-
Save srdanrasic/06224c55304e681a00e0953f356b527d to your computer and use it in GitHub Desktop.
Stateless Control
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
/// Example interface of a control that does not manage its own state. | |
public class Stepper { | |
/// Enum is usually a convenient type to represent action. | |
public enum Action { | |
case increase | |
case decrease | |
} | |
/// On the output is an action or intent describing what to do with the state. | |
/// Control never updates its own state. It only sends intents to model/logic layer. | |
public let action: Signal<Action> | |
/// On the input is the binding the updates displayed state. | |
/// Model/logic layer binds a signal to this property that represents a state to be displayed. | |
public let value: Bond<Int> | |
/// Internally control probably holds the state for rendering/optimization purposes, but | |
/// it never changes the state on its own (as a result of action happening). | |
private var _value: Int | |
} | |
func test() { | |
let stepper = Stepper() | |
stepper.action | |
.reduce(4) { value, action in // initial value is 4 | |
switch action { | |
case .decrease: | |
return value - 2 // on .decrease, subtract 2 | |
case .increase: | |
return value + 2 // on .increase, add 2 | |
} | |
} | |
.bind(to: stepper.value) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment