Dependência RxSwift

Se você já usou o RxSwift, está familiarizado com este trecho de código:
var taskChangeEvent = BehaviorSubject<Bool>()
...
if newValue != oldValue {
subject.onNext(newValue)
}
...
taskChangeEvent.subscribe(onNext: { [weak self] done in
....
}).dispose(by: disposeBag)
Para tornar esse processo um pouco mais simples e reduzir a dependência do RxSwift a um único local, construo uma classe simples apenas seguindo essas etapas.
import RxSwift
class EventSerializer<T: Equatable> {
private var disposeBag = DisposeBag()
fileprivate let contactPoint: BehaviorSubject<T>
var value: T
func send(_ newValue: T) {
contactPoint.onNext(newValue)
self.value = newValue
}
func sendValueIf(_ newValue: T, compareWith: (_ oldValue: T, _ newValue: T) -> Bool) {
if compareWith(self.value, newValue) {
contactPoint.onNext(newValue)
self.value = newValue
}
}
func listen(onChange: @escaping ((T) -> Void)) {
contactPoint.subscribe( onNext: { value in
onChange(value)
}).disposed(by: disposeBag)
}
init(defaultValue: T, contactPoint: BehaviorSubject<T>? = nil) {
self.contactPoint = contactPoint ?? BehaviorSubject<T>(value: defaultValue)
self.value = defaultValue
}
}
A lógica de disparo está dentro da classe e você pode dividir a lógica em uma função separada.
Ouvir o stream é muito mais limpo e não há descarteBag em todo o código.
var task = EventSerilizer<Bool>(defaultValue: false)
...
func shouldInformTaskChange(_ oldValue: Bool, _ newValue: Bool) {
oldValue != newValue
}
task.sendValueIf(true, compareWith: shouldInforTaskChange)
...
task.listen { [weak self] value in
....
}
func listenToCombineLatest<E: Equatable>(with otherSerializer: EventSerializer<E>, onChange: @escaping ((T, E) -> Void)) {
Observable.combineLatest(self.contactPoint, otherSerializer.contactPoint).subscribe(onNext: { tValue, eValue in
onChange(tValue, eValue)
}).disposed(by: disposeBag)
}
Uma nota lateral sobre o uso de BehaviorSubject
Como você sabe o BehaviorSubject emite o último evento para quem começa a ouvir. Isso pode ser um problema, durante a fase de instalação, porque é como um encanamento enquanto há água nos canos. Você é visitado enquanto ainda não está pronto. Por outro lado, o PublishSubject permite conectar pipes e enviar os eventos.
Portanto, considere usar PublishSubject se precisar de algum tempo de preparação antes de receber eventos.