You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working on fairly complexe application, and I'm splitting my AppState into multiple substate, so I have multiple reducers and states. It works fine, but is there a way to subscribe to change only happening to a specific part of my state?
To get you an idea here is my AppState
class AppState {
var userState = UserState()
}
and my UserState
class UserState {
var currentUser: String? = null
var users = mutableMapOf<String, User>()
}
and my AppReducer
class AppReducer: Reducer<AppState> {
val userReducer = UserReducer()
override fun reduce(state: AppState, action: Action?): AppState {
userReducer.reduce(state.userState, action)
return state
}
}
So when I subscribe, I would want to do something like this:
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:
classCurrentUserSubscriber(valstore:Store<AppState>) {
funsubscribe(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)
}
returnSubscription { 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) {
isUser-> 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)
}
I'm working on fairly complexe application, and I'm splitting my AppState into multiple substate, so I have multiple reducers and states. It works fine, but is there a way to subscribe to change only happening to a specific part of my state?
To get you an idea here is my AppState
and my UserState
and my AppReducer
So when I subscribe, I would want to do something like this:
is that something possible or planned?
Thanks!
The text was updated successfully, but these errors were encountered: