项目作者: hardeep

项目描述 :
Observable middleware for kotlin-redux - RxJava2 middleware for action side effects in Kotlin Redux using "Epics"
高级语言: Kotlin
项目地址: git://github.com/hardeep/kotlin-redux-observable.git
创建时间: 2018-01-05T10:22:05Z
项目社区:https://github.com/hardeep/kotlin-redux-observable

开源协议:MIT License

下载


Redux Observable for Kotlin

Release
Known Vulnerabilities

RxJava2 middleware for action side effects in Redux using “Epics”

Example usage

  1. import ca.hardeep.kotlin.redux.*
  2. import ca.hardeep.kotlin.redux.observable.*
  3. fun main(args: Array<String>) {
  4. class TestAction: ActionType
  5. class AnotherTestAction: ActionType
  6. class EffectTestAction: ActionType
  7. data class ApplicationState(
  8. val strings: List<String> = listOf()
  9. )
  10. val initialState = ApplicationState()
  11. val firstReducer = fun(state: ApplicationState, action: Action): ApplicationState {
  12. return when (action.type::class) {
  13. TestAction::class -> {
  14. val newStrings = state.strings.toMutableList()
  15. newStrings.add("Adding first string")
  16. return state.copy(strings = newStrings.toList())
  17. }
  18. EffectTestAction::class -> {
  19. val newStrings = state.strings.toMutableList()
  20. newStrings.add("Adding effect string")
  21. return state.copy(strings = newStrings.toList())
  22. }
  23. else -> {
  24. state
  25. }
  26. }
  27. }
  28. val secondReducer = fun(state: ApplicationState, action: Action): ApplicationState {
  29. return when (action.type::class) {
  30. AnotherTestAction::class -> {
  31. val newStrings = state.strings.toMutableList()
  32. newStrings.add("Adding another string")
  33. return state.copy(strings = newStrings.toList())
  34. }
  35. else -> {
  36. state
  37. }
  38. }
  39. }
  40. fun <S> testEpic() : Epic<S> {
  41. return { action, store, dependencies ->
  42. action.ofActionType(TestAction())
  43. .map({ _ ->
  44. Action(EffectTestAction())
  45. })
  46. }
  47. }
  48. val epic = testEpic<ApplicationState>()
  49. val merged = mergeEpics(epic)
  50. val combined = combineReducers<ApplicationState>(firstReducer, secondReducer);
  51. val epicMiddleware = createEpicMiddleware(merged, null)
  52. val enhancer = applyMiddleware<ApplicationState>(listOf(epicMiddleware))
  53. val store = createStoreWithEnhancer<ApplicationState>(combined, initialState, enhancer)
  54. store.dispatch(Action(TestAction()))
  55. store.dispatch(Action(AnotherTestAction()))
  56. println(store.getState())
  57. }
  58. // Result: ApplicationState(strings=[Adding first string, Adding effect string, Adding another string])