-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5870915
wip
stephencelis 475bd76
wip
stephencelis 121b6d4
Merge branch 'store-lifetime-tests' into cache-short-circuit
stephencelis 81662da
Update Sources/ComposableArchitecture/Store.swift
stephencelis fc32d7c
Merge branch 'main' into cache-short-circuit
stephencelis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
106 changes: 106 additions & 0 deletions
106
Tests/ComposableArchitectureTests/StoreLifetimeTests.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,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)) | ||
} | ||
} | ||
|
||
@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 | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both
childStore
assertions fail on1.5.0...main
.