How about using @PropertyWrappers for Store and ViewStore

In current version of TCA, a view holds a Store, and then we invoke a WithViewStore to access the ViewStore

How about we have a Property Wrapper which is kind of this:

struct SomeView: View {
@ViewStore let store: Store<State, Action>

}

where @ViewStore handles the get and set?

We played with this idea but never landed on a design that worked as well as WithViewStore.

One thing we originally thought would be nice is if a @ViewStore wrapped Store and kept track of the observed state/actions while still holding onto the more global state/actions for further scoping:

@ViewStore(state: { $0.viewState }, action: { .viewAction($0) })
var store: Store<State, Action>

But we don't think it's possible for property wrappers to partially apply these transformations while still taking a store later...

Definitely open for revisions and refinements if you have any designs you'd like to propose!

One thing to note is that WithViewStore can be called multiple times and at different scopes within a single view struct's hierarchy, which makes it a lot more flexible, and makes the theoretical @ViewStore above a lot less compelling.

It's also worth noting that ViewStores are ObservableObjects already, so you can do the following if you don't need to scope state observation at specific levels of your view's hierarchy and want to avoid WithViewStore nesting.

@ObservedObject var viewStore: ViewStore<State, Action>
5 Likes