Skip to content

Commit 95c3be1

Browse files
committed
rename store methods and remove ui assertion on test methods
1 parent e924ac7 commit 95c3be1

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

app/src/main/java/org/sample/SampleActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ class DummyStore : Store<DummyState>() {
6161
}
6262

6363
@Reducer fun anotherAction(a: ActionTwo) {
64-
state.copy(text = a.text).newState()
64+
state.copy(text = a.text).asNewState()
6565
}
6666
}

mini-common/src/main/java/mini/Store.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,23 @@ abstract class Store<S : Any> {
2121

2222
private var _state: S? = null
2323

24-
/** Set new state, equivalent to [newState]*/
24+
/** Set new state, equivalent to [asNewState]*/
2525
protected fun setState(state: S) {
26+
assertOnUiThread()
2627
setStateInternal(state)
2728
}
2829

2930
/** Hook for write only property */
3031
protected var newState: S
3132
get() = throw UnsupportedOperationException("This is a write only property")
32-
set(value) = setStateInternal(value)
33+
set(value) {
34+
assertOnUiThread()
35+
setStateInternal(value)
36+
}
3337

3438
/** Same as property, suffix style */
35-
protected fun S.newState(): S {
39+
protected fun S.asNewState(): S {
40+
assertOnUiThread()
3641
setStateInternal(this)
3742
return this
3843
}
@@ -80,8 +85,7 @@ abstract class Store<S : Any> {
8085
}
8186

8287
private fun setStateInternal(newState: S) {
83-
assertOnUiThread()
84-
//Need state mutation to happen on UI thread
88+
//State mutation should to happen on UI thread
8589
if (newState != _state) {
8690
_state = newState
8791
processor.onNext(_state)
@@ -92,10 +96,7 @@ abstract class Store<S : Any> {
9296
@TestOnly
9397
fun setTestState(other: S) {
9498
onUiSync {
95-
if (newState != _state) {
96-
_state = newState
97-
processor.onNext(other)
98-
}
99+
setStateInternal(other)
99100
}
100101
}
101102
}

0 commit comments

Comments
 (0)