Skip to content

Commit 8215f37

Browse files
authored
Merge pull request #1 from channel-io/feature/multi_dispatch
[Feature] Add batchDispatch
2 parents 11f0a2e + 0801c71 commit 8215f37

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

ReSwift/CoreTypes/Store.swift

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ open class Store<State>: StoreType {
1919

2020
private(set) public var state: State! {
2121
didSet {
22+
guard canEmitNewState else { return }
23+
2224
subscriptions.forEach {
2325
if $0.subscriber == nil {
2426
subscriptions.remove($0)
@@ -34,10 +36,10 @@ open class Store<State>: StoreType {
3436
private var reducer: Reducer<State>
3537

3638
var subscriptions: Set<SubscriptionType> = []
39+
40+
private var canEmitNewState: Bool = true
3741

38-
private var isDispatching = Synchronized<Bool>(false)
39-
40-
/// Indicates if new subscriptions attempt to apply `skipRepeats`
42+
/// Indicates if new subscriptions attempt to apply `skipRepeats`
4143
/// by default.
4244
fileprivate let subscriptionsAutomaticallySkipRepeats: Bool
4345

@@ -156,25 +158,22 @@ open class Store<State>: StoreType {
156158

157159
// swiftlint:disable:next identifier_name
158160
open func _defaultDispatch(action: Action) {
159-
guard !isDispatching.value else {
160-
raiseFatalError(
161-
"ReSwift:ConcurrentMutationError- Action has been dispatched while" +
162-
" a previous action is being processed. A reducer" +
163-
" is dispatching an action, or ReSwift is used in a concurrent context" +
164-
" (e.g. from multiple threads). Action: \(action)"
165-
)
166-
}
167-
168-
isDispatching.value { $0 = true }
169161
let newState = reducer(action, state)
170-
isDispatching.value { $0 = false }
171-
172162
state = newState
173163
}
174164

175165
open func dispatch(_ action: Action) {
176166
dispatchFunction(action)
177167
}
168+
169+
open func batchDispatch(_ actions: [Action]) {
170+
canEmitNewState = false
171+
actions.forEach { self.dispatch($0) }
172+
canEmitNewState = true
173+
174+
let newState = state
175+
state = newState
176+
}
178177

179178
@available(*, deprecated, message: "Deprecated in favor of https://github.com/ReSwift/ReSwift-Thunk")
180179
open func dispatch(_ actionCreatorProvider: @escaping ActionCreator) {

0 commit comments

Comments
 (0)