|
1 | 1 | public extension Atom { |
2 | | - /// Provides the latest value that matches the specified condition instead of the current value. |
3 | | - /// |
4 | | - /// ```swift |
5 | | - /// struct Item { |
6 | | - /// let id: Int |
7 | | - /// let isValid: Bool |
8 | | - /// } |
9 | | - /// |
10 | | - /// struct ItemAtom: StateAtom, Hashable { |
11 | | - /// func defaultValue(context: Context) -> Item { |
12 | | - /// Item(id: 0, isValid: false) |
13 | | - /// } |
14 | | - /// } |
15 | | - /// |
16 | | - /// struct ExampleView: View { |
17 | | - /// @Watch(ItemAtom()) |
18 | | - /// var currentItem |
19 | | - /// |
20 | | - /// @Watch(ItemAtom().latest(\.isValid)) |
21 | | - /// var latestValidItem |
22 | | - /// |
23 | | - /// var body: some View { |
24 | | - /// VStack { |
25 | | - /// Text("Current ID: \(currentItem.id)") |
26 | | - /// Text("Latest Valid ID: \(latestValidItem?.id ?? 0)") |
27 | | - /// } |
28 | | - /// } |
29 | | - /// } |
30 | | - /// ``` |
31 | | - /// |
32 | 2 | #if hasFeature(InferSendableFromCaptures) |
| 3 | + /// Provides the latest value that matches the specified condition instead of the current value. |
| 4 | + /// |
| 5 | + /// ## Example |
| 6 | + /// |
| 7 | + /// ```swift |
| 8 | + /// struct Item { |
| 9 | + /// let id: Int |
| 10 | + /// let isValid: Bool |
| 11 | + /// } |
| 12 | + /// |
| 13 | + /// struct ItemAtom: StateAtom, Hashable { |
| 14 | + /// func defaultValue(context: Context) -> Item { |
| 15 | + /// Item(id: 0, isValid: false) |
| 16 | + /// } |
| 17 | + /// } |
| 18 | + /// |
| 19 | + /// struct ExampleView: View { |
| 20 | + /// @Watch(ItemAtom()) |
| 21 | + /// var currentItem |
| 22 | + /// |
| 23 | + /// @Watch(ItemAtom().latest(\.isValid)) |
| 24 | + /// var latestValidItem |
| 25 | + /// |
| 26 | + /// var body: some View { |
| 27 | + /// VStack { |
| 28 | + /// Text("Current ID: \(currentItem.id)") |
| 29 | + /// Text("Latest Valid ID: \(latestValidItem?.id ?? 0)") |
| 30 | + /// } |
| 31 | + /// } |
| 32 | + /// } |
| 33 | + /// ``` |
| 34 | + /// |
| 35 | + /// - Parameter keyPath: A key path to a `Bool` property of the atom value that determines whether the value should be retained as the latest. |
| 36 | + /// |
| 37 | + /// - Returns: An atom that provides the latest value that matches the specified condition, or `nil` if no value has matched yet. |
33 | 38 | func latest(_ keyPath: any KeyPath<Produced, Bool> & Sendable) -> ModifiedAtom<Self, LatestModifier<Produced>> { |
34 | 39 | modifier(LatestModifier(keyPath: keyPath)) |
35 | 40 | } |
36 | 41 | #else |
| 42 | + /// Provides the latest value that matches the specified condition instead of the current value. |
| 43 | + /// |
| 44 | + /// ## Example |
| 45 | + /// |
| 46 | + /// ```swift |
| 47 | + /// struct Item { |
| 48 | + /// let id: Int |
| 49 | + /// let isValid: Bool |
| 50 | + /// } |
| 51 | + /// |
| 52 | + /// struct ItemAtom: StateAtom, Hashable { |
| 53 | + /// func defaultValue(context: Context) -> Item { |
| 54 | + /// Item(id: 0, isValid: false) |
| 55 | + /// } |
| 56 | + /// } |
| 57 | + /// |
| 58 | + /// struct ExampleView: View { |
| 59 | + /// @Watch(ItemAtom()) |
| 60 | + /// var currentItem |
| 61 | + /// |
| 62 | + /// @Watch(ItemAtom().latest(\.isValid)) |
| 63 | + /// var latestValidItem |
| 64 | + /// |
| 65 | + /// var body: some View { |
| 66 | + /// VStack { |
| 67 | + /// Text("Current ID: \(currentItem.id)") |
| 68 | + /// Text("Latest Valid ID: \(latestValidItem?.id ?? 0)") |
| 69 | + /// } |
| 70 | + /// } |
| 71 | + /// } |
| 72 | + /// ``` |
| 73 | + /// |
| 74 | + /// - Parameter keyPath: A key path to a `Bool` property of the atom value that determines whether the value should be retained as the latest. |
| 75 | + /// |
| 76 | + /// - Returns: An atom that provides the latest value that matches the specified condition, or `nil` if no value has matched yet. |
37 | 77 | func latest(_ keyPath: KeyPath<Produced, Bool>) -> ModifiedAtom<Self, LatestModifier<Produced>> { |
38 | 78 | modifier(LatestModifier(keyPath: keyPath)) |
39 | 79 | } |
|
0 commit comments