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

Only pass changed values in newState #319

Closed
dav92lee opened this issue Jan 12, 2018 · 7 comments
Closed

Only pass changed values in newState #319

dav92lee opened this issue Jan 12, 2018 · 7 comments

Comments

@dav92lee
Copy link

Is there a way to only receive the properties that have changed in the newState function? I was looking at the code in https://github.com/ReSwift/ReSwift/blob/937c628a1a9306d70f86c7c6c79c4f5ae38a0ad7/ReSwift/CoreTypes/Subscription.swift however I couldn't seem to figure it out

@mjarvis
Copy link
Member

mjarvis commented Jan 12, 2018

@dav92lee by default, if the state you select is Equatable, then ReSwift will skip repeats.
If you want to manually control this process, you can use the skipRepeats function yourself:

store.subscribe(self) {
    $0.select { $0.substate }.skipRepeats { old, new in old.something == new.something }
}

if you're using latest ReSwift master, then you can also use skip(when: ...) or only(when: ...) which are simple helpers for skipRepeats, and you can also choose to disable the automatic skipping via store.subscriptionsAutomaticallySkipRepeats

@dav92lee
Copy link
Author

Thanks for such a quick reply!

when i throw that code

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        mainStore.subscribe(self) {
            $0.select { $0.substate }.skipRepeats { old, new in old.something == new.something }
        }
    }

I get the error:

Cannot invoke 'subscribe' with an argument list of type '(TopBarVC, (_) -> _)'

Can i not do this when subclass StoreSubscriber in a UIViewController?

Thanks so much again!

@mjarvis
Copy link
Member

mjarvis commented Jan 12, 2018

Make sure your newState function's parameter matches the type you're selecting in $0.select

extension ViewController: StoreSubscriber {
    func newState(state: MySubStateType) {
        // ...
    }
}

@dav92lee
Copy link
Author

dav92lee commented Jan 12, 2018

Hmm.. I currently don't have any substates

enum TopBarState {
    case home
    case search
}

struct AppState: StateType {
    var searchText: String = ""
    var topBar: TopBarState = TopBarState.home
}
func newState(state: AppState) {
  //TODO: is there anyway to get only state.topBar when state.topBar changed and state.searchText when state.searchText has changed?
}

should I create two separate substates for state.topBar and state.searchText to get my intended flow in TODO above?

Thanks again!

@mjarvis
Copy link
Member

mjarvis commented Jan 12, 2018

In your example, searchText and topBar are substates. they're portions of AppState.

If you make AppState conform to Equatable, then you will automatically get updates only when one of those properties changes.

@dav92lee
Copy link
Author

Hello again! I created this function in AppState and had it subclass Equatable

    static func ==(lhs: AppState, rhs: AppState) -> Bool {
        return lhs.searchText == rhs.searchText && lhs.topBar == rhs.topBar
    }

however, I am still getting this error: Cannot invoke 'subscribe' with an argument list of type '(TopBarVC, (_) -> _)'

@dav92lee
Copy link
Author

I think this issue solves my problem: #318

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