In fact, store-pattern
doesn't need something library to run.
The actually necessary thing is the changing detection in UIKit.
Without the changing detection, the code is here. There is no dependencies.
class MyView: UIView {
private struct State {
var count: Int = 0
}
private var state: State {
didSet {
update(with: state)
}
}
private func update(with state: State) {
...
}
}
Next, we focus on update(with:)
method.
Try to simulate updating the label's value.
private func update(with state: State) {
myLabel.text = "\\(state.count)"
}
As you can see, you will think you want to prevent updating the value until the value changed.
With installing Verge/Tiny
module, we can write up like followings.
private func update(with state: State) {
associatedProperties.doIfChanged(state.count) { count in
myLabel.text = "\\(count)"
}
}
associatedProperties
is a storage of the values that associated with its owner object(NSObject).