EventBus Kotlin implementation source code parsing

Introduction:

Use third-party frameworks may speed up the progress of project development, in order to enhance understanding of the principles EventBus framework and better grasp kotlin language, this time I use kotlin achieve eventbus basic functions: registration, cancellation of registration, messaging, the main thread, the child thread event delivery .

For a better understanding the following be described by the flowchart of several:

EventBus registration process

//注解注册
fun register(item: Object){
        //判断改对象是否已经注册
       var list : ArrayList<SubscrbileMethod>? = cacheMap?.get(item)
        if(list == null){
            list = findSubscrbileMethods(item)
            //将所有注解方法和对象绑定到map中
            list?.let { cacheMap?.put(item, it) }
        }
}

//获取改注解所有注解方法
fun findSubscrbileMethods(item:Object):ArrayList<SubscrbileMethod>{
        var list : ArrayList<SubscrbileMethod> = ArrayList()
        //找到类对象
        var clazz : Class<*> = item.`class`
        //循环查找父类带有注解的方法
        while(clazz != null){
            val name : String = clazz.name
            if(name.startsWith("java.") || name .startsWith("javax.")
              || name.startsWith("android.")){
                break
            }
            //找到类中所有方法
            var methods: Array<Method>? = clazz.declaredMethods
            methods?.forEach {
                var method = it
                //获取每一个方法所有Subscrbile注解
                var subscrbile=it.getAnnotation(Subscrbile::class.java)
                subscrbile?.let {
                    //找到注解参数
                    val types:Array<Class<*>> ? = method.parameterTypes
                    //只要一个参数的注解方法
                    if(types?.size == 1){
                        //获取注解参数
                        val threadMode : ThreadMode = it.threadMode
                        val subscrbileMethod : SubscrbileMethod = SubscrbileMethod(method,threadMode,types[0])
                        list.add(subscrbileMethod)
                    }else{
                        Log.w("EventBus","参数个数不只一个")
                    }
                }
            }
            clazz = clazz.superclass
        }
        return list
    }

Message communication flow

fun post(bean:EventBean){
        //变量cacheMap所有带有bean类型的方法并调用
        cacheMap?.forEach {
            var vm:Map.Entry<Object,List<SubscrbileMethod>> = it
            it.value.forEach {
                val subscrbileMethod: SubscrbileMethod = it
                //判断当前类型与传入类型关系
                if(it.type!!.isAssignableFrom(bean.javaClass)){
                    when(subscrbileMethod.mThreadMod){
                         ThreadMode.MAIN->{//主线程执行
                            if(Looper.myLooper() == Looper.getMainLooper()){//当前是在主线程
                                invoke(it,vm,bean)
                            }else{
                                handler?.post(Runnable {//handler切换到主线程
                                    invoke(it,vm,bean)
                                })
                            }
                        }
                        ThreadMode.BACKGROUND ->{//子线程执行
                            if(Looper.myLooper() != Looper.getMainLooper()){//当前是在子线程
                                invoke(it,vm,bean)
                            }else {
                                GlobalScope.launch(){//启动一个新的线程
                                    invoke(it,vm,bean)
                                }
                            }
                        }
                        else->{
                            Log.d(Tag,"can't match")
                        }
                    }
                }
            }
        }
    }

private fun invoke(subscrbileMethod: SubscrbileMethod, vm: Map.Entry<Object, List<SubscrbileMethod>>, bean: EventBean) {
        val method = subscrbileMethod.mMeath
        //调用注解方法
        method?.invoke(vm.key, bean)
}

Cancellation process

fun unregister(item: Object){
        var list : ArrayList<SubscrbileMethod>? = cacheMap?.get(item)
        list?.clear()
        cacheMap?.remove(item)
    }

to sum up:

Knock yourself again you will find there are many details to consider, especially when using kotlin some language expression for a moment think, but later used to find kotlin language to be powerful indeed easy to use than java.

Specific code: Portal

Published 92 original articles · won praise 27 · views 90000 +

Guess you like

Origin blog.csdn.net/zhuxingchong/article/details/93415929