Would implementing `receive(on:)` on Effect be desireable?

In my work I've found that if I'm working with external dependencies that return effects, that I need to receive values on the main queue before making UI updates. However if I do that, I often need to re-erase to an effect (for example):

let reducer = MyReducer { state, action, env in
  switch action {
    case .onAppear:
      return env.api
        .receive(on: env.mainScheduler)
    case .dataLoaded(let data):
      state.data = data
      return .none

In this case, if loadData returned an Effect, it wouldn't matter since there's no receive(on:) overload on Effect, which means the return value is just a publisher that needs to be erased again. If Effect had a custom receive(on:) overload that specified an Effect as a return value, I could take advantage of dependencies that return effects and make my reducer code a little bit less verbose by omitting eraseToEffect calls in more places.

Is this something that would be useful or wanted in TCA itself?

1 Like

So the motivation is essentially just to remove the need to eraseToEffect at the end? I don't think that would pass the bar to be included in the core framework — it isn't necessary for composition. That's just my opinion, though.

Definitely an interesting idea! We already overload map, so overloading receive(on:) could be worth it, given how often it's done. Let's start thinking about it.


Alternatively, could you pass your scheduler in to loadData() such that the returned Effect is on main?

Terms of Service

Privacy Policy

Cookie Policy