Skip to content

Commit d31d81d

Browse files
author
Pablo Orgaz
committed
Remove default dispatcher constructor, cleanup unused code
1 parent e2ec772 commit d31d81d

File tree

9 files changed

+61
-68
lines changed

9 files changed

+61
-68
lines changed

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
buildscript {
22
ext {
3-
kotlin_version = "1.3.41"
4-
mini_version = "4.0.4"
3+
kotlin_version = "1.3.50"
4+
mini_version = "4.0.5"
55
}
66

77
repositories {
@@ -11,7 +11,7 @@ buildscript {
1111
}
1212

1313
dependencies {
14-
classpath "com.android.tools.build:gradle:3.4.2"
14+
classpath "com.android.tools.build:gradle:3.5.0"
1515
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1616
classpath "org.junit.platform:junit-platform-gradle-plugin:1.0.0"
1717
classpath "com.github.dcendents:android-maven-gradle-plugin:2.1"

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip

mini-common/src/main/AndroidManifest.xml

Lines changed: 0 additions & 11 deletions
This file was deleted.

mini-common/src/main/java/mini/ActionReducer.kt

Lines changed: 0 additions & 12 deletions
This file was deleted.

mini-common/src/main/java/mini/ActionUtils.kt

Lines changed: 0 additions & 26 deletions
This file was deleted.

mini-common/src/main/java/mini/Dispatcher.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,23 @@ import java.util.concurrent.atomic.AtomicInteger
66
import kotlin.reflect.KClass
77

88
/**
9-
* Hub for actions.
9+
* Hub for actions. Use code generation with ```MiniGen.newDispatcher()```
10+
* or provide action type map information.
1011
*
1112
* @param actionTypes All types an action can be observed as.
1213
* If map is empty, the runtime type itself will be used. If using code generation,
1314
* Mini.actionTypes will contain a map with all super types of @Action annotated classes.
1415
*/
15-
class Dispatcher(val actionTypes: Map<KClass<*>, List<KClass<*>>> = emptyMap()) {
16+
class Dispatcher(val actionTypes: Map<KClass<*>, List<KClass<*>>>) {
1617

1718
private val subscriptionCaller: Chain = object : Chain {
1819
override fun proceed(action: Any): Any {
1920
synchronized(subscriptions) {
20-
val types = actionTypes[action::class] ?: listOf(action::class)
21+
val types = actionTypes[action::class]
22+
?: error("Object $action [${action::class}] is not action, " +
23+
"register it in type map or use ```MiniGen.newDispatcher``` " +
24+
"if using " +
25+
"code generation")
2126
types.forEach { type ->
2227
subscriptions[type]?.forEach { it.fn(action) }
2328
}

mini-common/src/main/java/mini/Threading.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ val uiHandler by lazy {
88
Handler(Looper.getMainLooper())
99
}
1010

11-
fun assertNotOnUiThread() {
12-
if (!isAndroid) return
13-
if (Looper.myLooper() != Looper.getMainLooper()) {
14-
error("This method can not be called from the main application thread")
15-
}
16-
}
17-
1811
fun assertOnUiThread() {
1912
if (!isAndroid) return
2013
if (Looper.myLooper() != Looper.getMainLooper()) {

mini-common/src/test/kotlin/mini/DispatcherTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class DispatcherTest {
99

1010
@Test
1111
fun `subscriptions are added`() {
12-
val dispatcher = Dispatcher()
12+
val dispatcher = TestDispatcher()
1313
var called = 0
1414
dispatcher.subscribe<TestAction> {
1515
called++
@@ -20,7 +20,7 @@ class DispatcherTest {
2020

2121
@Test
2222
fun `order is respected for same priority`() {
23-
val dispatcher = Dispatcher()
23+
val dispatcher = TestDispatcher()
2424
val calls = ArrayList<Int>()
2525
dispatcher.subscribe<TestAction> {
2626
calls.add(0)
@@ -35,7 +35,7 @@ class DispatcherTest {
3535

3636
@Test
3737
fun `order is respected for different priority`() {
38-
val dispatcher = Dispatcher()
38+
val dispatcher = TestDispatcher()
3939
val calls = ArrayList<Int>()
4040
dispatcher.subscribe<TestAction>(priority = 10) {
4141
calls.add(0)
@@ -50,7 +50,7 @@ class DispatcherTest {
5050

5151
@Test
5252
fun `disposing registration removes subscription`() {
53-
val dispatcher = Dispatcher()
53+
val dispatcher = TestDispatcher()
5454
var called = 0
5555
dispatcher.subscribe<TestAction> {
5656
called++
@@ -61,7 +61,7 @@ class DispatcherTest {
6161

6262
@Test
6363
fun `interceptors are called`() {
64-
val dispatcher = Dispatcher()
64+
val dispatcher = TestDispatcher()
6565
var called = 0
6666
val interceptor: Interceptor = { action, chain ->
6767
called++
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package mini
2+
3+
import kotlin.reflect.KClass
4+
import kotlin.reflect.jvm.jvmErasure
5+
6+
fun TestDispatcher(): Dispatcher {
7+
return Dispatcher(newReflectiveMap())
8+
}
9+
10+
private fun reflectActionTypes(type: KClass<*>, depth: Int = 0): List<ReflectedType> {
11+
return type.supertypes
12+
.asSequence()
13+
.map { (it.jvmErasure.java as Class<*>).kotlin }
14+
.map { reflectActionTypes(it, depth + 1) }
15+
.flatten()
16+
.plus(ReflectedType(type, depth))
17+
.toList()
18+
}
19+
20+
private class ReflectedType(val clazz: KClass<*>, val depth: Int)
21+
22+
private fun newReflectiveMap(): Map<KClass<*>, List<KClass<*>>> {
23+
return object : Map<KClass<*>, List<KClass<*>>> {
24+
private val genericTypes = listOf(Object::class)
25+
private val map = HashMap<KClass<*>, List<KClass<*>>>()
26+
override val entries: Set<Map.Entry<KClass<*>, List<KClass<*>>>> = map.entries
27+
override val keys: Set<KClass<*>> = map.keys
28+
override val size: Int = map.size
29+
override val values: Collection<List<KClass<*>>> = map.values
30+
override fun containsKey(key: KClass<*>): Boolean = map.containsKey(key)
31+
override fun containsValue(value: List<KClass<*>>): Boolean = map.containsValue(value)
32+
override fun isEmpty(): Boolean = map.isEmpty()
33+
override fun get(key: KClass<*>): List<KClass<*>>? {
34+
return map.getOrPut(key) {
35+
reflectActionTypes(key)
36+
.asSequence()
37+
.sortedBy { it.depth }
38+
.map { it.clazz }
39+
.filter { it !in genericTypes }
40+
.toList()
41+
}
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)