RxProperty
A get-only BehaviorRelay
that is (almost) equivalent to ReactiveSwift's Property
.
This class is highly useful to encapsulate BehaviorRelay
inside ViewModel so that other classes cannot reach its setter (unbindable) and make state management safer.
In short:
public final class 💩ViewModel<T> {
// 💩 This is almost the same as `var state: T`.
// DON'T EXPOSE VARIABLE!
public let state = BehaviorRelay<T>(...)
// 💩 Exposing `Observable` is NOT a good solution either
// because type doesn't tell us that it contains at least one value
// and we cannot even "extract" it.
public var stateObservable: Observable<T> {
return state.asObservable()
}
}
public protocol 👍ViewModelProtocol {
associatedtype T
var state: RxProperty<T> { get }
}
public final class 👍ViewModel<T>: 👍ViewModelProtocol {
@ReadWrite public let state: RxProperty<T>
public init(initialValue: T) {
state = RxProperty(initialValue)
//...
}
private func setState(to newValue: T) {
_state.accept(newValue)
}
}
Disclaimer
Since this library should rather go to RxSwift-core, there is no plan to release as a 3rd-party microframework for CocoaPods/Carthage /SwiftPackageManager.
(Swift Package Manager is supported in inamiy/RxProperty#5)
If you like it, please upvote ReactiveX/RxSwift#1118 and join the discussion. Current recommended way is to directly use the source code to your project.