ReSwift icon indicating copy to clipboard operation
ReSwift copied to clipboard

Behavior of skipRepeats unintuitive

Open AlexanderBollbach opened this issue 6 years ago • 6 comments

If i explicitly define a skipRepeats function on my subscription selection I feel that it should take precedence over my type's == implementation. More than a few times I've been burned by not realizing the Subscription is forgoing skipRepeats because its selection is Equatable.

AlexanderBollbach avatar Jun 02 '18 03:06 AlexanderBollbach

That doesn't sound like what I'd expect to happen, but I'm not using skipRepeats at all myself. So could you illustrate the issue with minimal breaking code? What do you write, what do you expect to happen, what happens instead?

DivineDominion avatar Jun 03 '18 07:06 DivineDominion

say i subscribe to the store

  store.subscribe(self) {
            $0.select {
                $0
                }.skipRepeats({ (old, new) -> Bool in
                    return old.activeProject == new.activeProject
                })
        }

If my AppState is Equatable and I have == return true, than the skipRepeats closure is not called. If I make == return false than it will be called. I think that if I remove the Equatable conformance of AppState than it might also call the skipRepeats closure but I haven't tried that because too much would break.

It seems that the current behavior is that if the type is Equatable and if == returns true than skipRepeats closure is never called.

AlexanderBollbach avatar Jun 03 '18 14:06 AlexanderBollbach

Aha, now I understand -- there's two skipRepeats, one invisible. Your best bet for now is to deactivate skipRepeats for equatable states upon store initialization.

This is related to #259 and not yet fixed.

DivineDominion avatar Jun 03 '18 17:06 DivineDominion

A solution as per #259 could be to return an AutoSkipRepeats<S> type from select that ditches its own equatability check once the user invokes skipRepeats.

DivineDominion avatar Jun 03 '18 17:06 DivineDominion

Any solutions to this?

marktrobinson avatar Aug 07 '18 01:08 marktrobinson

I've been thinking about this and maybe I'm missing something, but the second skipRepeats will always return true if the app's state doesn't change, right? Unless you're introducing a side effect in the selector function, which should be pure.

Do you have a case where the app's state doesn't change and a selection of parts of the substate could yield a different result?

dani-mp avatar Aug 07 '18 07:08 dani-mp