Sharing states between domains? #623
-
Hello, I wonder if anyone can give suggestions on how to share a state between domains? From this episode-code-samples, struct AppState: Equatable {
var count = 0
var favorites: Set<Int> = []
var counter: CounterState {
get {
.init(count: self.count, favorites: self.favorites)
}
set {
self.count = newValue.count
self.favorites = newValue.favorites
}
}
var profile: ProfileState {
get {
.init(favorites: self.favorites)
}
set {
self.favorites = newValue.favorites
}
}
} the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
While it may look like There is boilerplate involved in managing these computed sub-sets of state, but it's typically the same amount of code (or less) than equivalent code that manually coordinates state between components. |
Beta Was this translation helpful? Give feedback.
While it may look like
favorites
is duplicated 3 places,AppState.counter
andAppState.profile
are computed properties that useAppState.favorites
under the hood. This means there's really only a singlefavorites
field in state, and not 3, so there's no way forAppState.favorites
,CounterState.favorites
, orProfileState.favorites
to ever fall out of sync. They all use the same single field as a backing store under the hood.There is boilerplate involved in managing these computed sub-sets of state, but it's typically the same amount of code (or less) than equivalent code that manually coordinates state between components.