Binder
Observer that enforces interface binding rules:
* can’t bind errors (in debug builds binding of errors causes fatalError
in release builds errors are being logged)
* ensures binding is performed on a specific scheduler
Binder
doesn’t retain target and in case target is released, element isn’t bound.
By default it binds elements on main scheduler.
public struct Binder: ObserverType
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public typealias Element = Value
private let _binding: (Event<Value>) -> Void
public init<Target: AnyObject>(_ target: Target, scheduler: ImmediateSchedulerType = MainScheduler(), binding: @escaping (Target, Value) -> Void) {
weak var weakTarget = target
self._binding = { event in
switch event {
case .next(let element):
_ = scheduler.schedule(element) { element in
if let target = weakTarget {
binding(target, element)
}
return Disposables.create()
}
case .error(let error):
bindingError(error)
case .completed:
break
}
}
}
/// Binds next element to owner view as described in `binding`.
public func on(_ event: Event<Value>) {
self._binding(event)
}
/// Erases type of observer.
///
/// - returns: type erased observer.
public func asObserver() -> AnyObserver<Value> {
return AnyObserver(eventHandler: self.on)
}