-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
[NormalizedStorage] Multiple query #429
Merged
+133
−3
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
|
||
extension NormalizedStoragePath { | ||
|
||
/** | ||
Make a new Derived of a composed object from the storage. | ||
This is an effective way to resolving relationship entities into a single object. it's like SQLite's view. | ||
|
||
``` | ||
store.normalizedStorage(.keyPath(\.db)).derived { | ||
MyComposed( | ||
book: $0.book.find(...) | ||
author: $0.author.find(...) | ||
) | ||
} | ||
``` | ||
|
||
This Derived makes a new composed object if the storage has updated. | ||
There is not filters for entity tables so that Derived possibly makes a new object if not related entity has updated. | ||
*/ | ||
public func derived<Composed: Equatable>(query: @escaping @Sendable (Self.Storage) -> Composed) -> Derived<Composed> { | ||
return store.derived(Pipeline(storageSelector: storageSelector, query: query), queue: .passthrough) | ||
} | ||
} | ||
|
||
private struct Pipeline< | ||
_StorageSelector: StorageSelector, | ||
Output | ||
>: PipelineType, Sendable { | ||
|
||
typealias Input = Changes<_StorageSelector.Source> | ||
typealias Storage = _StorageSelector.Storage | ||
|
||
private let storageSelector: _StorageSelector | ||
private let query: @Sendable (Storage) -> Output | ||
|
||
init( | ||
storageSelector: consuming _StorageSelector, | ||
query: @escaping @Sendable (Storage) -> Output | ||
) { | ||
self.storageSelector = storageSelector | ||
self.query = query | ||
} | ||
|
||
func yield(_ input: consuming Input) -> Output { | ||
|
||
let storage = storageSelector.select(source: input.primitive) | ||
let output = query(storage) | ||
|
||
return output | ||
|
||
} | ||
|
||
func yieldContinuously(_ input: Input) -> Verge.ContinuousResult<Output> { | ||
|
||
guard let previous = input.previous else { | ||
return .new(yield(input)) | ||
} | ||
|
||
// check if the storage has been updated | ||
if NormalizedStorageComparisons<Storage>.StorageComparison()(storageSelector.select(source: input.primitive), storageSelector.select(source: previous.primitive)) { | ||
return .noUpdates | ||
} | ||
|
||
return .new(yield(input)) | ||
|
||
} | ||
|
||
} |
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,56 @@ | ||
import XCTest | ||
import VergeNormalizationDerived | ||
|
||
final class CombiningTests: XCTestCase { | ||
|
||
func test_combine() { | ||
|
||
let store = Store<DemoState, Never>( | ||
initialState: .init() | ||
) | ||
|
||
struct View: Equatable { | ||
var book: Book? | ||
var author: Author? | ||
} | ||
|
||
let derived = store.normalizedStorage(.keyPath(\.db)).derived { storage in | ||
View( | ||
book: storage.book.find(by: .init("1")), | ||
author: storage.author.find(by: .init("1")) | ||
) | ||
} | ||
|
||
let exp = expectation(description: "call") | ||
exp.expectedFulfillmentCount = 3 | ||
|
||
let sub = derived.sinkState { view in | ||
exp.fulfill() | ||
} | ||
|
||
store.commit { | ||
$0.db.performBatchUpdates { t in | ||
t.modifying.book.insert(Book(rawID: "1", authorID: .init("1"))) | ||
t.modifying.author.insert(Author(rawID: "1", name: "Hiroshi")) | ||
} | ||
} | ||
|
||
// no affects | ||
store.commit { | ||
$0.count += 1 | ||
} | ||
|
||
store.commit { | ||
$0.db.performBatchUpdates { t in | ||
t.modifying.author.insert(Author(rawID: "1", name: "Hiroshi Kimura")) | ||
} | ||
} | ||
|
||
wait(for: [exp]) | ||
|
||
XCTAssertEqual(derived.state.author?.name, "Hiroshi Kimura") | ||
|
||
_ = sub | ||
} | ||
} | ||
|
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.
new