@@ -549,48 +549,44 @@ public final class Store<State, Action> {
549
549
}
550
550
551
551
private protocol AnyScope {
552
- func rescope< State , Action , ChildState , ChildAction > (
553
- _ store: Store < State , Action > ,
554
- state toNewChildState : @escaping ( State ) -> ChildState ,
555
- action fromNewChildAction : @escaping ( ChildAction ) -> Action
556
- ) -> Store < ChildState , ChildAction >
552
+ func rescope< ScopedState , ScopedAction , RescopedState , RescopedAction > (
553
+ _ store: Store < ScopedState , ScopedAction > ,
554
+ state toRescopedState : @escaping ( ScopedState ) -> RescopedState ,
555
+ action fromRescopedAction : @escaping ( RescopedAction ) -> ScopedAction
556
+ ) -> Store < RescopedState , RescopedAction >
557
557
}
558
558
559
559
private struct Scope < RootState, RootAction> : AnyScope {
560
560
let root : Store < RootState , RootAction >
561
- let toChildState : Any
562
- let fromChildAction : Any
561
+ let fromScopedAction : Any
563
562
564
563
init ( root: Store < RootState , RootAction > ) {
565
- self . init ( root: root, toChildState : { $0 } , fromChildAction : { $0 } )
564
+ self . init ( root: root, fromScopedAction : { $0 } )
566
565
}
567
566
568
- private init < State , Action > (
567
+ private init < ScopedAction > (
569
568
root: Store < RootState , RootAction > ,
570
- toChildState: @escaping ( RootState ) -> State ,
571
- fromChildAction: @escaping ( Action ) -> RootAction
569
+ fromScopedAction: @escaping ( ScopedAction ) -> RootAction
572
570
) {
573
571
self . root = root
574
- self . toChildState = toChildState
575
- self . fromChildAction = fromChildAction
572
+ self . fromScopedAction = fromScopedAction
576
573
}
577
574
578
- func rescope< State, Action, ChildState, ChildAction> (
579
- _ store: Store < State , Action > ,
580
- state toChildState: @escaping ( State ) -> ChildState ,
581
- action fromChildAction: @escaping ( ChildAction ) -> Action
582
- ) -> Store < ChildState , ChildAction > {
583
- let toState = self . toChildState as! ( RootState ) -> State
584
- let fromAction = self . fromChildAction as! ( Action ) -> RootAction
585
-
575
+ func rescope< ScopedState, ScopedAction, RescopedState, RescopedAction> (
576
+ _ scopedStore: Store < ScopedState , ScopedAction > ,
577
+ state toRescopedState: @escaping ( ScopedState ) -> RescopedState ,
578
+ action fromRescopedAction: @escaping ( RescopedAction ) -> ScopedAction
579
+ ) -> Store < RescopedState , RescopedAction > {
580
+ let fromScopedAction = self . fromScopedAction as! ( ScopedAction ) -> RootAction
581
+
586
582
var isSending = false
587
- let childStore = Store < ChildState , ChildAction > (
588
- initialState: toChildState ( store . state) ,
589
- reducer: . init { childState , childAction , _ in
583
+ let rescopedStore = Store < RescopedState , RescopedAction > (
584
+ initialState: toRescopedState ( scopedStore . state) ,
585
+ reducer: . init { rescopedState , rescopedAction , _ in
590
586
isSending = true
591
587
defer { isSending = false }
592
- let task = self . root. send ( fromAction ( fromChildAction ( childAction ) ) )
593
- childState = toChildState ( store . state)
588
+ let task = self . root. send ( fromScopedAction ( fromRescopedAction ( rescopedAction ) ) )
589
+ rescopedState = toRescopedState ( scopedStore . state)
594
590
if let task = task {
595
591
return . fireAndForget { await task. cancellableValue }
596
592
} else {
@@ -599,17 +595,16 @@ private struct Scope<RootState, RootAction>: AnyScope {
599
595
} ,
600
596
environment: ( )
601
597
)
602
- childStore . parentDisposable = store . producer
598
+ scopedStore . parentDisposable = scopedStore . producer
603
599
. skip ( first: 1 )
604
- . startWithValues { [ weak childStore ] newValue in
600
+ . startWithValues { [ weak rescopedStore ] newValue in
605
601
guard !isSending else { return }
606
- childStore ? . state = toChildState ( newValue)
602
+ rescopedStore ? . state = toRescopedState ( newValue)
607
603
}
608
- childStore . scope = Scope < RootState , RootAction > (
604
+ rescopedStore . scope = Scope < RootState , RootAction > (
609
605
root: self . root,
610
- toChildState: { toChildState ( toState ( $0) ) } ,
611
- fromChildAction: { fromAction ( fromChildAction ( $0) ) }
606
+ fromScopedAction: { fromScopedAction ( fromRescopedAction ( $0) ) }
612
607
)
613
- return childStore
608
+ return rescopedStore
614
609
}
615
610
}
0 commit comments