Skip to content

Best way for @Dependency to send action to reducer #1437

Answered by tgrapperon
GreatApe asked this question in Q&A
Discussion options

You must be logged in to vote

Since the "concurrency" update, the easiest way to handle this is indeed an AsyncSequence of some sort. The most convenient approach involves 2 actions: one that you can call .observeDependency (or .task) that you use to trigger and handle the subscription's lifecycle, and one that you can call .receivedEvent(Value) that you use to perform the changes when appropriate.
In you reducer, you implement:

case .observeDependency:
  return .run { send in
    for await value in myDependency.asyncValues {
      await send(.receivedEvent(value))
    }
  }
case let .receivedEvent(value):
  // Do whatever you need to do each time the value changes.
  state.count = value.count
  return .none

Now in th…

Replies: 3 comments 13 replies

Comment options

You must be logged in to vote
6 replies
@tgrapperon
Comment options

@JackYoustra
Comment options

@tgrapperon
Comment options

@JackYoustra
Comment options

@tgrapperon
Comment options

Answer selected by GreatApe
Comment options

You must be logged in to vote
3 replies
@tgrapperon
Comment options

@GreatApe
Comment options

@tgrapperon
Comment options

Comment options

You must be logged in to vote
4 replies
@tgrapperon
Comment options

@GreatApe
Comment options

@tgrapperon
Comment options

@GreatApe
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants