Skip to content

Commit

Permalink
Merge pull request #36 from Ryu0118/deprecate-lock
Browse files Browse the repository at this point in the history
Remove Lock when reduce is executed
  • Loading branch information
Ryu0118 authored Oct 1, 2023
2 parents f6a46f9 + 5e0ed52 commit 3f537e5
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 14 deletions.
3 changes: 1 addition & 2 deletions Sources/SimplexArchitecture/StateContainer.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Foundation
import XCTestDynamicOverlay

/// StateContainer is not thread-safe. Therefore, StateContainer must use NSLock or NSRecursiveLock for exclusions when changing values.
/// In Store, NSRecursiveLock is used for exclusions when executing the `reduce(into:action)`.
/// StateContainer is not thread-safe. StateContainer must be accessed from MainActor.
@dynamicMemberLookup
public final class StateContainer<Target: ActionSendable> {
public var reducerState: Target.Reducer.ReducerState {
Expand Down
13 changes: 2 additions & 11 deletions Sources/SimplexArchitecture/Store/Store+send.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ extension Store {
// If Unit Testing is in progress and an action is sent from SideEffect
if _XCTIsTesting, let effectContext = EffectContext.id {
let before = container.copy()
sideEffect = withLock {
reduce(container, action)
}
sideEffect = reduce(container, action)
sentFromEffectActions.append(
ActionTransition(
previous: .init(state: before.states, reducerState: before._reducerState),
Expand All @@ -86,7 +84,7 @@ extension Store {
)
)
} else {
sideEffect = withLock { reduce(container, action) }
sideEffect = reduce(container, action)
}

if case .none = sideEffect.kind {
Expand Down Expand Up @@ -120,13 +118,6 @@ extension Store {
}
}

@inlinable
func withLock<T>(_ operation: () throws -> T) rethrows -> T {
lock.lock()
defer { lock.unlock() }
return try operation()
}

func makeSend(for container: StateContainer<Reducer.Target>) -> Send<Reducer> {
Send(
sendAction: { [weak self] action in
Expand Down
1 change: 0 additions & 1 deletion Sources/SimplexArchitecture/Store/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public final class Store<Reducer: ReducerProtocol> {
// Buffer to store Actions recurrently invoked through SideEffect in a single Action sent from View
@TestOnly var sentFromEffectActions: [ActionTransition<Reducer>] = []

@usableFromInline let lock = NSRecursiveLock()
@usableFromInline var pullbackAction: ((Reducer.Action) -> Void)?
@usableFromInline var pullbackReducerAction: ((Reducer.ReducerAction) -> Void)?

Expand Down

0 comments on commit 3f537e5

Please sign in to comment.