Skip to content

Commit

Permalink
some optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
qiaoyuang committed Aug 12, 2019
1 parent 89b6805 commit bcacdc9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 34 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ dependencies {
// Android 支持包
implementation "androidx.legacy:legacy-support-v4:1.0.0"
implementation "androidx.appcompat:appcompat:1.0.2"
implementation "com.google.android.material:material:1.1.0-alpha08"
implementation "com.google.android.material:material:1.1.0-alpha09"
implementation "androidx.recyclerview:recyclerview:1.0.0"
implementation "androidx.cardview:cardview:1.0.0"
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.w10group.hertzdictionary.core

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.launch
import java.util.LinkedList
import kotlin.reflect.KClass

Expand All @@ -12,10 +10,10 @@ import kotlin.reflect.KClass
* @author Qiao
*/

object CoroutinesBus {
object CoroutineBus {

interface OnWorkListener<in T> {
fun onWork(channel: ReceiveChannel<T>)
fun onWork(t: T)
}

private val map = HashMap<KClass<*>, LinkedList<out Pair<OnWorkListener<*>, Channel<*>>>>()
Expand All @@ -28,39 +26,34 @@ object CoroutinesBus {
private fun <T : Any> get(key: KClass<T>) = map[key] as LinkedList<Pair<OnWorkListener<T>, Channel<T>>>?

fun <T : Any> register(clazz: KClass<T>, observer: OnWorkListener<T>, capacity: Int = 0) {
var list = get(clazz)
if (list == null) {
list = LinkedList()
put(clazz, list)
}
list.add(observer to Channel(capacity))
get(clazz) ?: LinkedList<Pair<OnWorkListener<T>, Channel<T>>>().also { put(clazz, it) }
.add(observer to Channel(capacity))
}

inline fun <reified T : Any> register(observer: OnWorkListener<T>,
capacity: Int = 0) = register(T::class, observer, capacity)

fun <T : Any> unRegister(clazz: KClass<T>, observer: OnWorkListener<T>) {
val list = get(clazz)
list?.let { linkedList ->
linkedList.forEach {
if (it === observer) {
linkedList.remove(it)
return
}
}
if (linkedList.isEmpty())
map.remove(clazz)
}
fun <T : Any> unRegister(clazz: KClass<T>, observer: OnWorkListener<T>) = get(clazz)?.let { list ->
list.find { it === observer }?.let { list.remove(it) }
if (list.isEmpty())
map.remove(clazz)
}

inline fun <reified T : Any> unRegister(observer: OnWorkListener<T>) = unRegister(T::class, observer)

fun <T : Any> CoroutineScope.post(clazz: KClass<T>, event: T) = get(clazz)?.forEach {
val (onWorkListener, channel) = it
launch { channel.send(event) }
onWorkListener.onWork(channel)
}

inline fun <reified T : Any> CoroutineScope.post(event: T) = post(T::class, event)
suspend fun <T : Any> post(clazz: KClass<T>, event: T) = coroutineScope {
get(clazz)?.forEach {
val (onWorkListener, channel) = it
launch(Dispatchers.Default) {
val deferred = async(Dispatchers.Default) { channel.receive() }
channel.send(event)
withContext(coroutineContext) {
onWorkListener.onWork(deferred.await())
}
}
}
}

suspend inline fun <reified T : Any> post(event: T) = post(T::class, event)

}
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

buildscript {
ext.kotlin_version = '1.3.41'
ext.coroutines_version = '1.2.2'
ext.coroutines_version = '1.3.0-RC2'
ext.anko_version = '0.10.8'
ext.room_version = '2.2.0-alpha01'
ext.room_version = '2.2.0-alpha02'
ext.glide_version = '4.9.0'
repositories {
google()
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.5-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-all.zip

0 comments on commit bcacdc9

Please sign in to comment.