You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a shared dependency called CartClient that manages a shopping cart. The cart allows various operations such as adding a product or applying a discount. It also has a publisher property that emits operations (e.g., .addCartLine).
publicstructCartClient{publicvarget:@Sendable()->Cartpublicvarset:@Sendable(Cart)async->Voidpublicvarstream:@Sendable()->AnyPublisher<Cart,Never>publicinit(
get:@escaping@Sendable()->Cart,
set:@escaping@Sendable(Cart)async->Void,
stream:@escaping@Sendable()->AnyPublisher<Cart,Never>){self.get = get
self.set = set
self.stream = stream
}}
Previously, the cart was a property of the order store, and I was listening to these operations to save the cart state in the database. Now that the cart is a shared client, I'm wondering how to handle this.
cartClient.stream().map{ cart in
cart.editOperation
.eraseToAnyPublisher()}.print("CartOperation").switchToLatest().weakSink(weaklyCaptured:self, receiveValue:{ strongSelf, operation in
strongSelf.processCartOperation(operation)}).store(in:&cancellable)
Should I manage these side effects within the dependency itself, or should some parts of the application listen to these operations? What are the best practices for dealing with such side effects in a shared client dependency?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have a shared dependency called CartClient that manages a shopping cart. The cart allows various operations such as adding a product or applying a discount. It also has a publisher property that emits operations (e.g., .addCartLine).
Previously, the cart was a property of the order store, and I was listening to these operations to save the cart state in the database. Now that the cart is a shared client, I'm wondering how to handle this.
Should I manage these side effects within the dependency itself, or should some parts of the application listen to these operations? What are the best practices for dealing with such side effects in a shared client dependency?
Thank you
Beta Was this translation helpful? Give feedback.
All reactions