Created
June 16, 2023 19:15
-
-
Save lukeredpath/34240b0df77afe81e4ee2d1cccfe30f5 to your computer and use it in GitHub Desktop.
Ergonomic store scoping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct StoreScope<State, Action, ChildState, ChildAction> { | |
typealias Scope<_State, _Action> = StoreScope<State, Action, _State, _Action> | |
typealias ScopeOf<R: ReducerProtocol> = StoreScope<State, Action, R.State, R.Action> | |
typealias PresentationScope<_State, _Action> = StoreScope<State, Action, PresentationState<_State>, PresentationAction<_Action>> | |
typealias PresentationScopeOf<R: ReducerProtocol> = StoreScope<State, Action, PresentationState<R.State>, PresentationAction<R.Action>> | |
var toChildState: (State) -> ChildState | |
var fromChildAction: (ChildAction) -> Action | |
} | |
extension Store { | |
func scope<ChildState, ChildAction>( | |
to storeScope: StoreScope<State, Action, ChildState, ChildAction> | |
) -> Store<ChildState, ChildAction> { | |
self.scope(state: storeScope.toChildState, action: storeScope.fromChildAction) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment