以上是总结的Observable核心逻辑。可能时序关系不正确。希望大家留言指正。
1:ObservableType.swift
public protocol ObservableType: ObservableConvertibleType {
func subscribe<Observer: ObserverType>(_ observer: Observer) -> Disposable where Observer.Element == Element
}
//这里subscribe在子类里具体实现。
在父类里定义E= Element
/// Type that can be converted to observable sequence (`Observable<Element>`).
public protocol ObservableConvertibleType {
/// Type of elements in sequence.
associatedtype Element
@available(*, deprecated, message: "Use `Element` instead.")
typealias E = Element //后面就用<E>代替
/// Converts `self` to `Observable` sequence.
///
/// - returns: Observable sequence that represents `self`.
func asObservable() -> Observable<Element>
}
2:Creat.swift
func on(_ event: Event<Element>) {
switch event {
case .next:
if load(self._isStopped) == 1 {
return
}
self.forwardOn(event)
case .error, .completed:
if fetchOr(self._isStopped, 1) == 0 {
self.forwardOn(event)
self.dispose()
}
}
}//赋予on方法具体咋样现在还是有点蒙蒙的。需要在学两遍。稍后完成补充。
AnonymousObservable.jpg
3:匿名可观察序列
final private class AnonymousObservable<Element>: Producer<Element>
很重要,提供订阅、调度者和销毁功能。
只提供定义
提供协议扩展功能
4:自己的计数逻辑
RxSwift拥有自己的计数逻辑
init(observer: Observer, cancel: Cancelable) {
#if TRACE_RESOURCES
_ = Resources.incrementTotal()
#endif
deinit {
#if TRACE_RESOURCES
_ = Resources.decrementTotal()
#endif
网友评论