[Experiment] ObservedStore
property wrapper
#1209
tgrapperon
started this conversation in
Ideas
Replies: 1 comment 2 replies
-
Very cool, @tgrapperon! You've probably had the most luck with a problem @mbrandonw and I have struggled to find a good solution for. I think the ergonomics we want just isn't possible with property wrappers today, but your solution could offer an alternative...we'll have to think about it. One other name to consider: @ObservedStore.Of<State>.Sending<Action> I think in an ideal world we would be able to do something like: // Your proposal:
@ObservedStore var store: Store<State, Action>
// $store == ViewStore<State, Action>
// But overloadable:
@ObservedStore(state: ViewState.init, action: ViewAction.init)
var store: Store<State, Action>
// $store == ViewStore<ViewState, ViewAction> This isn't possible today for a few reasons:
We've explored a bunch of other permutations that were worse, and most of them didn't work. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello!
I've recently experimented with a
DynamicProperty
called@ObservedStore
. The objective was to offer an alternative to top-levelWithViewStore
while keeping the same.init(store: Store<State, Action>)
synthetized initializer for the views.If your store's state is
Equatable
and you want to observe all of it, thenThe projected value right now is a
ViewStore<State, Action>
. You can instantiate this view from a parent as usual:View(store: store)
.In case you need a
ViewState
, I've introduced aViewStateProtocol
which is anEquatable
that as aninit(state: State)
initializer. When using aViewState
:If you need a
ViewAction
that narrows the actions you can send to aStore
, I've introduced theViewActionProtocol
which has only a staticembed: (Self) -> StoreAction
requirement. When using aViewAction
and aViewState
, you would useAgain, when you instantiate the view from a parent, you still use
View(store: store)
.I've experimented with a few different names for the
ViewState/Action
chain:I've selected the first for obvious reasons after trying the third (there is currently a remnant of it if you want a
ViewAction
only:@ObservedStore.ViewAction<ViewAction>
).EDIT: The branch was updated and is now using:
@ObservedStore.Of<ViewState>.Sending<ViewAction>
You can find here a branch with this experiment, and where I've updated TicTacToe and one Study to show how it's used. Unfortunately, I've reindented the bodies when removing the
WithViewStore
, so diffing shows many changes, but in bodies, it merely consists in replacingviewStore
by$store
. You can also define avar viewStore: ViewStore<ViewState, ViewAction> { $store }
computed property in your view. It is also possible to define aStoreViewProtocol: View
with this property already defined in an extension.There's a lot of repeats in the code, but I've tried many things without success, and this is by far the easiest. In the same branch, you can check the first commit where I used another approach that was working but was rather odd:
@ViewScope<ViewState>.ObservedStore var store: Store<>
.This is not a replacement for
WithViewStore
, which is very convenient and allows to really narrow the scope of modified views, but this property wrapper can provide a convenient alternative to switching to a manual@ObservedObject var viewStore
.It is also possible to use this property wrapper in another way. Right now, we're returning the
ViewStore
as its projected value, but we could imagine returning the property wrapper itself instead. By doing so, we could probably regain the$
notation for bindings (I don't remember exactly why it was pulled off, but I believe it was related toObservableObject
getting in the way with its own bindings. WithObservedStore
, we have one level of indirection that can maybe provides enough room to reinstate this functionality. I didn't push farther yet, as I don't know if the whole idea of this wrapper is sound.What do you think?
Beta Was this translation helpful? Give feedback.
All reactions