实现简单的 RxKotlin (上)

在 Rx 里面,实现的想法是 有一个数据源头,然后观察者去关注,得到数据的回调,中间可能通过很多链式的操作符,简单优美。
Observable 是 数据源,Subscriber Observer 是观察者。
Observable 构造方法有个参数 OnSubscribe,这个接口需要实现一个 call 方法,在里面执行数据库查询、 网络请求之类的行为,并在里面 调用 Subscriber 的 onNext onCompleted onError。
基于上面的知识 我们可以完成一个初级版的demo。

  • 新建观察者 数据源
//Observer.kt
interface Observer {
    fun onCompleted()
    fun onError(t : Throwable)
    fun onNext(t: T)
}

//Subscription.kt
interface Subscription {
    fun unsubscribe()
    fun isUnsubscribed(): Boolean
}

//Subscriber.kt
abstract class Subscriber : Observer, Subscription{

    private var subscribe = true

    open fun onStart() {}

    override fun unsubscribe() {
        this.subscribe = false
    }

    override fun isUnsubscribed(): Boolean  = !subscribe
}

//Observable.kt
class Observable(private var onSubscribe: OnSubscribe?) {
    interface OnSubscribe {
        fun call(subscriber: Subscriber)
    }
}
  • createjustsubscribe
//Observable.kt
class Observable(private var onSubscribe: OnSubscribe?) {

    companion object {

        fun  create(subscribe: OnSubscribe): Observable {
            return Observable(subscribe)
        }

        fun  just(list: List): Observable {
            return create(object : OnSubscribe {
                override fun call(subscriber: Subscriber) {
                    try {
                        for (t in list) {
                            if (!subscriber.isUnsubscribed())
                                subscriber.onNext(t)
                            else
                                break
                        }
                    }catch (e: Exception) {
                        subscriber.onError(e)
                    }
                    subscriber.onCompleted()
                }
            })
        }

        fun  just(vararg ts : T): Observable {
            return just(ts.asList())
        }
    }

    fun subscribe(observer: Observer): Subscription{
        return if (observer is Subscriber)
            subscribe(observer)
        else {
            val subscriber = ObserverSubscriber(observer)
            subscribe(subscriber)
        }
    }

    fun subscribe(subscriber: Subscriber): Subscription{
        subscriber.onStart()
        onSubscribe?.call(subscriber)
        return subscriber
    }
}

//ObserverSubscriber.kt
class ObserverSubscriber(private var observer: Observer) : Subscriber(){

    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() {
            override fun onCompleted() {

            }

            override fun onError(t: Throwable) {
            }

            override fun onNext(t: Int) {
                System.out.println(t)
            }
        })
}

mapfilter

//OnSubscribeFilter.kt
class OnSubscribeFilter(private var source: Observable, private var predicate: (T) -> Boolean) : Observable.OnSubscribe {

    override fun call(subscriber: Subscriber) {
        source.subscribe(object : Subscriber(){
            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(private var source: Observable, private var transformer: (T) -> R) : Observable.OnSubscribe {

    override fun call(subscriber: Subscriber) {
        source.subscribe(object : Subscriber(){
            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  map(transformer: (t: T) -> R): Observable {
    return create(OnSubscribeMap(this, transformer))
}
//Observable.kt
fun filter(predicate: (t: T) -> Boolean): Observable {
    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() {
            override fun onCompleted() {

            }

            override fun onError(t: Throwable) {
            }

            override fun onNext(t: Int) {
                System.out.println(t)
            }
        })

至上 完成了最基本的 Rx 的 操作符,下篇将说到 线程切换方面的操作符。

你可能感兴趣的:(实现简单的 RxKotlin (上))