@@ -307,10 +307,10 @@ public final class Store<State, Action> {
307307 self . threadCheck ( status: . scope)
308308
309309 #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 ) } )
311311 #else
312312 return ( self . scope ?? StoreScope ( root: self ) )
313- . rescope ( self , state: toChildState, action: fromChildAction)
313+ . rescope ( self , state: toChildState, action: { fromChildAction ( $1 ) } )
314314 #endif
315315 }
316316
@@ -326,6 +326,19 @@ public final class Store<State, Action> {
326326 self . scope ( state: toChildState, action: { $0 } )
327327 }
328328
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+
329342 @_spi ( Internals) public func send(
330343 _ action: Action ,
331344 originatingFrom originatingAction: Action ? = nil
@@ -571,7 +584,7 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
571584 fileprivate func rescope< ChildState, ChildAction> (
572585 _ store: Store < State , Action > ,
573586 state toChildState: @escaping ( State ) -> ChildState ,
574- action fromChildAction: @escaping ( ChildAction ) -> Action
587+ action fromChildAction: @escaping ( ChildState , ChildAction ) -> Action ?
575588 ) -> Store < ChildState , ChildAction > {
576589 ( self as? any AnyScopedReducer ?? ScopedReducer ( rootStore: store) )
577590 . rescope ( store, state: toChildState, action: fromChildAction)
@@ -584,7 +597,7 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
584597 let rootStore : Store < RootState , RootAction >
585598 let toScopedState : ( RootState ) -> ScopedState
586599 private let parentStores : [ Any ]
587- let fromScopedAction : ( ScopedAction ) -> RootAction
600+ let fromScopedAction : ( ScopedState , ScopedAction ) -> RootAction ?
588601 private( set) var isSending = false
589602
590603 @inlinable
@@ -593,14 +606,14 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
593606 self . rootStore = rootStore
594607 self . toScopedState = { $0 }
595608 self . parentStores = [ ]
596- self . fromScopedAction = { $0 }
609+ self . fromScopedAction = { $1 }
597610 }
598611
599612 @inlinable
600613 init (
601614 rootStore: Store < RootState , RootAction > ,
602615 state toScopedState: @escaping ( RootState ) -> ScopedState ,
603- action fromScopedAction: @escaping ( ScopedAction ) -> RootAction ,
616+ action fromScopedAction: @escaping ( ScopedState , ScopedAction ) -> RootAction ? ,
604617 parentStores: [ Any ]
605618 ) {
606619 self . rootStore = rootStore
@@ -618,7 +631,7 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
618631 state = self . toScopedState ( self . rootStore. state. value)
619632 self . isSending = false
620633 }
621- if let task = self . rootStore. send ( self . fromScopedAction ( action) ) {
634+ if let action = self . fromScopedAction ( state , action ) , let task = self . rootStore. send ( action) {
622635 return . fireAndForget { await task. cancellableValue }
623636 } else {
624637 return . none
@@ -630,7 +643,7 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
630643 func rescope< ScopedState, ScopedAction, RescopedState, RescopedAction> (
631644 _ store: Store < ScopedState , ScopedAction > ,
632645 state toRescopedState: @escaping ( ScopedState ) -> RescopedState ,
633- action fromRescopedAction: @escaping ( RescopedAction ) -> ScopedAction
646+ action fromRescopedAction: @escaping ( RescopedState , RescopedAction ) -> ScopedAction ?
634647 ) -> Store < RescopedState , RescopedAction >
635648 }
636649
@@ -639,13 +652,13 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
639652 func rescope< ScopedState, ScopedAction, RescopedState, RescopedAction> (
640653 _ store: Store < ScopedState , ScopedAction > ,
641654 state toRescopedState: @escaping ( ScopedState ) -> RescopedState ,
642- action fromRescopedAction: @escaping ( RescopedAction ) -> ScopedAction
655+ action fromRescopedAction: @escaping ( RescopedState , RescopedAction ) -> ScopedAction ?
643656 ) -> Store < RescopedState , RescopedAction > {
644- let fromScopedAction = self . fromScopedAction as! ( ScopedAction ) -> RootAction
657+ let fromScopedAction = self . fromScopedAction as! ( ScopedState , ScopedAction ) -> RootAction ?
645658 let reducer = ScopedReducer < RootState , RootAction , RescopedState , RescopedAction > (
646659 rootStore: self . rootStore,
647660 state: { _ in toRescopedState ( store. state. value) } ,
648- action: { fromScopedAction ( fromRescopedAction ( $0) ) } ,
661+ action: { fromRescopedAction ( $0, $1 ) . flatMap { fromScopedAction ( store . state . value , $0 ) } } ,
649662 parentStores: self . parentStores + [ store]
650663 )
651664 let childStore = Store < RescopedState , RescopedAction > (
@@ -666,7 +679,7 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
666679 func rescope< ScopedState, ScopedAction, RescopedState, RescopedAction> (
667680 _ store: Store < ScopedState , ScopedAction > ,
668681 state toRescopedState: @escaping ( ScopedState ) -> RescopedState ,
669- action fromRescopedAction: @escaping ( RescopedAction ) -> ScopedAction
682+ action fromRescopedAction: @escaping ( RescopedState , RescopedAction ) -> ScopedAction ?
670683 ) -> Store < RescopedState , RescopedAction >
671684 }
672685
@@ -675,12 +688,15 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
675688 let fromScopedAction : Any
676689
677690 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+ )
679695 }
680696
681- private init < ScopedAction> (
697+ private init < ScopedState , ScopedAction> (
682698 root: Store < RootState , RootAction > ,
683- fromScopedAction: @escaping ( ScopedAction ) -> RootAction
699+ fromScopedAction: @escaping ( ScopedState , ScopedAction ) -> RootAction ?
684700 ) {
685701 self . root = root
686702 self . fromScopedAction = fromScopedAction
@@ -689,17 +705,21 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
689705 func rescope< ScopedState, ScopedAction, RescopedState, RescopedAction> (
690706 _ scopedStore: Store < ScopedState , ScopedAction > ,
691707 state toRescopedState: @escaping ( ScopedState ) -> RescopedState ,
692- action fromRescopedAction: @escaping ( RescopedAction ) -> ScopedAction
708+ action fromRescopedAction: @escaping ( RescopedState , RescopedAction ) -> ScopedAction ?
693709 ) -> Store < RescopedState , RescopedAction > {
694- let fromScopedAction = self . fromScopedAction as! ( ScopedAction ) -> RootAction
710+ let fromScopedAction = self . fromScopedAction as! ( ScopedState , ScopedAction ) -> RootAction ?
695711
696712 var isSending = false
697713 let rescopedStore = Store < RescopedState , RescopedAction > (
698714 initialState: toRescopedState ( scopedStore. state. value) ,
699715 reducer: . init { rescopedState, rescopedAction, _ in
700716 isSending = true
701717 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)
703723 rescopedState = toRescopedState ( scopedStore. state. value)
704724 if let task = task {
705725 return . fireAndForget { await task. cancellableValue }
@@ -717,7 +737,9 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
717737 }
718738 rescopedStore. scope = StoreScope < RootState , RootAction > (
719739 root: self . root,
720- fromScopedAction: { fromScopedAction ( fromRescopedAction ( $0) ) }
740+ fromScopedAction: {
741+ fromRescopedAction ( $0, $1) . flatMap { fromScopedAction ( scopedStore. state. value, $0) }
742+ }
721743 )
722744 return rescopedStore
723745 }
0 commit comments