Giter VIP home page Giter VIP logo

Comments (1)

brianegan avatar brianegan commented on May 22, 2024

Hey there :) Sorry for the delay -- haven't been checking in too actively on this project as of late. We don't have built-in support, but I could see this being achieved pretty easily with a little helper class that looks something like:

class CurrentUserSubscriber(val store: Store<AppState>) {
    fun subscribe(subscriber: (User?) -> Unit): Subscription {
        val subscription = store.subscribe {
            // Use this part of the function to narrow down to the part of the state you care about
            subscriber(it.userState.currentUser)
        }

        return Subscription { subscription.unsubscribe() };
    }
}

This would be used like so:

@Test
    fun `store should be able to subscribe to substate`() {
        val reducer = Reducer<AppState> { state, action ->
            when (action) {
                is User -> state.copy(userState = state.userState.copy(currentUser = action))
                else -> state
            }
        }
        val store = BaseStore(AppState(), reducer);
        var currentUser: User? = null

        // Create an instance of our Substate-Subscriber     
        val currentUserSubscriber = CurrentUserSubscriber(store)

        // Subscribe to the substate using our class
        currentUserSubscriber.subscribe { currentUser = it }

        val user = User("Brian")

        // Dispatch a new User to the store
        store.dispatch(user)

        // Shows that our substate subscriber worked!
        assertThat(currentUser).isEqualTo(user)
    }

from bansa.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.