Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Sources/ComposableArchitecture/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public final class Store<State, Action>: _Store {
.compactMap { [weak self] in (self?.currentState as? T)?._$id }
.removeDuplicates()
.dropFirst()
.sink { [weak self] _ in
.sink { [weak self, weak parent] _ in
guard let scopeID = self?.scopeID
else { return }
parent?.removeChild(scopeID: scopeID)
Expand Down
63 changes: 63 additions & 0 deletions Tests/ComposableArchitectureTests/StoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@
childTask.cancel()
await mainQueue.advance(by: 1)
try await Task.sleep(nanoseconds: 100_000_000)
XCTTODO(

Check warning on line 849 in Tests/ComposableArchitectureTests/StoreTests.swift

View workflow job for this annotation

GitHub Actions / xcodebuild (16) (test, MACOS, 16.4)

'XCTTODO' is deprecated: This is a test that currently fails but should not in the future.

Check warning on line 849 in Tests/ComposableArchitectureTests/StoreTests.swift

View workflow job for this annotation

GitHub Actions / xcodebuild (16) (test, IOS, 16.4)

'XCTTODO' is deprecated: This is a test that currently fails but should not in the future.
"""
This fails because cancelling a child task will cancel all parent effects too.
"""
Expand Down Expand Up @@ -1177,7 +1177,7 @@

@MainActor
func testSharedMutation() async {
XCTTODO(

Check warning on line 1180 in Tests/ComposableArchitectureTests/StoreTests.swift

View workflow job for this annotation

GitHub Actions / xcodebuild (16) (test, MACOS, 16.4)

'XCTTODO' is deprecated: This is a test that currently fails but should not in the future.

Check warning on line 1180 in Tests/ComposableArchitectureTests/StoreTests.swift

View workflow job for this annotation

GitHub Actions / xcodebuild (16) (test, IOS, 16.4)

'XCTTODO' is deprecated: This is a test that currently fails but should not in the future.
"""
Ideally this will pass in 2.0 but it's a breaking change for test stores to not eagerly \
process all received actions.
Expand Down Expand Up @@ -1338,6 +1338,69 @@
#expect(store.count == 1729)
}
}

@Suite
struct ParentChildLifecycle {
@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *)
@MainActor
@Test
func parentChildLifecycle() async throws {
weak var parentStore: StoreOf<Parent>?
do {
let store = Store(initialState: Parent.State()) {
Parent()
}
parentStore = store
store.send(.presentButtonTapped)
guard let _ = store.scope(state: \.child, action: \.child) else {
Issue.record("Child is 'nil'")
return
}
}
#expect(parentStore == nil)
}

@Reducer struct Child {
@ObservableState struct State {
var count = 0
}
enum Action {
case incrementButtonTapped
}
var body: some Reducer<State, Action> {
Reduce { state, action in
switch action {
case .incrementButtonTapped:
state.count += 1
return .none
}
}
}
}
@Reducer struct Parent {
@ObservableState struct State {
@Presents var child: Child.State?
}
enum Action {
case child(PresentationAction<Child.Action>)
case presentButtonTapped
}
var body: some Reducer<State, Action> {
Reduce { state, action in
switch action {
case .child:
return .none
case .presentButtonTapped:
state.child = Child.State()
return .none
}
}
.ifLet(\.$child, action: \.child) {
Child()
}
}
}
}
}
#endif

Expand Down
Loading