在 Rx 里面,实现的想法是 有一个数据源头,然后观察者去关注,得到数据的回调,中间可能通过很多链式的操作符,简单优美。
Observable 是 数据源,Subscriber Observer 是观察者。
Observable 构造方法有个参数 OnSubscribe,这个接口需要实现一个 call 方法,在里面执行数据库查询、 网络请求之类的行为,并在里面 调用 Subscriber 的 onNext onCompleted onError。
基于上面的知识 我们可以完成一个初级版的demo。
- 新建观察者 数据源
//Observer.kt
interface Observer<T> {
fun onCompleted()
fun onError(t : Throwable)
fun onNext(t: T)
}
//Subscription.kt
interface Subscription {
fun unsubscribe()
fun isUnsubscribed(): Boolean
}
//Subscriber.kt
abstract class Subscriber<T> : Observer<T>, Subscription{
private var subscribe = true
open fun onStart() {}
override fun unsubscribe() {
this.subscribe = false
}
override fun isUnsubscribed(): Boolean = !subscribe
}
//Observable.kt
class Observable<T>(private var onSubscribe: OnSubscribe<T>?) {
interface OnSubscribe<T> {
fun call(subscriber: Subscriber<T>)
}
}
- create , just, subscribe
//Observable.kt
class Observable<T>(private var onSubscribe: OnSubscribe<T>?) {
companion object {
fun <T> create(subscribe: OnSubscribe<T>): Observable<T> {
return Observable(subscribe)
}
fun <T> just(list: List<T>): Observable<T> {
return create(object : OnSubscribe<T> {
override fun call(subscriber: Subscriber<T>) {
try {
for (t in list) {
if (!subscriber.isUnsubscribed())
subscriber.onNext(t)
else
break
}
}catch (e: Exception) {
subscriber.onError(e)
}
subscriber.onCompleted()
}
})
}
fun <T> just(vararg ts : T): Observable<T> {
return just(ts.asList())
}
}
fun subscribe(observer: Observer<T>): Subscription{
return if (observer is Subscriber)
subscribe(observer)
else {
val subscriber = ObserverSubscriber(observer)
subscribe(subscriber)
}
}
fun subscribe(subscriber: Subscriber<T>): Subscription{
subscriber.onStart()
onSubscribe?.call(subscriber)
return subscriber
}
}
//ObserverSubscriber.kt
class ObserverSubscriber<T>(private var observer: Observer<T>) : Subscriber<T>(){
override fun onCompleted() {
observer.onCompleted()
}
override fun onError(t: Throwable) {
observer.onError(t)
}
override fun onNext(t: T) {
observer.onNext(t)
}
}
//UnitTest.kt
@Test
fun rx() {
Observable.just(1, 2, 3)
.subscribe(object : Subscriber<Int>() {
override fun onCompleted() {
}
override fun onError(t: Throwable) {
}
override fun onNext(t: Int) {
System.out.println(t)
}
})
}
map, filter
//OnSubscribeFilter.kt
class OnSubscribeFilter<T>(private var source: Observable<T>, private var predicate: (T) -> Boolean) : Observable.OnSubscribe<T> {
override fun call(subscriber: Subscriber<T>) {
source.subscribe(object : Subscriber<T>(){
override fun onCompleted() {
subscriber.onCompleted()
}
override fun onError(t: Throwable) {
subscriber.onError(t)
}
override fun onNext(t: T) {
val result = predicate(t)
if (result)
subscriber.onNext(t)
}
})
}
}
//OnSubscribeMap.kt
class OnSubscribeMap<T, R>(private var source: Observable<T>, private var transformer: (T) -> R) : Observable.OnSubscribe<R> {
override fun call(subscriber: Subscriber<R>) {
source.subscribe(object : Subscriber<T>(){
override fun onCompleted() {
subscriber.onCompleted()
}
override fun onError(t: Throwable) {
subscriber.onError(t)
}
override fun onNext(t: T) {
subscriber.onNext(transformer(t))
}
})
}
}
//Observable.kt
fun <R> map(transformer: (t: T) -> R): Observable<R> {
return create(OnSubscribeMap(this, transformer))
}
//Observable.kt
fun filter(predicate: (t: T) -> Boolean): Observable<T> {
return create(OnSubscribeFilter(this, predicate))
}
//UnitTest.kt
@Test
fun rx() {
Observable.just("1", "2", "3")
.map {
it.toInt() + 1
}
.filter {
it != 1
}
.subscribe(object : Subscriber<Int>() {
override fun onCompleted() {
}
override fun onError(t: Throwable) {
}
override fun onNext(t: Int) {
System.out.println(t)
}
})
至上 完成了最基本的 Rx 的 操作符,下篇将说到 线程切换方面的操作符。