@@ -9,6 +9,8 @@ public class BaseStore<State: Equatable, RawAction, RefinedAction>: StoreProtoco
99 public typealias ActionsAndState = ( [ RefinedAction ] , ( previous: State , current: State ) )
1010 @Published
1111 public private( set) var state : State
12+ @Published
13+ public var dispatchEnabled = true
1214 public var statePublisher : Published < State > . Publisher { $state }
1315 public var underlying : BaseStore < State , RawAction , RefinedAction > { self }
1416 public let stateLens : ( State ) -> State = { $0 }
@@ -137,6 +139,10 @@ public class BaseStore<State: Equatable, RawAction, RefinedAction>: StoreProtoco
137139 collect: Bool = false ,
138140 actions: S
139141 ) where S. Element == Action {
142+ guard dispatchEnabled else {
143+ return
144+ }
145+
140146 let maxPublishers : Subscribers . Demand = serially. if ( true : . max ( 1 ) , false : . unlimited)
141147 weak var `self` = self
142148
@@ -177,6 +183,44 @@ public class BaseStore<State: Equatable, RawAction, RefinedAction>: StoreProtoco
177183 }
178184
179185 open func injectBypassingMiddleware< S: Sequence > ( actions: S ) where S. Element == RefinedAction {
180- _postMiddlewareRefinedActions. send ( . init( actions) )
186+ dispatchEnabled. if (
187+ true : _postMiddlewareRefinedActions. send ( . init( actions) )
188+ )
189+ }
190+
191+ open func _replay< S: Sequence > ( _ values: S ) where S. Element == ( offset: Double , actions: [ RefinedAction ] ) {
192+ values
193+ . publisher
194+ . flatMap { offset, actions in
195+ Just ( actions) . delay (
196+ for: . seconds( max ( 0 , offset) ) ,
197+ scheduler: DispatchQueue . global ( )
198+ )
199+ }
200+
201+ values. dropLast ( )
202+ . publisher
203+ }
204+
205+ open func replay< S: Sequence > ( _ values: S ) -> AnyPublisher < [ RefinedAction ] , Never >
206+ where S. Element == ( offset: Double , actions: [ RefinedAction ] )
207+ {
208+ values
209+ . publisher
210+ . flatMap { offset, actions in
211+ Just ( actions) . delay (
212+ for: . seconds( max ( 0 , offset) ) ,
213+ scheduler: DispatchQueue . global ( )
214+ )
215+ }
216+ . handleEvents (
217+ receiveSubscription: { _ in self . dispatchEnabled = false } ,
218+ receiveOutput: _postMiddlewareRefinedActions. send,
219+ receiveCompletion: { _ in self . dispatchEnabled = true } ,
220+ receiveCancel: { self . dispatchEnabled = true }
221+ )
222+ // Cancel if dispatch is manually reenabled.
223+ . prefix ( untilOutputFrom: $dispatchEnabled. filter { $0 } )
224+ . eraseToAnyPublisher ( )
181225 }
182226}
0 commit comments