Skip to content

Commit 1cf1e81

Browse files
authored
Rename select modifier to changes(of:) (#102)
* Rename select modifier to changes(of:) * Add deprecated select modifier
1 parent 019a3cc commit 1cf1e81

File tree

8 files changed

+39
-34
lines changed

8 files changed

+39
-34
lines changed

Examples/Packages/iOS/Sources/ExampleMap/Atoms.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@ struct CoordinateAtom: ValueAtom, Hashable {
5858

5959
struct AuthorizationStatusAtom: ValueAtom, Hashable {
6060
func value(context: Context) -> CLAuthorizationStatus {
61-
context.watch(LocationObserverAtom().select(\.manager.authorizationStatus))
61+
context.watch(LocationObserverAtom().changes(of: \.manager.authorizationStatus))
6262
}
6363
}

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -634,13 +634,13 @@ struct ContactView: View {
634634

635635
Modifiers can be applied to an atom to produce a different versions of the original atom to make it more coding friendly or to reduce view re-computation for performance optimization.
636636

637-
#### [select](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/atom/select(_:))
637+
#### [changes(of:)](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/atom/changes(of:))
638638

639639
| |Description|
640640
|:--------------|:----------|
641-
|Summary |Selects a partial property with the specified key path from the original atom. The selected property doesn't notify updates if the new value is equivalent to the old value.|
641+
|Summary |Derives a partial property with the specified key path from the original atom and prevent it from updating its downstream when its new value is equivalent to old value.|
642642
|Output |`T: Equatable`|
643-
|Compatible |All atoms types. The selected property must be `Equatable` compliant.|
643+
|Compatible |All atoms types. The derived property must be `Equatable` compliant.|
644644
|Use Case |Performance optimization, Property scope restriction|
645645

646646
<details><summary><code>📖 Expand to see example</code></summary>
@@ -653,7 +653,7 @@ struct CountAtom: StateAtom, Hashable {
653653
}
654654

655655
struct CountDisplayView: View {
656-
@Watch(CountAtom().select(\.description))
656+
@Watch(CountAtom().changes(of: \.description))
657657
var description // : String
658658

659659
var body: some View {

Sources/Atoms/Atoms.docc/Atoms.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ Building state by compositing atoms automatically optimizes rendering based on i
2727

2828
### Modifiers
2929

30-
- ``Atom/select(_:)``
3130
- ``Atom/changes``
31+
- ``Atom/changes(of:)``
3232
- ``Atom/phase``
3333

3434
### Attributes
@@ -70,8 +70,8 @@ Building state by compositing atoms automatically optimizes rendering based on i
7070
- ``Atom``
7171
- ``AtomStore``
7272
- ``AtomModifier``
73-
- ``SelectModifier``
7473
- ``ChangesModifier``
74+
- ``ChangesOfModifier``
7575
- ``TaskPhaseModifier``
7676
- ``AtomLoader``
7777
- ``RefreshableAtomLoader``

Sources/Atoms/Deprecated.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public extension Atom {
2+
@available(*, deprecated, renamed: "changes(of:)")
3+
func select<Selected: Equatable>(
4+
_ keyPath: KeyPath<Loader.Value, Selected>
5+
) -> ModifiedAtom<Self, ChangesOfModifier<Loader.Value, Selected>> {
6+
changes(of: keyPath)
7+
}
8+
}

Sources/Atoms/Modifier/ChangesModifier.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
public extension Atom where Loader.Value: Equatable {
2-
/// Prevents the atom from updating its child views or atoms when its new value is the
3-
/// same as its old value.
2+
/// Prevents the atom from updating its downstream when its new value is equivalent to old value.
43
///
54
/// ```swift
65
/// struct FlagAtom: StateAtom, Hashable {

Sources/Atoms/Modifier/SelectModifier.swift renamed to Sources/Atoms/Modifier/ChangesOfModifier.swift

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
public extension Atom {
2-
/// Selects a partial property with the specified key path from the original atom.
3-
///
4-
/// When this modifier is used, the atom provides the partial value which conforms to `Equatable`
5-
/// and prevent the view from updating its child view if the new value is equivalent to old value.
2+
/// Derives a partial property with the specified key path from the original atom and prevent it
3+
/// from updating its downstream when its new value is equivalent to old value.
64
///
75
/// ```swift
86
/// struct IntAtom: ValueAtom, Hashable {
@@ -12,7 +10,7 @@ public extension Atom {
1210
/// }
1311
///
1412
/// struct ExampleView: View {
15-
/// @Watch(IntAtom().select(\.description))
13+
/// @Watch(IntAtom().changes(of: \.description))
1614
/// var description
1715
///
1816
/// var body: some View {
@@ -24,20 +22,20 @@ public extension Atom {
2422
/// - Parameter keyPath: A key path for the property of the original atom value.
2523
///
2624
/// - Returns: An atom that provides the partial property of the original atom value.
27-
func select<Selected: Equatable>(
28-
_ keyPath: KeyPath<Loader.Value, Selected>
29-
) -> ModifiedAtom<Self, SelectModifier<Loader.Value, Selected>> {
30-
modifier(SelectModifier(keyPath: keyPath))
25+
func changes<T: Equatable>(
26+
of keyPath: KeyPath<Loader.Value, T>
27+
) -> ModifiedAtom<Self, ChangesOfModifier<Loader.Value, T>> {
28+
modifier(ChangesOfModifier(keyPath: keyPath))
3129
}
3230
}
3331

34-
/// A modifier that selects the partial value of the specified key path
35-
/// from the original atom.
32+
/// A modifier that derives a partial property with the specified key path from the original atom
33+
/// and prevent it from updating its downstream when its new value is equivalent to old value.
3634
///
37-
/// Use ``Atom/select(_:)`` instead of using this modifier directly.
38-
public struct SelectModifier<BaseValue, Selected: Equatable>: AtomModifier {
35+
/// Use ``Atom/changes(of:)`` instead of using this modifier directly.
36+
public struct ChangesOfModifier<BaseValue, T: Equatable>: AtomModifier {
3937
/// A type of modified value to provide.
40-
public typealias Value = Selected
38+
public typealias Value = T
4139

4240
/// A type representing the stable identity of this modifier.
4341
public struct Key: Hashable {

Tests/AtomsTests/Atom/ModifiedAtomTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ final class ModifiedAtomTests: XCTestCase {
66
@MainActor
77
func testKey() {
88
let base = TestAtom(value: 0)
9-
let modifier = SelectModifier<Int, String>(keyPath: \.description)
9+
let modifier = ChangesOfModifier<Int, String>(keyPath: \.description)
1010
let atom = ModifiedAtom(atom: base, modifier: modifier)
1111

1212
XCTAssertEqual(atom.key, atom.key)
@@ -20,7 +20,7 @@ final class ModifiedAtomTests: XCTestCase {
2020
@MainActor
2121
func testValue() async {
2222
let base = TestStateAtom(defaultValue: "test")
23-
let modifier = SelectModifier<String, Int>(keyPath: \.count)
23+
let modifier = ChangesOfModifier<String, Int>(keyPath: \.count)
2424
let atom = ModifiedAtom(atom: base, modifier: modifier)
2525
let context = AtomTestContext()
2626

Tests/AtomsTests/Modifier/SelectModifierTests.swift renamed to Tests/AtomsTests/Modifier/ChangesOfModifierTests.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import XCTest
22

33
@testable import Atoms
44

5-
final class SelectModifierTests: XCTestCase {
5+
final class ChangesOfModifierTests: XCTestCase {
66
@MainActor
7-
func testSelect() {
7+
func testChangesOf() {
88
let atom = TestStateAtom(defaultValue: "")
99
let context = AtomTestContext()
1010
var updatedCount = 0
@@ -14,21 +14,21 @@ final class SelectModifierTests: XCTestCase {
1414
}
1515

1616
XCTAssertEqual(updatedCount, 0)
17-
XCTAssertEqual(context.watch(atom.select(\.count)), 0)
17+
XCTAssertEqual(context.watch(atom.changes(of: \.count)), 0)
1818

1919
context[atom] = "modified"
2020

2121
XCTAssertEqual(updatedCount, 1)
22-
XCTAssertEqual(context.watch(atom.select(\.count)), 8)
22+
XCTAssertEqual(context.watch(atom.changes(of: \.count)), 8)
2323
context[atom] = "modified"
2424

2525
// Should not be updated with an equivalent value.
2626
XCTAssertEqual(updatedCount, 1)
2727
}
2828

2929
func testKey() {
30-
let modifier0 = SelectModifier<Int, Int>(keyPath: \.byteSwapped)
31-
let modifier1 = SelectModifier<Int, Int>(keyPath: \.leadingZeroBitCount)
30+
let modifier0 = ChangesOfModifier<Int, Int>(keyPath: \.byteSwapped)
31+
let modifier1 = ChangesOfModifier<Int, Int>(keyPath: \.leadingZeroBitCount)
3232

3333
XCTAssertEqual(modifier0.key, modifier0.key)
3434
XCTAssertEqual(modifier0.key.hashValue, modifier0.key.hashValue)
@@ -38,7 +38,7 @@ final class SelectModifierTests: XCTestCase {
3838

3939
@MainActor
4040
func testShouldUpdate() {
41-
let modifier = SelectModifier<String, Int>(keyPath: \.count)
41+
let modifier = ChangesOfModifier<String, Int>(keyPath: \.count)
4242

4343
XCTAssertFalse(modifier.shouldUpdate(newValue: 100, oldValue: 100))
4444
XCTAssertTrue(modifier.shouldUpdate(newValue: 100, oldValue: 200))
@@ -47,7 +47,7 @@ final class SelectModifierTests: XCTestCase {
4747
@MainActor
4848
func testModify() {
4949
let atom = TestValueAtom(value: 0)
50-
let modifier = SelectModifier<Int, String>(keyPath: \.description)
50+
let modifier = ChangesOfModifier<Int, String>(keyPath: \.description)
5151
let transaction = Transaction(key: AtomKey(atom)) {}
5252
let context = AtomModifierContext<String>(transaction: transaction) { _ in }
5353
let value = modifier.modify(value: 100, context: context)
@@ -58,7 +58,7 @@ final class SelectModifierTests: XCTestCase {
5858
@MainActor
5959
func testAssociateOverridden() {
6060
let atom = TestValueAtom(value: 0)
61-
let modifier = SelectModifier<Int, String>(keyPath: \.description)
61+
let modifier = ChangesOfModifier<Int, String>(keyPath: \.description)
6262
let transaction = Transaction(key: AtomKey(atom)) {}
6363
let context = AtomModifierContext<String>(transaction: transaction) { _ in }
6464
let value = modifier.associateOverridden(value: "100", context: context)

0 commit comments

Comments
 (0)