Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow re-entrant actions to be processed (#1352)
* wip * wip * clean up * wip * wip * wip * wip * fix Co-authored-by: Brandon Williams <mbrandonw@hey.com>
- Loading branch information
1 parent
4c98b43
commit 5b78fbc
Showing
4 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
Tests/ComposableArchitectureTests/CompatibilityTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import Combine | ||
import CombineSchedulers | ||
import ComposableArchitecture | ||
import XCTest | ||
|
||
@MainActor | ||
final class CompatibilityTests: XCTestCase { | ||
func testCaseStudy_ReentrantEffect() { | ||
let cancelID = UUID() | ||
|
||
struct State: Equatable {} | ||
enum Action: Equatable { | ||
case start | ||
case kickOffAction | ||
case actionSender(OnDeinit) | ||
case stop | ||
|
||
var description: String { | ||
switch self { | ||
case .start: | ||
return "start" | ||
case .kickOffAction: | ||
return "kickOffAction" | ||
case .actionSender: | ||
return "actionSender" | ||
case .stop: | ||
return "stop" | ||
} | ||
} | ||
} | ||
let passThroughSubject = PassthroughSubject<Action, Never>() | ||
|
||
var handledActions: [String] = [] | ||
|
||
let reducer = Reducer<State, Action, Void> { state, action, env in | ||
handledActions.append(action.description) | ||
|
||
switch action { | ||
case .start: | ||
return passThroughSubject | ||
.eraseToEffect() | ||
.cancellable(id: cancelID) | ||
|
||
case .kickOffAction: | ||
return Effect(value: .actionSender(OnDeinit { passThroughSubject.send(.stop) })) | ||
|
||
case .actionSender: | ||
return .none | ||
|
||
case .stop: | ||
return .cancel(id: cancelID) | ||
} | ||
} | ||
|
||
let store = Store( | ||
initialState: .init(), | ||
reducer: reducer, | ||
environment: () | ||
) | ||
|
||
let viewStore = ViewStore(store) | ||
|
||
viewStore.send(.start) | ||
viewStore.send(.kickOffAction) | ||
|
||
XCTAssertNoDifference( | ||
handledActions, | ||
[ | ||
"start", | ||
"kickOffAction", | ||
"actionSender", | ||
"stop", | ||
] | ||
) | ||
} | ||
} | ||
|
||
private final class OnDeinit: Equatable { | ||
private let onDeinit: () -> () | ||
init(onDeinit: @escaping () -> ()) { | ||
self.onDeinit = onDeinit | ||
} | ||
deinit { self.onDeinit() } | ||
static func == (lhs: OnDeinit, rhs: OnDeinit) -> Bool { true } | ||
} |