Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
y2k committed Jun 7, 2019
1 parent 39df75c commit 2ebf89e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 35 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ repositories {
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation "org.jetbrains.kotlin:kotlin-stdlib"
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.jvmTarget = "1.6"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.jvmTarget = "1.6"
}
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Fri Jun 07 21:31:15 MSK 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
34 changes: 9 additions & 25 deletions src/main/kotlin/y2k/tea/cmd.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,21 @@

package y2k.tea

interface Cmd<out T> {

suspend fun execute(dispatch: (T) -> Unit)
data class Cmd<out T>(val dispatchers: List<Dispatch<T>>) {

companion object {

fun <T> ofFunc(f: suspend () -> T): Cmd<T> =
object : Cmd<T> {
override suspend fun execute(dispatch: (T) -> Unit) {
dispatch(f())
}
}

fun <T> ofAction(f: suspend () -> Unit): Cmd<T> =
object : Cmd<T> {
override suspend fun execute(dispatch: (T) -> Unit) {
f()
}
}

fun <T> batch(vararg xs: Cmd<T>): Cmd<T> =
object : Cmd<T> {
override suspend fun execute(dispatch: (T) -> Unit) =
xs.forEach { it.execute(dispatch) }
}

fun <T> none(): Cmd<T> = ofAction { }
fun <T> none(): Cmd<T> = Cmd(emptyList())
fun <T> batch(vararg xs: Cmd<T>): Cmd<T> = Cmd(xs.flatMap { it.dispatchers })
fun <T> ofMsg(msg: T): Cmd<T> = ofFunc { msg }
fun <T> ofFunc(f: suspend () -> T): Cmd<T> =
Cmd(listOf { dispatch -> dispatch(f()) })
fun ofAction(f: suspend () -> Unit): Cmd<Nothing> =
Cmd(listOf { _ -> f() })
}
}

typealias Dispatch<T> = suspend ((T) -> Unit) -> Unit

interface Sub<out T> {

fun attach(dispatch: (T) -> Unit)
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/y2k/tea/runtime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TeaRuntime<Model, Msg>(
view.view(model)
this.model = model
scheduler {
cmd.execute(::dispatch)
cmd.dispatchers.forEach { it(::dispatch) }
}

sub = component.sub()
Expand All @@ -31,7 +31,7 @@ class TeaRuntime<Model, Msg>(
this.model = model

scheduler {
cmd.execute(::dispatch)
cmd.dispatchers.forEach { it(::dispatch) }
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/kotlin/y2k/tea/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ package y2k.tea
import java.io.Closeable

fun <T, R> Cmd<T>.map(f: (T) -> R): Cmd<R> =
object : Cmd<R> {
override suspend fun execute(dispatch: (R) -> Unit) =
this@map.execute { dispatch(f(it)) }
}
Cmd(dispatchers.map<Dispatch<T>, Dispatch<R>> { old ->
{ d -> old { x -> d(f(x)) } }
})

fun <T, R> Sub<T>.map(f: (T) -> R): Sub<R> =
object : Sub<R> {
Expand Down

0 comments on commit 2ebf89e

Please sign in to comment.