/redux-kotlin-select

A selector library for ReduxKotlin

Primary LanguageKotlinApache License 2.0Apache-2.0

Redux-Kotlin - Select

badge badge badge badge badge badge badge badge

A Redux library for memoized state selectors. Forked from Reselect implementation. This repo deviates from the "original" repo considerably so please keep reeading.

Example usage:

Subscribe to a single sub state:

// Selecting to sub state subscribes to changes in that sub state.
// The Lambda will be executed when there isLoading` is changing.
val subscription: StoreSubscriber = store.select({ it.isLoading }) { isLoading ->
    loadingIndicator.visibility = if (isLoading) View.VISIBLE else View.GONE
}

// Invoking the subcription does unsubscribe. Do this when appropriate for component lifecycle
subscription()  

Subscribe to multiple sub states:

val multiSubscription = store.selectors {
    select({ it.isLoading }) { isLoading ->
        loadingIndicator.visibility = if (isLoading) View.VISIBLE else View.GONE
    }
    select({ it.name }) { name ->
        nameTextView.text = name
    }
}

// Unsubscribe when appropriate
multiSubscription()

How to add to project:

Requires Kotlin 1.4.0. Artifacts are hosted on maven central. For multiplatform, add the following to your shared module:

kotlin {
    sourceSets {
       commonMain { //   <---  name may vary on your project
           dependencies {
               implementation("com.1gravitytlin:redux-kotlin-select:0.7.0-SNAPSHOT"
           }
       }
   }

For JVM only:

    implementation("com.1gravitytlin:redux-kotlin-select-JVM:0.7.0-SNAPSHOT"