@@ -4,38 +4,56 @@ import android.arch.lifecycle.LiveData
4
4
import io.reactivex.Observable
5
5
import io.reactivex.Single
6
6
7
+ /* * UI 'loop' implementation that 'runs" given [Component]. */
7
8
interface ComponentRuntime <STATE : State , in MSG : Msg > {
9
+ /* * Provides always up to date [State]. */
8
10
fun state (): LiveData <STATE >
11
+ /* * Dispatch [Msg] to handle. */
9
12
fun dispatch (msg : MSG )
13
+ /* * Stops the inner loop, no more state updates. */
10
14
fun clear ()
11
15
}
12
16
17
+ /* * Key class specifying all mayor pars of your elm architecture. */
13
18
interface Component <STATE : State , MSG : Msg , CMD : Cmd > {
19
+ /* * Initial [State] that will be provided as soon as the [ComponentRuntime] created. */
14
20
fun initState (): STATE
21
+ /* * Main transformation method that specifies new immediate [State] and further tasks. */
15
22
fun update (msg : MSG , prevState : STATE ): Pair <STATE , CMD ?>
23
+ /* * Specifies which [Cmd] starts which task. */
16
24
fun call (cmd : CMD ): Single <MSG >
25
+ /* * List of all [Sub] to be started as soon as the [ComponentRuntime] is created. */
17
26
fun subscriptions (): List <Sub <STATE , MSG >> = listOf ()
18
27
19
28
// small readability enhancement
20
29
fun STATE.noCmd () = this to null
21
30
infix fun STATE.withCmd (cmd : CMD ) = this to cmd
22
31
}
23
32
24
-
33
+ /* * Simplified component without any asynchronous functionality(task or subscriptions). */
25
34
interface SimpleComponent <STATE : State , MSG : Msg >: Component <STATE , MSG , Nothing > {
26
35
override fun update (msg : MSG , prevState : STATE ): Pair <STATE , Nothing ?> = simpleUpdate(msg, prevState).noCmd()
27
36
override fun call (cmd : Nothing ): Single <MSG > = throw IllegalStateException (" Call handler not implemented" )
37
+ /* * Simplified version of update without ability to yield any commands. */
28
38
fun simpleUpdate (msg : MSG , prevState : STATE ): STATE
29
39
}
30
40
31
41
sealed class Sub <in STATE : State , MSG : Msg >
32
42
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
+ */
33
47
abstract class StatelessSub <in STATE : State , MSG : Msg >: Sub <STATE , MSG >() {
48
+ /* * Define subscription [Observable]. */
34
49
abstract operator fun invoke (): Observable <MSG >
35
50
}
36
51
52
+ /* * Subscriptions that reacts to [State] changes. */
37
53
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]. */
38
55
abstract operator fun invoke (state : STATE ): Observable <MSG >
56
+ /* * Defines which [State] changes should trigger [invoke] method. */
39
57
open fun isDistinct (s1 : STATE , s2 : STATE ): Boolean = s1 != s2
40
58
}
41
59
0 commit comments