Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Short-circuit the store cache from uncached stores #2605

Merged
merged 5 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 9 additions & 3 deletions Sources/ComposableArchitecture/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ import SwiftUI
/// of the store are also checked to make sure that work is performed on the main thread.
public final class Store<State, Action> {
private var bufferedActions: [Action] = []
fileprivate var canCacheChildren = true
fileprivate var children: [AnyHashable: AnyObject] = [:]
@_spi(Internals) public var effectCancellables: [UUID: AnyCancellable] = [:]
var _isInvalidated = { false }
Expand Down Expand Up @@ -1033,14 +1034,16 @@ extension ScopedStoreReducer: AnyScopedStoreReducer {
removeDuplicates isDuplicate: ((ChildState, ChildState) -> Bool)?
) -> Store<ChildState, ChildAction> {
let id = id?(store.stateSubject.value)
if let id = id,
if
store.canCacheChildren,
let id = id,
let childStore = store.children[id] as? Store<ChildState, ChildAction>
{
return childStore
}
let fromAction = self.fromAction as! (A) -> RootAction?
let isInvalid =
id == nil
id == nil || !store.canCacheChildren
? {
store._isInvalidated() || isInvalid?(store.stateSubject.value) == true
}
Expand All @@ -1067,6 +1070,7 @@ extension ScopedStoreReducer: AnyScopedStoreReducer {
reducer
}
childStore._isInvalidated = isInvalid
childStore.canCacheChildren = store.canCacheChildren && id != nil
childStore.parentCancellable = store.stateSubject
.dropFirst()
.sink { [weak store, weak childStore] state in
Expand All @@ -1092,7 +1096,9 @@ extension ScopedStoreReducer: AnyScopedStoreReducer {
Logger.shared.log("\(storeTypeName(of: store)).scope")
}
if let id = id {
store.children[id] = childStore
if store.canCacheChildren {
store.children[id] = childStore
}
}
return childStore
}
Expand Down
106 changes: 106 additions & 0 deletions Tests/ComposableArchitectureTests/StoreLifetimeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import XCTest
import ComposableArchitecture

@MainActor
final class StoreLifetimeTests: BaseTCATestCase {
func testStoreCaching() {
let grandparentStore = Store(initialState: Grandparent.State()) {
Grandparent()
}
let parentStore = grandparentStore.scope(state: \.child, action: \.child)
XCTAssertTrue(parentStore === grandparentStore.scope(state: \.child, action: \.child))
XCTAssertFalse(
parentStore === grandparentStore.scope(state: { $0.child }, action: { .child($0) })
)
let childStore = parentStore.scope(state: \.child, action: \.child)
XCTAssertTrue(childStore === parentStore.scope(state: \.child, action: \.child))
XCTAssertFalse(
childStore === parentStore.scope(state: { $0.child }, action: { .child($0) })
)
}

func testStoreInvalidation() {
let grandparentStore = Store(initialState: Grandparent.State()) {
Grandparent()
}
var parentStore: Store! = grandparentStore.scope(state: { $0.child }, action: { .child($0) })
let childStore = parentStore.scope(state: \.child, action: \.child)

childStore.send(.tap)
XCTAssertEqual(1, grandparentStore.withState(\.child.child.count))
XCTAssertEqual(1, parentStore.withState(\.child.count))
XCTAssertEqual(1, childStore.withState(\.count))
grandparentStore.send(.incrementGrandchild)
XCTAssertEqual(2, grandparentStore.withState(\.child.child.count))
XCTAssertEqual(2, parentStore.withState(\.child.count))
XCTAssertEqual(2, childStore.withState(\.count))

parentStore = nil

childStore.send(.tap)
XCTAssertEqual(3, grandparentStore.withState(\.child.child.count))
XCTAssertEqual(3, childStore.withState(\.count))
grandparentStore.send(.incrementGrandchild)
XCTAssertEqual(4, grandparentStore.withState(\.child.child.count))
XCTAssertEqual(4, childStore.withState(\.count))
Comment on lines +40 to +45
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both childStore assertions fail on 1.5.0...main.

}
}

@Reducer
fileprivate struct Child {
struct State: Equatable {
var count = 0
}
enum Action {
case tap
}
var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .tap:
state.count += 1
return .none
}
}
}
}

@Reducer
fileprivate struct Parent {
struct State: Equatable {
var child = Child.State()
}
enum Action {
case child(Child.Action)
}
var body: some ReducerOf<Self> {
Scope(state: \.child, action: \.child) {
Child()
}
}
}

@Reducer
fileprivate struct Grandparent {
struct State: Equatable {
var child = Parent.State()
}
enum Action {
case child(Parent.Action)
case incrementGrandchild
}
var body: some ReducerOf<Self> {
Scope(state: \.child, action: \.child) {
Parent()
}
Reduce { state, action in
switch action {
case .child:
return .none
case .incrementGrandchild:
state.child.child.count += 1
return .none
}
}
}
}