@@ -27,6 +27,7 @@ public class BaseStore<State: Equatable, RawAction, RefinedAction>: StoreProtoco
2727 reducer: R ,
2828 middleware: Middleware < State , RawAction , RefinedAction > = . init( ) ,
2929 thunk: Thunk < State , RawAction , RefinedAction > = . init { _, _ in Empty ( ) } ,
30+ sideEffect: SideEffect < RefinedAction > = . init( ) ,
3031 publishOn scheduler: S
3132 ) where R. State == State , R. Action == RefinedAction {
3233 self . state = state
@@ -72,6 +73,9 @@ public class BaseStore<State: Equatable, RawAction, RefinedAction>: StoreProtoco
7273 group? . enter ( )
7374 DispatchQueue . global ( ) . async {
7475 self . _postMiddlewareRefinedActions
76+ . handleEvents ( receiveOutput: {
77+ sideEffect. closure ( $0)
78+ } )
7579 . scan ( state) { state, actions in
7680 actions. reduce ( state, reducer. reduce)
7781 }
@@ -123,12 +127,17 @@ public class BaseStore<State: Equatable, RawAction, RefinedAction>: StoreProtoco
123127 _actionsPairedWithState. eraseToAnyPublisher ( )
124128 }
125129
130+ /// Dispatch actions to the store.
131+ ///
132+ /// - parameter serially: Whether to resolve the actions concurrently or serially.
133+ /// - parameter collect: Whether to collect all refined actions and send them when finished, or send them as they are resolved.
134+ /// - parameter actions: The actions to be sent.
126135 open func dispatch< S: Sequence > (
127136 serially: Bool = false ,
128137 collect: Bool = false ,
129138 actions: S
130139 ) where S. Element == Action {
131- let maxPublishers : Subscribers . Demand = serially ? . max( 1 ) : . unlimited
140+ let maxPublishers : Subscribers . Demand = serially. if ( true : . max ( 1 ) , false : . unlimited)
132141 weak var `self` = self
133142
134143 func recurse( actions: Action ) -> AnyPublisher < [ RefinedAction ] , Never > {
@@ -152,23 +161,19 @@ public class BaseStore<State: Equatable, RawAction, RefinedAction>: StoreProtoco
152161 maxPublishers: maxPublishers,
153162 recurse ( actions: )
154163 )
155- let transformed : AnyPublisher < [ RefinedAction ] , Never >
156164
157- if collect {
158- transformed = recursed
165+ collect. if (
166+ true : recursed
159167 . collect ( )
160168 . map { $0. flatMap { $0 } }
169+ . eraseToAnyPublisher ( ) ,
170+ false : recursed
161171 . eraseToAnyPublisher ( )
162- } else {
163- transformed = recursed
164- . eraseToAnyPublisher ( )
172+ )
173+ . sink {
174+ self ? . _preMiddlewareRefinedActions . send ( $0 )
165175 }
166-
167- transformed
168- . sink {
169- self ? . _preMiddlewareRefinedActions. send ( $0)
170- }
171- . store ( in: & cancellables)
176+ . store ( in: & cancellables)
172177 }
173178
174179 open func injectBypassingMiddleware< S: Sequence > ( actions: S ) where S. Element == RefinedAction {
0 commit comments