-
-
Notifications
You must be signed in to change notification settings - Fork 17
Latest modifier #199
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
Merged
Merged
Latest modifier #199
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 hidden or 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 hidden or 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 hidden or 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,125 @@ | ||
public extension Atom { | ||
/// Provides the latest value that matches the specified condition instead of the current value. | ||
/// | ||
/// ```swift | ||
/// struct Item { | ||
/// let id: Int | ||
/// let isValid: Bool | ||
/// } | ||
/// | ||
/// struct ItemAtom: StateAtom, Hashable { | ||
/// func defaultValue(context: Context) -> Item { | ||
/// Item(id: 0, isValid: false) | ||
/// } | ||
/// } | ||
/// | ||
/// struct ExampleView: View { | ||
/// @Watch(ItemAtom()) | ||
/// var currentItem | ||
/// | ||
/// @Watch(ItemAtom().latest(\.isValid)) | ||
/// var latestValidItem | ||
/// | ||
/// var body: some View { | ||
/// VStack { | ||
/// Text("Current ID: \(currentItem.id)") | ||
/// Text("Latest Valid ID: \(latestValidItem?.id ?? 0)") | ||
/// } | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
#if hasFeature(InferSendableFromCaptures) | ||
func latest(_ keyPath: any KeyPath<Produced, Bool> & Sendable) -> ModifiedAtom<Self, LatestModifier<Produced>> { | ||
modifier(LatestModifier(keyPath: keyPath)) | ||
} | ||
#else | ||
func latest(_ keyPath: KeyPath<Produced, Bool>) -> ModifiedAtom<Self, LatestModifier<Produced>> { | ||
modifier(LatestModifier(keyPath: keyPath)) | ||
} | ||
#endif | ||
} | ||
|
||
/// A modifier that provides the latest value that matches the specified condition instead of the current value. | ||
/// | ||
/// Use ``Atom/latest(_:)`` instead of using this modifier directly. | ||
public struct LatestModifier<Base>: AtomModifier { | ||
/// A type of base value to be modified. | ||
public typealias Base = Base | ||
|
||
/// A type of value the modified atom produces. | ||
public typealias Produced = Base? | ||
|
||
#if hasFeature(InferSendableFromCaptures) | ||
/// A type representing the stable identity of this modifier. | ||
public struct Key: Hashable, Sendable { | ||
private let keyPath: any KeyPath<Base, Bool> & Sendable | ||
|
||
fileprivate init(keyPath: any KeyPath<Base, Bool> & Sendable) { | ||
self.keyPath = keyPath | ||
} | ||
} | ||
|
||
private let keyPath: any KeyPath<Base, Bool> & Sendable | ||
|
||
internal init(keyPath: any KeyPath<Base, Bool> & Sendable) { | ||
self.keyPath = keyPath | ||
} | ||
|
||
/// A unique value used to identify the modifier internally. | ||
public var key: Key { | ||
Key(keyPath: keyPath) | ||
} | ||
#else | ||
public struct Key: Hashable, Sendable { | ||
private let keyPath: UnsafeUncheckedSendable<KeyPath<Base, Bool>> | ||
|
||
fileprivate init(keyPath: UnsafeUncheckedSendable<KeyPath<Base, Bool>>) { | ||
self.keyPath = keyPath | ||
} | ||
} | ||
|
||
private let _keyPath: UnsafeUncheckedSendable<KeyPath<Base, Bool>> | ||
private var keyPath: KeyPath<Base, Bool> { | ||
_keyPath.value | ||
} | ||
|
||
internal init(keyPath: KeyPath<Base, Bool>) { | ||
_keyPath = UnsafeUncheckedSendable(keyPath) | ||
} | ||
|
||
/// A unique value used to identify the modifier internally. | ||
public var key: Key { | ||
Key(keyPath: _keyPath) | ||
} | ||
#endif | ||
|
||
/// A producer that produces the value of this atom. | ||
public func producer(atom: some Atom<Base>) -> AtomProducer<Produced> { | ||
AtomProducer { context in | ||
context.transaction { context in | ||
let value = context.watch(atom) | ||
let storage = context.watch(StorageAtom()) | ||
|
||
if value[keyPath: keyPath] { | ||
storage.latest = value | ||
} | ||
|
||
return storage.latest | ||
} | ||
} | ||
} | ||
} | ||
|
||
private extension LatestModifier { | ||
@MainActor | ||
final class Storage { | ||
var latest: Base? | ||
} | ||
|
||
struct StorageAtom: ValueAtom, Hashable { | ||
func value(context: Context) -> Storage { | ||
Storage() | ||
} | ||
} | ||
} |
This file contains hidden or 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,140 @@ | ||
import XCTest | ||
|
||
@testable import Atoms | ||
|
||
final class LatestModifierTests: XCTestCase { | ||
struct Item { | ||
let id: Int | ||
let isValid: Bool | ||
} | ||
|
||
@MainActor | ||
func testLatest() { | ||
let atom = TestStateAtom(defaultValue: Item(id: 1, isValid: false)) | ||
let context = AtomTestContext() | ||
|
||
// Initially nil because isValid is false | ||
XCTAssertNil(context.watch(atom.latest(\.isValid))) | ||
|
||
// Update with valid item | ||
context[atom] = Item(id: 2, isValid: true) | ||
|
||
// Should return the valid item | ||
XCTAssertEqual(context.watch(atom.latest(\.isValid))?.id, 2) | ||
|
||
// Update with invalid item | ||
context[atom] = Item(id: 3, isValid: false) | ||
|
||
// Should still return the last valid item | ||
XCTAssertEqual(context.watch(atom.latest(\.isValid))?.id, 2) | ||
|
||
// Update with another valid item | ||
context[atom] = Item(id: 4, isValid: true) | ||
|
||
// Should return the new valid item | ||
XCTAssertEqual(context.watch(atom.latest(\.isValid))?.id, 4) | ||
|
||
// Update with invalid item again | ||
context[atom] = Item(id: 5, isValid: false) | ||
|
||
// Should still return the last valid item | ||
XCTAssertEqual(context.watch(atom.latest(\.isValid))?.id, 4) | ||
} | ||
|
||
@MainActor | ||
func testLatestWithMultipleWatchers() { | ||
let atom = TestStateAtom(defaultValue: Item(id: 1, isValid: false)) | ||
let context = AtomTestContext() | ||
|
||
// Watch both current and latest | ||
XCTAssertEqual(context.watch(atom).id, 1) | ||
XCTAssertNil(context.watch(atom.latest(\.isValid))) | ||
|
||
// Update with valid item | ||
context[atom] = Item(id: 2, isValid: true) | ||
|
||
XCTAssertEqual(context.watch(atom).id, 2) | ||
XCTAssertEqual(context.watch(atom.latest(\.isValid))?.id, 2) | ||
|
||
// Update with invalid item | ||
context[atom] = Item(id: 3, isValid: false) | ||
|
||
XCTAssertEqual(context.watch(atom).id, 3) | ||
XCTAssertEqual(context.watch(atom.latest(\.isValid))?.id, 2) | ||
} | ||
|
||
@MainActor | ||
func testLatestUpdatesDownstream() { | ||
let atom = TestStateAtom(defaultValue: Item(id: 1, isValid: false)) | ||
let context = AtomTestContext() | ||
var updatedCount = 0 | ||
|
||
context.onUpdate = { | ||
updatedCount += 1 | ||
} | ||
|
||
// Initial watch | ||
XCTAssertEqual(updatedCount, 0) | ||
XCTAssertNil(context.watch(atom.latest(\.isValid))) | ||
|
||
// Update with valid item - should trigger update | ||
context[atom] = Item(id: 2, isValid: true) | ||
XCTAssertEqual(updatedCount, 1) | ||
XCTAssertEqual(context.watch(atom.latest(\.isValid))?.id, 2) | ||
|
||
// Update with invalid item - should still trigger update | ||
context[atom] = Item(id: 3, isValid: false) | ||
XCTAssertEqual(updatedCount, 2) | ||
XCTAssertEqual(context.watch(atom.latest(\.isValid))?.id, 2) | ||
|
||
// Update with another valid item - should trigger update | ||
context[atom] = Item(id: 4, isValid: true) | ||
XCTAssertEqual(updatedCount, 3) | ||
XCTAssertEqual(context.watch(atom.latest(\.isValid))?.id, 4) | ||
} | ||
|
||
@MainActor | ||
func testKey() { | ||
let modifier1 = LatestModifier<Item>(keyPath: \.isValid) | ||
let modifier2 = LatestModifier<Item>(keyPath: \.isValid) | ||
|
||
XCTAssertEqual(modifier1.key, modifier2.key) | ||
XCTAssertEqual(modifier1.key.hashValue, modifier2.key.hashValue) | ||
} | ||
|
||
@MainActor | ||
func testLatestWithBoolValue() { | ||
let atom = TestStateAtom(defaultValue: true) | ||
let context = AtomTestContext() | ||
|
||
// Initially should return the value if it's true | ||
XCTAssertEqual(context.watch(atom.latest(\.self)), true) | ||
|
||
// Update to false | ||
context[atom] = false | ||
|
||
// Should still return the last true value | ||
XCTAssertEqual(context.watch(atom.latest(\.self)), true) | ||
|
||
// Update to true again | ||
context[atom] = true | ||
|
||
// Should return the new true value | ||
XCTAssertEqual(context.watch(atom.latest(\.self)), true) | ||
} | ||
|
||
@MainActor | ||
func testLatestWithInitialValidValue() { | ||
let atom = TestStateAtom(defaultValue: Item(id: 1, isValid: true)) | ||
let context = AtomTestContext() | ||
|
||
// Should immediately return the initial valid value | ||
XCTAssertEqual(context.watch(atom.latest(\.isValid))?.id, 1) | ||
|
||
// Update with invalid item | ||
context[atom] = Item(id: 2, isValid: false) | ||
|
||
// Should still return the initial valid value | ||
XCTAssertEqual(context.watch(atom.latest(\.isValid))?.id, 1) | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.