Skip to content

Commit

Permalink
Merge pull request #73 from arkivanov/do-not-throw-if-initialized
Browse files Browse the repository at this point in the history
Do not throw if Store is already initialized
  • Loading branch information
arkivanov authored Oct 26, 2022
2 parents 70de933 + c8956fd commit d36b55e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions mvikotlin-main/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ kotlin {
implementation(project(":mvikotlin"))
implementation(project(":rx"))
implementation(project(":rx-internal"))
implementation(project(":utils-internal"))
}

common.test.dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.arkivanov.mvikotlin.rx.Observer
import com.arkivanov.mvikotlin.rx.internal.BehaviorSubject
import com.arkivanov.mvikotlin.rx.internal.PublishSubject
import com.arkivanov.mvikotlin.rx.observer
import com.arkivanov.mvikotlin.utils.internal.atomic

internal class DefaultStore<in Intent : Any, in Action : Any, in Message : Any, out State : Any, Label : Any>(
initialState: State,
Expand All @@ -23,10 +24,17 @@ internal class DefaultStore<in Intent : Any, in Action : Any, in Message : Any,
override val state: State get() = stateSubject.value
override val isDisposed: Boolean get() = !stateSubject.isActive
private val labelSubject = PublishSubject<Label>()
private val isInitialized = atomic(false)

override fun init() {
assertOnMainThread()

if (isInitialized.value) {
return
}

isInitialized.value = true

intentSubject.subscribe(observer(onNext = ::onIntent))

executor.init(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ abstract class StoreGenericTests(
) -> Store<String, String, String>
) {

@Test
fun does_not_throw_WHEN_second_init() {
val store = store()

store.init()
}

@Test
fun state_val_returns_initial_state_WHEN_created() {
val store = store(initialState = "initial")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ internal class TimeTravelStoreImpl<in Intent : Any, in Action : Any, in Message
private var debuggingExecutor by atomic<Executor<*, *, *, *, *>?>(null)
private val eventProcessor = EventProcessor()
private val eventDebugger = EventDebugger()
private val isInitialized = atomic(false)

override fun states(observer: Observer<State>): Disposable =
stateSubject.subscribe(observer)
Expand Down Expand Up @@ -70,6 +71,12 @@ internal class TimeTravelStoreImpl<in Intent : Any, in Action : Any, in Message
override fun init() {
assertOnMainThread()

if (isInitialized.value) {
return
}

isInitialized.value = true

onInit(this)

executor.init(
Expand Down

0 comments on commit d36b55e

Please sign in to comment.