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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions Sources/ComposableArchitecture/RootStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public final class RootStore {
defer { index += 1 }
let action = self.bufferedActions[index] as! Action
let effect = reducer.reduce(into: &currentState, action: action)
let uuid = UUID()

switch effect.operation {
case .none:
break
case let .publisher(publisher):
var didComplete = false
let boxedTask = Box<Task<Void, Never>?>(wrappedValue: nil)
let uuid = UUID()
let effectCancellable = withEscapedDependencies { continuation in
publisher
.receive(on: UIScheduler.shared)
Expand Down Expand Up @@ -88,11 +88,13 @@ public final class RootStore {
}
boxedTask.wrappedValue = task
tasks.withValue { $0.append(task) }
self.effectCancellables[uuid] = effectCancellable
self.effectCancellables[uuid] = AnyCancellable {
task.cancel()
}
}
case let .run(priority, operation):
withEscapedDependencies { continuation in
let task = Task(priority: priority) { @MainActor in
let task = Task(priority: priority) { @MainActor [weak self] in
let isCompleted = LockIsolated(false)
defer { isCompleted.setValue(true) }
await operation(
Expand All @@ -118,14 +120,18 @@ public final class RootStore {
)
}
if let task = continuation.yield({
self.send(effectAction, originatingFrom: action)
self?.send(effectAction, originatingFrom: action)
}) {
tasks.withValue { $0.append(task) }
}
}
)
self?.effectCancellables[uuid] = nil
}
tasks.withValue { $0.append(task) }
self.effectCancellables[uuid] = AnyCancellable {
task.cancel()
}
}
}
}
Expand Down
41 changes: 35 additions & 6 deletions Tests/ComposableArchitectureTests/StoreLifetimeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Combine
@_spi(Logging) import ComposableArchitecture
import XCTest

@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *)
final class StoreLifetimeTests: BaseTCATestCase {
@available(*, deprecated)
@MainActor
Expand Down Expand Up @@ -69,9 +70,6 @@ final class StoreLifetimeTests: BaseTCATestCase {

@MainActor
func testStoreDeinit_RunningEffect() async {
XCTTODO(
"We would like for this to pass, but it requires full deprecation of uncached child stores"
)
Logger.shared.isEnabled = true
let effectFinished = self.expectation(description: "Effect finished")
do {
Expand Down Expand Up @@ -99,9 +97,6 @@ final class StoreLifetimeTests: BaseTCATestCase {

@MainActor
func testStoreDeinit_RunningCombineEffect() async {
XCTTODO(
"We would like for this to pass, but it requires full deprecation of uncached child stores"
)
Logger.shared.isEnabled = true
let effectFinished = self.expectation(description: "Effect finished")
do {
Expand Down Expand Up @@ -129,28 +124,61 @@ final class StoreLifetimeTests: BaseTCATestCase {
await self.fulfillment(of: [effectFinished], timeout: 0.5)
}
#endif

@MainActor
@available(*, deprecated)
func testUnCachedStores() async {
Logger.shared.isEnabled = true
let clock = TestClock()
let store = Store(initialState: Parent.State()) {
Parent()
} withDependencies: {
$0.continuousClock = clock
}
do {
let child = store.scope(state: { $0.child }, action: { .child($0) })
child.send(.start)
XCTAssertEqual(store.withState(\.child.count), 1)
}
await clock.run()
XCTAssertEqual(store.withState(\.child.count), 2)
}
}

@Reducer
@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *)
private struct Child {
struct State: Equatable {
var count = 0
}
enum Action {
case tap
case start
case response
}
@Dependency(\.continuousClock) var clock
var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .tap:
state.count += 1
return .none
case .start:
state.count += 1
return .run { send in
try await clock.sleep(for: .seconds(0))
await send(.response)
}
case .response:
state.count += 1
return .none
}
}
}
}

@Reducer
@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *)
private struct Parent {
struct State: Equatable {
var child = Child.State()
Expand All @@ -166,6 +194,7 @@ private struct Parent {
}

@Reducer
@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *)
private struct Grandparent {
struct State: Equatable {
var child = Parent.State()
Expand Down
Loading