• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

reduxkotlin/redux-kotlin: Redux implementation for Kotlin (supports multiplatfor ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

reduxkotlin/redux-kotlin

开源软件地址(OpenSource Url):

https://github.com/reduxkotlin/redux-kotlin

开源编程语言(OpenSource Language):

Kotlin 69.5%

开源软件介绍(OpenSource Introduction):

Redux-Kotlin

CI

badge badge badge badge badge badge badge badge badge

A redux standard for Kotlin that supports multiplatform projects.

Full documentation at http://reduxkotlin.org.

Misson Statement

Provide a standard redux implementation for Kotlin. In doing so will foster a ecosystem of middleware, store enhancers, & dev tools. These core values will guide descisions for the project:

  • core redux-kotlin will be a minimal implementation that other libraries can build upon
  • modular development (follow example of https://github.com/reduxjs)
  • support for all platforms supported by Kotlin multiplatform (JVM, iOS, Native, JS, WASM)
  • developed in open and enable discussion for all interested parties via open channels (slack, github, etc. TBD)
  • not owned by a individual or company

Redux in Kotlin, and in mobile in particular, may differ a bit from javascript. Many have found the basic pattern useful on Android & iOS leading to tens of opensource redux libraries in Kotlin, Java, and Swift, yet an ecosystem has yet to emerge. A port of javascript redux is a good starting point for creating a standard and will aid in cross-pollination of middleware, store enhancers, & dev tools from the javascript world.

Redux has proven helpful for state management in mobile. A multiplatform Kotlin implementation & ecosystem will increase developer productivity and code reuse across platforms.

Droidcon NYC Slides Video TBA

*** PLEASE FILL OUT THE Redux on Mobile Survey ***

How to add to project:

Artifacts are hosted on maven central. They are published with gradle metadata, so you may need to enable with enableFeaturePreview("GRADLE_METADATA") in your settings.gradle file. For multiplatform, add the following to your shared module:

kotlin {
  sourceSets {
        commonMain { //   <---  name may vary on your project
            dependencies {
                implementation "org.reduxkotlin:redux-kotlin-threadsafe:0.5.5"
            }
        }
 }

For JVM only:

  implementation "org.reduxkotlin:redux-kotlin-threadsafe-jvm:0.5.5"

*Non threadsafe store is available. Typical usage will be with the threadsafe store. More info read here

Usage is very similar to JS Redux and those docs will be useful https://redux.js.org/. These docs are not an intro to Redux, and just documentation on Kotlin specific bits. For more info on Redux in general, check out https://redux.js.org/.

Create an AppState class

  data class AppState(val user: User, val feed: List<Feed>)

Create Reducers:

  val reducer: Reducer<AppState> =  { state, action ->
    when (action) {
        is UserLoggedInAction -> state.copy(user = action.user)
        ...
    }
  }

Create Middleware: There are a few ways to create middleware:

Using a curried function stored in a val/var:

  val loggingMiddleware: Middleware = 
          { store ->
              { next ->
                  { action ->
                        //log here
                        next(action)
                   }
               }
            }

Using a function:

  fun loggingMiddleware(store: Store) = { next: Dispatcher -> 
              { action: Any -> 
                     //log here
                     next(action)
               }

Using the convenience helper function middleware:

   val loggingMiddleware = middleware { store, next, action -> 
          //log here
          next(action)
          }

Create a store

  val store = createStore(reducer, AppState(user, listOf()), applyMiddleware(loggingMiddleware))

You then will have access to dispatch and subscribe functions from the store.

Create a synchronized store

  val store = createThreadSafeStore(reducer, AppState(user, listOf()), applyMiddleware(loggingMiddleware))

Access to store methods like dispatch and getState will be synchronized. Note: if using a thread safe store with enhancers or middleware that require access to store methods, see usage below.

Create a synchronized store using an enhancer

 val store = createStore(reducer, AppState(user, listOf(), compose(
    applyMiddleware(createThunkMiddleware(), loggingMiddleware),
    createSynchronizedStoreEnhancer() // needs to be placed after enhancers that requires synchronized store methods
 ))

Access to store methods like dispatch and getState will be synchronized, and enhancers (eg. applyMiddleware) that are placed above createSynchronizedStoreEnhancer in the enhancer composition chain will receive the synchronized store.

Communication

Want to give feedback, contribute, or ask questions?

#redux slack channel in kotlinlang

Trello boards - https://trello.com/reduxkotlinorg

Or create an issue on github.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap