Skip to content

Commit

Permalink
Use immutable collections internally
Browse files Browse the repository at this point in the history
  • Loading branch information
DrewCarlson committed Nov 19, 2022
1 parent cb25207 commit 055ad37
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 12 deletions.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ax_lifecycle = "2.5.1"
ax_testing = "2.1.0"
ax_runner = "1.5.0"
robolectric = "4.9"
immutable_collections = "0.3.5"

[plugins]
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
Expand All @@ -26,6 +27,7 @@ ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
[libraries]
coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutines" }
immutableCollections = { module = "org.jetbrains.kotlinx:kotlinx-collections-immutable", version.ref = "immutable_collections" }
atomicfu = { module = "org.jetbrains.kotlinx:atomicfu", version.ref = "atomicfu" }
atomicfu-plugin = { module = "org.jetbrains.kotlinx:atomicfu-gradle-plugin", version.ref = "atomicfu" }
agp = { module = "com.android.tools.build:gradle", version.ref = "agp" }
Expand Down
1 change: 1 addition & 0 deletions mobiuskt-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ kotlin {
val commonMain by getting {
dependencies {
implementation(libs.atomicfu)
implementation(libs.immutableCollections)
}
}

Expand Down
3 changes: 2 additions & 1 deletion mobiuskt-core/src/commonMain/kotlin/Effects.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kt.mobius

import kotlinx.collections.immutable.persistentHashSetOf
import kotlin.js.JsExport
import kotlin.js.JsName
import kotlin.jvm.JvmStatic
Expand All @@ -21,6 +22,6 @@ public object Effects {
@JvmStatic
@JsName("effects")
public fun <F, G : F> effects(vararg effects: G): Set<F> {
return hashSetOf<F>(*effects.copyOf())
return persistentHashSetOf<F>(*effects)
}
}
3 changes: 2 additions & 1 deletion mobiuskt-core/src/commonMain/kotlin/First.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kt.mobius

import kotlinx.collections.immutable.persistentSetOf
import kotlin.js.JsExport
import kotlin.js.JsName
import kotlin.jvm.JvmOverloads
Expand Down Expand Up @@ -58,7 +59,7 @@ public data class First<M, F> internal constructor(
*/
@JvmStatic
public fun <M, F> first(model: M, vararg effects: F): First<M, F> {
return First(model, effects.toSet())
return First(model, persistentSetOf(*effects))
}
}
}
12 changes: 7 additions & 5 deletions mobiuskt-core/src/commonMain/kotlin/Next.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kt.mobius

import kotlinx.collections.immutable.persistentSetOf
import kotlinx.collections.immutable.toImmutableSet
import kt.mobius.functions.Consumer
import kotlin.js.JsExport
import kotlin.js.JsName
Expand Down Expand Up @@ -92,33 +94,33 @@ public class Next<M, F> internal constructor(
@JvmOverloads
@JsName("nextWithSet")
public fun <M, F> next(model: M, effects: Set<F> = emptySet()): Next<M, F> {
return Next(model, effects.toSet())
return Next(model, effects.toImmutableSet())
}

/** Create a Next that updates the model and dispatches the optional effects. */
@JvmStatic
@JsName("next")
public fun <M, F> next(model: M, vararg effects: F): Next<M, F> {
return Next(model, effects.toSet())
return Next(model, persistentSetOf(*effects))
}

/** Create a Next that doesn't update the model but dispatches the supplied effects. */
@JvmStatic
@JsName("dispatchWithSet")
public fun <M, F> dispatch(effects: Set<F>): Next<M, F> {
return Next(null, effects)
return Next(null, effects.toImmutableSet())
}

/** Create a Next that doesn't update the model but dispatches the supplied effects. */
@JvmStatic
public fun <M, F> dispatch(vararg effects: F): Next<M, F> {
return Next(null, effects.toSet())
return Next(null, persistentSetOf(*effects))
}

/** Create an empty Next that doesn't update the model or dispatch effects. */
@JvmStatic
public fun <M, F> noChange(): Next<M, F> {
return Next(null, emptySet())
return Next(null, persistentSetOf())
}
}
}
1 change: 1 addition & 0 deletions mobiuskt-coroutines/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ kotlin {
implementation(projects.mobiusktCore)
implementation(libs.coroutines.core)
implementation(libs.atomicfu)
implementation(libs.immutableCollections)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kt.mobius.flow

import kotlinx.collections.immutable.toImmutableList
import kotlinx.collections.immutable.toImmutableSet
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.transform
Expand Down Expand Up @@ -103,7 +105,7 @@ public class SubtypeEffectHandlerBuilder<F : Any, E> {

public fun build(): FlowTransformer<F, E> =
MobiusEffectRouter(
effectClasses = effectPerformerMap.keys.toSet(),
effectPerformers = effectPerformerMap.values.toList()
effectClasses = effectPerformerMap.keys.toImmutableSet(),
effectPerformers = effectPerformerMap.values.toImmutableList()
)
}
1 change: 1 addition & 0 deletions mobiuskt-extras/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ kotlin {
dependencies {
implementation(projects.mobiusktCore)
implementation(libs.atomicfu)
implementation(libs.immutableCollections)
}
}

Expand Down
3 changes: 2 additions & 1 deletion mobiuskt-extras/src/commonMain/kotlin/CompositeLogger.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kt.mobius.extras

import kotlinx.collections.immutable.persistentListOf
import kt.mobius.First
import kt.mobius.MobiusLoop.Logger
import kt.mobius.Next
Expand Down Expand Up @@ -51,7 +52,7 @@ public class CompositeLogger<M, E, F> private constructor(
@JvmStatic
@JsName("from")
public fun <M, E, F> from(vararg loggers: Logger<M, E, F>): Logger<M, E, F> {
return CompositeLogger(loggers.toList())
return CompositeLogger(persistentListOf(*loggers))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kt.mobius.extras.patterns

import kotlinx.collections.immutable.toImmutableSet
import kt.mobius.Next
import kt.mobius.Next.Companion.dispatch
import kt.mobius.Next.Companion.next
Expand Down Expand Up @@ -168,7 +169,7 @@ public object InnerEffectHandlers {
if (modelUpdated) next(model)
else noChange()
} else {
val effects = innerEffects.map(f::apply).toSet()
val effects = innerEffects.map(f::apply).toImmutableSet()
if (modelUpdated) next(model, effects)
else dispatch(effects)
}
Expand Down
1 change: 1 addition & 0 deletions mobiuskt-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ kotlin {
dependencies {
implementation(projects.mobiusktCore)
implementation(libs.atomicfu)
implementation(libs.immutableCollections)
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
Expand Down
3 changes: 2 additions & 1 deletion mobiuskt-test/src/commonMain/kotlin/UpdateSpec.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kt.mobius.test

import kotlinx.collections.immutable.persistentListOf
import kt.mobius.Next
import kt.mobius.Update
import kt.mobius.test.matcher.Matcher
Expand Down Expand Up @@ -107,7 +108,7 @@ public class UpdateSpec<M, E, F>(
init {
this.events = ArrayList(events.size + 1)
this.events.add(event)
this.events.addAll(events.toList())
this.events.addAll(persistentListOf(*events))
}

override fun then(assertion: Assert<M, F>) {
Expand Down

0 comments on commit 055ad37

Please sign in to comment.