Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions ReSwift/CoreTypes/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ open class Store<State>: StoreType {

private(set) public var state: State! {
didSet {
guard canEmitNewState else { return }

subscriptions.forEach {
if $0.subscriber == nil {
subscriptions.remove($0)
Expand All @@ -34,10 +36,10 @@ open class Store<State>: StoreType {
private var reducer: Reducer<State>

var subscriptions: Set<SubscriptionType> = []

private var canEmitNewState: Bool = true

private var isDispatching = Synchronized<Bool>(false)

/// Indicates if new subscriptions attempt to apply `skipRepeats`
/// Indicates if new subscriptions attempt to apply `skipRepeats`
/// by default.
fileprivate let subscriptionsAutomaticallySkipRepeats: Bool

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

// swiftlint:disable:next identifier_name
open func _defaultDispatch(action: Action) {
guard !isDispatching.value else {
raiseFatalError(
"ReSwift:ConcurrentMutationError- Action has been dispatched while" +
" a previous action is being processed. A reducer" +
" is dispatching an action, or ReSwift is used in a concurrent context" +
" (e.g. from multiple threads). Action: \(action)"
)
}

isDispatching.value { $0 = true }
let newState = reducer(action, state)
isDispatching.value { $0 = false }

state = newState
}

open func dispatch(_ action: Action) {
dispatchFunction(action)
}

open func batchDispatch(_ actions: [Action]) {
canEmitNewState = false
actions.forEach { self.dispatch($0) }
canEmitNewState = true

let newState = state
state = newState
}

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