Skip to content

Commit 4558739

Browse files
committed
Add architecture documentation
1 parent 6a5f4a7 commit 4558739

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

elmdroid/src/main/java/cz/inventi/elmdroid/ElmArchitecture.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,56 @@ import android.arch.lifecycle.LiveData
44
import io.reactivex.Observable
55
import io.reactivex.Single
66

7+
/** UI 'loop' implementation that 'runs" given [Component]. */
78
interface ComponentRuntime<STATE : State, in MSG : Msg> {
9+
/** Provides always up to date [State]. */
810
fun state(): LiveData<STATE>
11+
/** Dispatch [Msg] to handle. */
912
fun dispatch(msg: MSG)
13+
/** Stops the inner loop, no more state updates. */
1014
fun clear()
1115
}
1216

17+
/** Key class specifying all mayor pars of your elm architecture. */
1318
interface Component<STATE : State, MSG : Msg, CMD : Cmd> {
19+
/** Initial [State] that will be provided as soon as the [ComponentRuntime] created. */
1420
fun initState(): STATE
21+
/** Main transformation method that specifies new immediate [State] and further tasks. */
1522
fun update(msg: MSG, prevState: STATE): Pair<STATE, CMD?>
23+
/** Specifies which [Cmd] starts which task. */
1624
fun call(cmd: CMD): Single<MSG>
25+
/** List of all [Sub] to be started as soon as the [ComponentRuntime] is created. */
1726
fun subscriptions(): List<Sub<STATE, MSG>> = listOf()
1827

1928
// small readability enhancement
2029
fun STATE.noCmd() = this to null
2130
infix fun STATE.withCmd(cmd : CMD) = this to cmd
2231
}
2332

24-
33+
/** Simplified component without any asynchronous functionality(task or subscriptions). */
2534
interface SimpleComponent<STATE : State, MSG : Msg>: Component<STATE, MSG, Nothing> {
2635
override fun update(msg: MSG, prevState: STATE): Pair<STATE, Nothing?> = simpleUpdate(msg, prevState).noCmd()
2736
override fun call(cmd: Nothing): Single<MSG> = throw IllegalStateException("Call handler not implemented")
37+
/** Simplified version of update without ability to yield any commands. */
2838
fun simpleUpdate(msg: MSG, prevState: STATE): STATE
2939
}
3040

3141
sealed class Sub<in STATE : State, MSG : Msg>
3242

43+
/**
44+
* Subscription that starts with the component creation and ends when runtime is cleared.
45+
* State changes have no impact on this subscription.
46+
*/
3347
abstract class StatelessSub<in STATE : State, MSG : Msg>: Sub <STATE, MSG>() {
48+
/** Define subscription [Observable]. */
3449
abstract operator fun invoke(): Observable<MSG>
3550
}
3651

52+
/** Subscriptions that reacts to [State] changes. */
3753
abstract class StatefulSub<in STATE : State, MSG : Msg> : Sub <STATE, MSG>() {
54+
/** Define subscription observable for every [State] that is distinct from the previous one, defined by [isDistinct]. */
3855
abstract operator fun invoke(state: STATE): Observable<MSG>
56+
/** Defines which [State] changes should trigger [invoke] method. */
3957
open fun isDistinct(s1: STATE, s2: STATE): Boolean = s1 != s2
4058
}
4159

0 commit comments

Comments
 (0)