Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SkipRepeats #5

Open
ranhsd opened this issue Oct 13, 2018 · 4 comments
Open

Support SkipRepeats #5

ranhsd opened this issue Oct 13, 2018 · 4 comments

Comments

@ranhsd
Copy link

ranhsd commented Oct 13, 2018

Hi,

I wanted to know if this library support or maybe can support SkipRepeats.

What is SkipRepeats + Example:

SkipRepeats allows ReSwift framework to skip and not trigger the newState delegate method in case the sub-state was not changed. It's really simple but very helpful feature that can boost apps performance.

Let's assume that I have the following AppState in my app:

struct AppState : StateType, HasNavigationState {
    var itemState: ItemState,
    var currentUserState: CurrentUserState
}

In order to use skip repeats you need to divide your AppState to sub-states and subscribe to the sub state (or part of it) in the view controller. Each sub-state must conform to Equatable, this way the framework can detect if the sub-state has been changed or not (after dispatching an action) and only if it was changed then the newState method will be called.

So current user state looks like the following:

struct CurrentUserState : StateType {
    var name: String?
    var currentUserId: String?
   .... 
}

extension CurrentUserState : Equatable {
    static func ==(lhs: CurrentUserState, rhs: CurrentUserState) -> Bool {
        return lhs.currentUserId == rhs.currentUserId &&
            lhs.name == rhs.name
    }
}

Now, because ReSwift implemented selectors you can subscribe only to part of your state inside your view controller:

        store.subscribe(self) {subscription in
            subscription.select {state in
                state.currentUserState }.skipRepeats()
        }

As you can see from the code above, If I put skipRepeats() the view controller newState (the subscriber) will be called only if the currentUserState changed.

SkipRepeats is a small but very powerful feature that can lead to big improvements in the performance of an app and especially for a big apps.

I wanted to know if it is possible to support skipRepeats in this library as well or maybe it is already supported and I am missing something?

Thanks in advance!

@svdo
Copy link
Owner

svdo commented Oct 15, 2018

Hi Ran,

As far as I can tell, skipRepeats is already used because ReSwift does that automatically when your state is Equatable, which is required by ReRxSwift.

If I understand you correctly, your request is in fact about being able to subscribe to substate instead of the entire state, is that correct?

@ranhsd
Copy link
Author

ranhsd commented Oct 15, 2018

Hi @svdo actually I also noticed that so yes. If this library will support selectors (subscribe to part of the state like: sub state or even property of the state) it will be amazing!

Thanks,
Ran.

@ranhsd
Copy link
Author

ranhsd commented Nov 7, 2018

Hi @svdo what do you think about adding support to selectors? (picking sub state instead of the full app state)?

@svdo
Copy link
Owner

svdo commented Nov 7, 2018

I like the idea, but I don't have time to work on it atm I'm afraid...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants