Skip to content

Instantly share code, notes, and snippets.

@lukeredpath
Created June 16, 2023 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukeredpath/34240b0df77afe81e4ee2d1cccfe30f5 to your computer and use it in GitHub Desktop.
Save lukeredpath/34240b0df77afe81e4ee2d1cccfe30f5 to your computer and use it in GitHub Desktop.
Ergonomic store scoping
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