Skip to content

Commit 693f7a8

Browse files
author
Pablo Orgaz
committed
Simplify codegen, prepare to include runtime variant
1 parent 15398b8 commit 693f7a8

File tree

10 files changed

+72
-27
lines changed

10 files changed

+72
-27
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ import mini.*
99

1010
class SampleActivity : AppCompatActivity(), SubscriptionTracker by DefaultSubscriptionTracker() {
1111

12-
private val dispatcher = Dispatcher(Mini.actionTypes)
13-
private val dummyStore = DummyStore(dispatcher)
12+
private val dispatcher = Dispatcher()
13+
private val dummyStore = DummyStore()
1414

1515
override fun onCreate(savedInstanceState: Bundle?) {
1616
super.onCreate(savedInstanceState)
1717
setContentView(R.layout.home_activity)
1818
Grove.plant(ConsoleLogTree())
1919
val stores = listOf(dummyStore)
2020

21-
Mini.register(dispatcher, stores)
21+
MiniGen.initialize(dispatcher, stores)
2222
dispatcher.addInterceptor(LoggerInterceptor(stores, { tag, msg ->
2323
Grove.tag(tag).d { msg }
2424
}))
@@ -48,17 +48,17 @@ data class ActionOne(override val text: String) : ActionInterface, SampleAbstrac
4848
@Action class ActionTwo(val text: String)
4949

5050
data class DummyState(val text: String = "dummy")
51-
class DummyStore(dispatcher: Dispatcher) : Store<DummyState>(dispatcher) {
51+
class DummyStore : Store<DummyState>() {
5252

5353
@Reducer fun onInterfaceAction(a: ActionInterface) {
5454

5555
}
5656

5757
@Reducer fun onSampleAction(a: ActionOne) {
58-
setState(state.copy(text = a.text))
58+
newState = state.copy(text = a.text)
5959
}
6060

6161
@Reducer fun anotherAction(a: ActionTwo) {
62-
setState(state.copy(text = a.text))
62+
state.copy(text = a.text).newState()
6363
}
6464
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import java.util.*
99
* If map is empty, the runtime type itself will be used. If using code generation,
1010
* Mini.actionTypes will contain a map with all super types of @Action annotated classes.
1111
*/
12-
class Dispatcher(val actionTypes: Map<Class<*>, List<Class<*>>> = emptyMap()) {
12+
class Dispatcher {
13+
14+
var actionTypes: Map<Class<*>, List<Class<*>>> = emptyMap()
1315

1416
private val subscriptionCaller: Chain = object : Chain {
1517
override fun proceed(action: Any): Any {

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import java.lang.reflect.ParameterizedType
88
/**
99
* State holder.
1010
*/
11-
abstract class Store<S : Any>(val dispatcher: Dispatcher) {
11+
abstract class Store<S : Any> {
1212

1313
companion object {
1414
const val INITIALIZE_ORDER_PROP = "store.init.order"
@@ -21,17 +21,33 @@ abstract class Store<S : Any>(val dispatcher: Dispatcher) {
2121

2222
private var _state: S? = null
2323

24-
fun setState(state: S) {
24+
/** Set new state, equivalent to [newState]*/
25+
protected fun setState(state: S) {
2526
setStateInternal(state)
2627
}
2728

28-
fun setState(fn: S.() -> Unit) {
29+
/** Hook for write only property */
30+
protected var newState: S
31+
get() = throw UnsupportedOperationException("This is a write only property")
32+
set(value) = setStateInternal(value)
2933

34+
/**
35+
* Same as property, suffix style
36+
*/
37+
protected fun S.newState(): S {
38+
setStateInternal(this)
39+
return this
3040
}
3141

3242
val state: S
3343
get() {
34-
if (_state == null) _state = initialState()
44+
if (_state == null) {
45+
synchronized(this) {
46+
if (_state == null) {
47+
_state = initialState()
48+
}
49+
}
50+
}
3551
return _state!!
3652
}
3753

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ object ActionTypesGenerator {
2020
.parameterizedBy(anyClassTypeName, listTypeName)
2121

2222
val prop = PropertySpec.builder("actionTypes", mapType)
23-
.addAnnotation(JvmField::class)
23+
.addModifiers(KModifier.PRIVATE)
2424
//⇤⇥«»
2525
.initializer(CodeBlock.builder()
2626
.add("mapOf(\n")

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ class MiniProcessor : AbstractProcessor() {
3232

3333
if (roundActions.isEmpty()) return false
3434

35-
val file = FileSpec.builder("mini", "Mini")
36-
val container = TypeSpec.objectBuilder("Mini")
35+
val className = "MiniGen"
36+
val file = FileSpec.builder("mini", className)
37+
val container = TypeSpec.objectBuilder(className)
3738
container.addKdoc("Automatically generated, do not edit.\n")
3839

3940
//Get non-abstract actions

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ object ReducersGenerator {
1616

1717
val whenBlock = CodeBlock.builder()
1818
//⇤⇥«»
19-
.beginControlFlow("stores.forEach { store ->")
2019
.addStatement("when (store) {").indent()
2120
.apply {
2221
reducers.forEach { (storeName, reducers) ->
@@ -32,29 +31,39 @@ object ReducersGenerator {
3231
}
3332
}
3433
.unindent().addStatement("}")
35-
.endControlFlow()
3634
.build()
3735

3836
val storeTypeName = Store::class.asTypeName().parameterizedBy(STAR)
3937
val listOfStoresTypeName = List::class.asTypeName().parameterizedBy(storeTypeName)
4038

41-
val registerListFunction = FunSpec.builder("register")
42-
.addAnnotation(JvmStatic::class)
39+
val registerListFn = FunSpec.builder("register")
40+
.addModifiers(KModifier.PRIVATE)
4341
.addParameter("dispatcher", Dispatcher::class)
4442
.addParameter("stores", listOfStoresTypeName)
45-
.addCode(whenBlock)
43+
.beginControlFlow("stores.forEach { store ->")
44+
.addStatement("register(dispatcher, store)")
45+
.endControlFlow()
4646
.build()
4747

48-
val registerOneFunction = FunSpec.builder("register")
49-
.addAnnotation(JvmStatic::class)
48+
val registerOneFn = FunSpec.builder("register")
49+
.addModifiers(KModifier.PRIVATE)
5050
.addParameter("dispatcher", Dispatcher::class)
5151
.addParameter("store", storeTypeName)
52-
.addCode(CodeBlock.of(
53-
"register(dispatcher, listOf(store))"
54-
)).build()
52+
.addCode(whenBlock)
53+
.build()
54+
55+
val initDispatcherFn = FunSpec.builder("initialize")
56+
.addParameter("dispatcher", Dispatcher::class)
57+
.addParameter("stores", listOfStoresTypeName)
58+
.addCode(CodeBlock.builder()
59+
.addStatement("dispatcher.actionTypes = actionTypes")
60+
.addStatement("register(dispatcher, stores)")
61+
.build())
62+
.build()
5563

56-
container.addFunction(registerOneFunction)
57-
container.addFunction(registerListFunction)
64+
container.addFunction(registerOneFn)
65+
container.addFunction(registerListFn)
66+
container.addFunction(initDispatcherFn)
5867

5968
}
6069
}

mini-runtime/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

mini-runtime/build.gradle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apply plugin: 'kotlin'
2+
apply from: "../jitpack.gradle"
3+
4+
dependencies {
5+
compile project(":mini-common")
6+
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
7+
compile 'com.github.yanex:takenoko:0.1'
8+
compile 'io.github.classgraph:classgraph:4.6.32'
9+
10+
testCompile 'junit:junit:4.12'
11+
testCompile 'com.google.testing.compile:compile-testing:0.15'
12+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.minikorp.runtime;
2+
3+
public class MyClass {
4+
}

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
include ':mini-processor', ':mini-common', ':mini-android-testing', ':app'
1+
include ':mini-processor', ':mini-common', ':mini-android-testing', ':app', ':mini-runtime'

0 commit comments

Comments
 (0)