@@ -307,10 +307,10 @@ public final class Store<State, Action> {
307
307
self . threadCheck ( status: . scope)
308
308
309
309
#if swift(>=5.7)
310
- return self . reducer. rescope ( self , state: toChildState, action: fromChildAction)
310
+ return self . reducer. rescope ( self , state: toChildState, action: { fromChildAction ( $1 ) } )
311
311
#else
312
312
return ( self . scope ?? StoreScope ( root: self ) )
313
- . rescope ( self , state: toChildState, action: fromChildAction)
313
+ . rescope ( self , state: toChildState, action: { fromChildAction ( $1 ) } )
314
314
#endif
315
315
}
316
316
@@ -326,6 +326,19 @@ public final class Store<State, Action> {
326
326
self . scope ( state: toChildState, action: { $0 } )
327
327
}
328
328
329
+ @_spi ( Internals) public func filter(
330
+ _ isSent: @escaping ( State , Action ) -> Bool
331
+ ) -> Store < State , Action > {
332
+ self . threadCheck ( status: . scope)
333
+
334
+ #if swift(>=5.7)
335
+ return self . reducer. rescope ( self , state: { $0 } , action: { isSent ( $0, $1) ? $1 : nil } )
336
+ #else
337
+ return ( self . scope ?? StoreScope ( root: self ) )
338
+ . rescope ( self , state: { $0 } , action: { isSent ( $0, $1) ? $1 : nil } )
339
+ #endif
340
+ }
341
+
329
342
@_spi ( Internals) public func send(
330
343
_ action: Action ,
331
344
originatingFrom originatingAction: Action ? = nil
@@ -571,7 +584,7 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
571
584
fileprivate func rescope< ChildState, ChildAction> (
572
585
_ store: Store < State , Action > ,
573
586
state toChildState: @escaping ( State ) -> ChildState ,
574
- action fromChildAction: @escaping ( ChildAction ) -> Action
587
+ action fromChildAction: @escaping ( ChildState , ChildAction ) -> Action ?
575
588
) -> Store < ChildState , ChildAction > {
576
589
( self as? any AnyScopedReducer ?? ScopedReducer ( rootStore: store) )
577
590
. rescope ( store, state: toChildState, action: fromChildAction)
@@ -584,7 +597,7 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
584
597
let rootStore : Store < RootState , RootAction >
585
598
let toScopedState : ( RootState ) -> ScopedState
586
599
private let parentStores : [ Any ]
587
- let fromScopedAction : ( ScopedAction ) -> RootAction
600
+ let fromScopedAction : ( ScopedState , ScopedAction ) -> RootAction ?
588
601
private( set) var isSending = false
589
602
590
603
@inlinable
@@ -593,14 +606,14 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
593
606
self . rootStore = rootStore
594
607
self . toScopedState = { $0 }
595
608
self . parentStores = [ ]
596
- self . fromScopedAction = { $0 }
609
+ self . fromScopedAction = { $1 }
597
610
}
598
611
599
612
@inlinable
600
613
init (
601
614
rootStore: Store < RootState , RootAction > ,
602
615
state toScopedState: @escaping ( RootState ) -> ScopedState ,
603
- action fromScopedAction: @escaping ( ScopedAction ) -> RootAction ,
616
+ action fromScopedAction: @escaping ( ScopedState , ScopedAction ) -> RootAction ? ,
604
617
parentStores: [ Any ]
605
618
) {
606
619
self . rootStore = rootStore
@@ -618,7 +631,7 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
618
631
state = self . toScopedState ( self . rootStore. state. value)
619
632
self . isSending = false
620
633
}
621
- if let task = self . rootStore. send ( self . fromScopedAction ( action) ) {
634
+ if let action = self . fromScopedAction ( state , action ) , let task = self . rootStore. send ( action) {
622
635
return . fireAndForget { await task. cancellableValue }
623
636
} else {
624
637
return . none
@@ -630,7 +643,7 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
630
643
func rescope< ScopedState, ScopedAction, RescopedState, RescopedAction> (
631
644
_ store: Store < ScopedState , ScopedAction > ,
632
645
state toRescopedState: @escaping ( ScopedState ) -> RescopedState ,
633
- action fromRescopedAction: @escaping ( RescopedAction ) -> ScopedAction
646
+ action fromRescopedAction: @escaping ( RescopedState , RescopedAction ) -> ScopedAction ?
634
647
) -> Store < RescopedState , RescopedAction >
635
648
}
636
649
@@ -639,13 +652,13 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
639
652
func rescope< ScopedState, ScopedAction, RescopedState, RescopedAction> (
640
653
_ store: Store < ScopedState , ScopedAction > ,
641
654
state toRescopedState: @escaping ( ScopedState ) -> RescopedState ,
642
- action fromRescopedAction: @escaping ( RescopedAction ) -> ScopedAction
655
+ action fromRescopedAction: @escaping ( RescopedState , RescopedAction ) -> ScopedAction ?
643
656
) -> Store < RescopedState , RescopedAction > {
644
- let fromScopedAction = self . fromScopedAction as! ( ScopedAction ) -> RootAction
657
+ let fromScopedAction = self . fromScopedAction as! ( ScopedState , ScopedAction ) -> RootAction ?
645
658
let reducer = ScopedReducer < RootState , RootAction , RescopedState , RescopedAction > (
646
659
rootStore: self . rootStore,
647
660
state: { _ in toRescopedState ( store. state. value) } ,
648
- action: { fromScopedAction ( fromRescopedAction ( $0) ) } ,
661
+ action: { fromRescopedAction ( $0, $1 ) . flatMap { fromScopedAction ( store . state . value , $0 ) } } ,
649
662
parentStores: self . parentStores + [ store]
650
663
)
651
664
let childStore = Store < RescopedState , RescopedAction > (
@@ -666,7 +679,7 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
666
679
func rescope< ScopedState, ScopedAction, RescopedState, RescopedAction> (
667
680
_ store: Store < ScopedState , ScopedAction > ,
668
681
state toRescopedState: @escaping ( ScopedState ) -> RescopedState ,
669
- action fromRescopedAction: @escaping ( RescopedAction ) -> ScopedAction
682
+ action fromRescopedAction: @escaping ( RescopedState , RescopedAction ) -> ScopedAction ?
670
683
) -> Store < RescopedState , RescopedAction >
671
684
}
672
685
@@ -675,12 +688,15 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
675
688
let fromScopedAction : Any
676
689
677
690
init ( root: Store < RootState , RootAction > ) {
678
- self . init ( root: root, fromScopedAction: { $0 } )
691
+ self . init (
692
+ root: root,
693
+ fromScopedAction: { ( state: RootState , action: RootAction ) -> RootAction ? in action }
694
+ )
679
695
}
680
696
681
- private init < ScopedAction> (
697
+ private init < ScopedState , ScopedAction> (
682
698
root: Store < RootState , RootAction > ,
683
- fromScopedAction: @escaping ( ScopedAction ) -> RootAction
699
+ fromScopedAction: @escaping ( ScopedState , ScopedAction ) -> RootAction ?
684
700
) {
685
701
self . root = root
686
702
self . fromScopedAction = fromScopedAction
@@ -689,17 +705,21 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
689
705
func rescope< ScopedState, ScopedAction, RescopedState, RescopedAction> (
690
706
_ scopedStore: Store < ScopedState , ScopedAction > ,
691
707
state toRescopedState: @escaping ( ScopedState ) -> RescopedState ,
692
- action fromRescopedAction: @escaping ( RescopedAction ) -> ScopedAction
708
+ action fromRescopedAction: @escaping ( RescopedState , RescopedAction ) -> ScopedAction ?
693
709
) -> Store < RescopedState , RescopedAction > {
694
- let fromScopedAction = self . fromScopedAction as! ( ScopedAction ) -> RootAction
710
+ let fromScopedAction = self . fromScopedAction as! ( ScopedState , ScopedAction ) -> RootAction ?
695
711
696
712
var isSending = false
697
713
let rescopedStore = Store < RescopedState , RescopedAction > (
698
714
initialState: toRescopedState ( scopedStore. state. value) ,
699
715
reducer: . init { rescopedState, rescopedAction, _ in
700
716
isSending = true
701
717
defer { isSending = false }
702
- let task = self . root. send ( fromScopedAction ( fromRescopedAction ( rescopedAction) ) )
718
+ guard
719
+ let scopedAction = fromRescopedAction ( rescopedState, rescopedAction) ,
720
+ let rootAction = fromScopedAction ( scopedStore. state. value, scopedAction)
721
+ else { return . none }
722
+ let task = self . root. send ( rootAction)
703
723
rescopedState = toRescopedState ( scopedStore. state. value)
704
724
if let task = task {
705
725
return . fireAndForget { await task. cancellableValue }
@@ -717,7 +737,9 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
717
737
}
718
738
rescopedStore. scope = StoreScope < RootState , RootAction > (
719
739
root: self . root,
720
- fromScopedAction: { fromScopedAction ( fromRescopedAction ( $0) ) }
740
+ fromScopedAction: {
741
+ fromRescopedAction ( $0, $1) . flatMap { fromScopedAction ( scopedStore. state. value, $0) }
742
+ }
721
743
)
722
744
return rescopedStore
723
745
}
0 commit comments