<commit_before>//
//  Store.swift
//  ReactiveReSwift
//
//  Created by <NAME> on 11/17/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

/**
 This class is the default implementation of the `Store` protocol. You will use this
 store in most of your applications. You shouldn't need to implement your own store.
 You initialize the store with a reducer and an initial application state,
 contained within an observable.
 */

public class Store<ObservableProperty: ObservablePropertyType>: StoreType where ObservableProperty.ValueType: StateType {

    public typealias StoreMiddleware = Middleware<ObservableProperty.ValueType>
    public typealias StoreReducer = Reducer<ObservableProperty.ValueType>

    public var dispatchMiddleware: Middleware<ObservableProperty.ValueType>!

    private var reducer: StoreReducer

    public var observable: ObservableProperty!

    private var isDispatching = false

    private var disposeBag = SubscriptionReferenceBag()

    public required convenience init(reducer: StoreReducer, stateType: ObservableProperty.ValueType.Type, observable: ObservableProperty) {
        self.init(reducer: reducer, stateType: stateType, observable: observable, middleware: Middleware { $2 })
    }

    public required init(reducer: StoreReducer, stateType: ObservableProperty.ValueType.Type, observable: ObservableProperty, middleware: StoreMiddleware) {
        self.reducer = reducer
        self.observable = observable
        self.dispatchMiddleware = middleware
    }

    private func defaultDispatch(action: Action) {
        guard !isDispatching else {
            raiseFatalError("ReSwift:IllegalDispatchFromReducer - Reducers may not dispatch actions.")
        }

        isDispatching = true
        observable.value = reducer.run(action: action, state: observable.value)
        isDispatching = false
    }

    @discardableResult
    public func dispatch(_ action: Action) {
        if let mappedAction = dispatchMiddleware.run(state: { self.observable.value }, dispatch: { self.dispatch($0) }, argument: action) {
            defaultDispatch(action: mappedAction)
        }
    }<commit_msg>Convert isDispatching to a lock<commit_after>//
//  Store.swift
//  ReactiveReSwift
//
//  Created by <NAME> on 11/17/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

/**
 This class is the default implementation of the `Store` protocol. You will use this
 store in most of your applications. You shouldn't need to implement your own store.
 You initialize the store with a reducer and an initial application state,
 contained within an observable.
 */

import Foundation

public class Store<ObservableProperty: ObservablePropertyType>: StoreType where ObservableProperty.ValueType: StateType {

    public typealias StoreMiddleware = Middleware<ObservableProperty.ValueType>
    public typealias StoreReducer = Reducer<ObservableProperty.ValueType>

    public var dispatchMiddleware: Middleware<ObservableProperty.ValueType>!

    private var reducer: StoreReducer

    public var observable: ObservableProperty!

    private let dispatchingLock = NSLock()

    private var disposeBag = SubscriptionReferenceBag()

    public required convenience init(reducer: StoreReducer, stateType: ObservableProperty.ValueType.Type, observable: ObservableProperty) {
        self.init(reducer: reducer, stateType: stateType, observable: observable, middleware: Middleware { $2 })
    }

    public required init(reducer: StoreReducer, stateType: ObservableProperty.ValueType.Type, observable: ObservableProperty, middleware: StoreMiddleware) {
        self.reducer = reducer
        self.observable = observable
        self.dispatchMiddleware = middleware
    }

    private func defaultDispatch(action: Action) {
        guard dispatchingLock.try() else {
            raiseFatalError("ReSwift:IllegalDispatchFromReducer - Reducers may not dispatch actions.")
        }
        observable.value = reducer.run(action: action, state: observable.value)
        dispatchingLock.unlock()
    }

    @discardableResult
    public func dispatch(_ action: Action) {
        if let mappedAction = dispatchMiddleware.run(state: { self.observable.value }, dispatch: { self.dispatch($0) }, argument: action) {
            defaultDispatch(action: mappedAction)
        }
    }
{'fraction_non_alphanumeric': 0.07807461999078766, 'fraction_numerical': 0.0055274067250115156, 'mean_word_length': 3.8042035398230087, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6629808', 'n_tokens_mistral': 1145, 'n_tokens_neox': 1080, 'n_words': 343}