Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread safety #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 4 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
19 changes: 5 additions & 14 deletions ReSwift/CoreTypes/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ open class Store<State: StateType>: StoreType {
}
}

///The dispatch function to be used.
///
///Note: The default dispatch function runs alls reductions within a thread-safe synchronized block.
public var dispatchFunction: DispatchFunction!

private var reducer: Reducer<State>

var subscriptions: [SubscriptionType] = []

private var isDispatching = false

public required init(
reducer: @escaping Reducer<State>,
state: State?,
Expand Down Expand Up @@ -109,20 +110,10 @@ open class Store<State: StateType>: StoreType {

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

isDispatching = true
objc_sync_enter(self)
let newState = reducer(action, state)
isDispatching = false

state = newState
objc_sync_exit(self)
}

open func dispatch(_ action: Action) {
Expand Down