Skip to content

Commit 0ae84b6

Browse files
author
Pablo Orgaz
committed
Complete runtime variant, minor cleanup
1 parent 693f7a8 commit 0ae84b6

File tree

12 files changed

+121
-22
lines changed

12 files changed

+121
-22
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ dependencies {
7676
implementation fileTree(dir: 'libs', include: ['*.jar'])
7777

7878
implementation project(":mini-common")
79+
implementation project(":mini-runtime")
7980
kapt project(":mini-processor")
8081

8182
//Misc

app/src/main/java/org/sample/SampleActivity.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ class SampleActivity : AppCompatActivity(), SubscriptionTracker by DefaultSubscr
1818
Grove.plant(ConsoleLogTree())
1919
val stores = listOf(dummyStore)
2020

21-
MiniGen.initialize(dispatcher, stores)
21+
//MiniGen.initialize(dispatcher, stores)
22+
MiniRuntime.initialize(dispatcher, stores)
23+
2224
dispatcher.addInterceptor(LoggerInterceptor(stores, { tag, msg ->
2325
Grove.tag(tag).d { msg }
2426
}))

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ buildscript {
1111
}
1212

1313
dependencies {
14-
classpath 'com.android.tools.build:gradle:3.3.0'
14+
classpath 'com.android.tools.build:gradle:3.3.1'
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.triplet.gradle:play-publisher:1.2.0'

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package mini
22

3+
import io.reactivex.disposables.Disposable
34
import java.util.*
5+
import kotlin.reflect.KClass
46

57
/**
68
* Hub for actions.
@@ -55,10 +57,15 @@ class Dispatcher {
5557
}
5658

5759
@Suppress("UNCHECKED_CAST")
58-
inline fun <reified A> register(priority: Int = 100, noinline callback: (A) -> Unit): Registration {
60+
inline fun <reified A : Any> register(priority: Int = 100, noinline callback: (A) -> Unit): Registration {
61+
return register(A::class, priority, callback)
62+
}
63+
64+
@Suppress("UNCHECKED_CAST")
65+
fun <T : Any> register(clazz: KClass<T>, priority: Int = 100, callback: (T) -> Unit): Registration {
5966
synchronized(subscriptions) {
60-
val reg = Registration(A::class.java, priority, callback as (Any) -> Unit)
61-
val set = subscriptions.getOrPut(A::class.java) {
67+
val reg = Registration(this, clazz.java, priority, callback as (Any) -> Unit)
68+
val set = subscriptions.getOrPut(clazz.java) {
6269
TreeSet(kotlin.Comparator { a, b -> a.priority.compareTo(b.priority) })
6370
}
6471
set.add(reg)
@@ -97,5 +104,15 @@ class Dispatcher {
97104
onUi { dispatch(action) }
98105
}
99106

100-
data class Registration(val type: Class<*>, val priority: Int, val fn: (Any) -> Unit)
107+
data class Registration(val dispatcher: Dispatcher,
108+
val type: Class<*>,
109+
val priority: Int, val fn: (Any) -> Unit) : Disposable {
110+
var disposed = false
111+
override fun isDisposed(): Boolean = disposed
112+
113+
override fun dispose() {
114+
dispatcher.unregister(this)
115+
disposed = true
116+
}
117+
}
101118
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package mini
2+
3+
/**
4+
* Common interface for code generation and reflection approaches.
5+
*/
6+
interface MiniInitializer {
7+
fun initialize(dispatcher: Dispatcher, stores: List<Store<*>>)
8+
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ abstract class Store<S : Any> {
3131
get() = throw UnsupportedOperationException("This is a write only property")
3232
set(value) = setStateInternal(value)
3333

34-
/**
35-
* Same as property, suffix style
36-
*/
34+
/** Same as property, suffix style */
3735
protected fun S.newState(): S {
3836
setStateInternal(this)
3937
return this
@@ -82,18 +80,20 @@ abstract class Store<S : Any> {
8280
}
8381

8482
private fun setStateInternal(newState: S) {
85-
onUiSync {
86-
//Need state mutation to happen on UI thread
87-
if (newState != _state) {
88-
_state = newState
89-
processor.onNext(_state)
90-
}
83+
assertOnUiThread()
84+
//Need state mutation to happen on UI thread
85+
if (newState != _state) {
86+
_state = newState
87+
processor.onNext(_state)
9188
}
9289
}
9390

91+
/** Thread safe, since espresso runs in it's own thread */
9492
@TestOnly
9593
fun setTestState(other: S) {
96-
setStateInternal(other)
94+
onUiSync {
95+
setStateInternal(other)
96+
}
9797
}
9898

9999
@TestOnly

mini-processor/src/main/java/mini/processor/MiniProcessor.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mini.processor
33
import com.squareup.kotlinpoet.FileSpec
44
import com.squareup.kotlinpoet.TypeSpec
55
import mini.Action
6+
import mini.MiniInitializer
67
import mini.Reducer
78
import javax.annotation.processing.*
89
import javax.lang.model.SourceVersion
@@ -35,7 +36,8 @@ class MiniProcessor : AbstractProcessor() {
3536
val className = "MiniGen"
3637
val file = FileSpec.builder("mini", className)
3738
val container = TypeSpec.objectBuilder(className)
38-
container.addKdoc("Automatically generated, do not edit.\n")
39+
.addSuperinterface(MiniInitializer::class)
40+
.addKdoc("Automatically generated, do not edit.\n")
3941

4042
//Get non-abstract actions
4143
ActionTypesGenerator.generate(container, roundActions)

mini-processor/src/main/java/mini/processor/ReducersGenerator.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ object ReducersGenerator {
5353
.build()
5454

5555
val initDispatcherFn = FunSpec.builder("initialize")
56+
.addModifiers(KModifier.OVERRIDE)
5657
.addParameter("dispatcher", Dispatcher::class)
5758
.addParameter("stores", listOfStoresTypeName)
5859
.addCode(CodeBlock.builder()

mini-runtime/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ apply from: "../jitpack.gradle"
33

44
dependencies {
55
compile project(":mini-common")
6+
compileOnly group: 'com.google.android', name: 'android', version: '4.1.1.4'
67
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
78
compile 'com.github.yanex:takenoko:0.1'
89
compile 'io.github.classgraph:classgraph:4.6.32'

mini-runtime/src/main/java/com/minikorp/runtime/MyClass.java

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

0 commit comments

Comments
 (0)