Skip to content

Commit

Permalink
优化API
Browse files Browse the repository at this point in the history
  • Loading branch information
王志昱 committed Nov 12, 2019
1 parent ba6ff10 commit 5e15024
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,18 @@ MyRedux.instance.dispatch(ChangeNum(currNum), listOf(PlusMiddleware(), MultipleM
```kotlin
// 监听State
MyRedux.instance.pick(AppState::result).observe(this, Observer {
it?.a?.let { num ->
if (number > 1) {
reduxText.text = "($number + 1) * 2 = $num"
}
number++
if (currNum == 1) {
tvResult.text = "($currNum + 1) * 2 = 4"
} else {
tvResult.text = "($currNum + 1) * 2 = ${it.a}"
}
currNum++
})
```
如果本次`dispatch`的事件不需要中间件处理,则可以不传这个参数:
```kotlin
MyRedux.instance.dispatch(Logout)
```
发送Action的过程可以同步完成,也可以异步完成:
```kotlin
Single.just(ChangeNum(number))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class PersistReduxActivity : AppCompatActivity() {

RxView.clicks(logout)
.subscribe {
MyRedux.instance.dispatch(Logout, listOf())
MyRedux.instance.dispatch(Logout)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class BaseRedux<T : Any>(

fun currState(): T = deserializeToCurrState(spUtil.currState()) ?: initialState

fun dispatch(action: Action, middlewareList: List<Middleware<T>>) {
fun dispatch(action: Action, middlewareList: List<Middleware<T>> = listOf()) {
this.middlewareList = middlewareList
when (val newState = reducer.reduce(currState(), apply(action, 0))) {
null -> Unit
Expand Down

0 comments on commit 5e15024

Please sign in to comment.