Skip to content

Commit b52666b

Browse files
authored
Merge pull request #15 from ra1028/refactor/atom-state
refactor: Internal system
2 parents fad9ad0 + 09e2f92 commit b52666b

File tree

70 files changed

+1491
-2031
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1491
-2031
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ Context that can simulate any scenarios in which atoms are used from a view or a
10791079
|API|Use|
10801080
|:--|:--|
10811081
|[unwatch(_:)](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/atomtestcontext/unwatch(_:))|Simulates a scenario in which the atom is no longer watched.|
1082-
|[override(_:with:)](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/atomtestcontext/override(_:with:)-67ivk)|Overwrites the output of a specific atom or all atoms of the given type with the fixed value.|
1082+
|[override(_:with:)](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/atomtestcontext/override(_:with:)-1ce4h)|Overwrites the output of a specific atom or all atoms of the given type with the fixed value.|
10831083
|[observe(_:)](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/atomtestcontext/observe(_:))|Observes changes in any atom values and its lifecycles.|
10841084
|[onUpdate](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/atomtestcontext/onupdate)|Sets a closure that notifies there has been an update to one of the atoms.|
10851085

Sources/Atoms/Atom/AsyncSequenceAtom.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
/// }
4545
/// ```
4646
///
47-
public protocol AsyncSequenceAtom: Atom where Hook == AsyncSequenceHook<Sequence> {
47+
public protocol AsyncSequenceAtom: Atom where State == AsyncSequenceAtomState<Sequence> {
4848
/// The type of asynchronous sequence that this atom manages.
4949
associatedtype Sequence: AsyncSequence
5050

@@ -64,7 +64,7 @@ public protocol AsyncSequenceAtom: Atom where Hook == AsyncSequenceHook<Sequence
6464

6565
public extension AsyncSequenceAtom {
6666
@MainActor
67-
var hook: Hook {
68-
Hook(sequence: sequence)
67+
func makeState() -> State {
68+
State(makeSequence: sequence)
6969
}
7070
}

Sources/Atoms/Atom/Atom.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/// Declares that a type can produce a value that can be accessed from everywhere.
22
///
3-
/// In summary, this protocol declares a hook that determines the behavioral details
4-
/// of this atom and a key determines the value uniqueness.
53
/// The value produced by an atom is created only when the atom is watched from somewhere,
64
/// and is immediately released when no longer watched to.
75
///
@@ -11,8 +9,8 @@ public protocol Atom {
119
/// A type representing the stable identity of this atom.
1210
associatedtype Key: Hashable
1311

14-
/// A type of the hook that determines behavioral details.
15-
associatedtype Hook: AtomHook
12+
/// A type of state that is an actual implementation of this atom.
13+
associatedtype State: AtomState
1614

1715
/// A type of the context structure that to read, watch, and otherwise interacting
1816
/// with other atoms.
@@ -34,9 +32,11 @@ public protocol Atom {
3432
/// If this atom conforms to `Hashable`, it will adopt itself as the `key` by default.
3533
var key: Key { get }
3634

37-
/// Internal use, the hook for managing the state of this atom.
35+
/// Creates a new state that is an actual implementation of this atom.
36+
///
37+
/// - Returns: A state object that handles internal process and a value.
3838
@MainActor
39-
var hook: Hook { get }
39+
func makeState() -> State
4040

4141
/// Returns a boolean value that determines whether it should notify the value update to
4242
/// watchers with comparing the given old value and the new value.
@@ -48,12 +48,12 @@ public protocol Atom {
4848
/// - Returns: A boolean value that determines whether it should notify the value update
4949
/// to watchers.
5050
@MainActor
51-
func shouldNotifyUpdate(newValue: Hook.Value, oldValue: Hook.Value) -> Bool
51+
func shouldNotifyUpdate(newValue: State.Value, oldValue: State.Value) -> Bool
5252
}
5353

5454
public extension Atom {
5555
@MainActor
56-
func shouldNotifyUpdate(newValue: Hook.Value, oldValue: Hook.Value) -> Bool {
56+
func shouldNotifyUpdate(newValue: State.Value, oldValue: State.Value) -> Bool {
5757
true
5858
}
5959
}

Sources/Atoms/Atom/ModifiedAtom.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// An atom type that applies a modifier to an atom.
22
///
33
/// Use ``Atom/modifier(_:)`` instead of using this atom directly.
4-
public struct ModifiedAtom<Node: Atom, Modifier: AtomModifier>: Atom where Node.Hook.Value == Modifier.Value {
4+
public struct ModifiedAtom<Node: Atom, Modifier: AtomModifier>: Atom where Node.State.Value == Modifier.Value {
55
/// A type representing the stable identity of this atom.
66
public struct Key: Hashable {
77
private let atomKey: Node.Key
@@ -29,9 +29,11 @@ public struct ModifiedAtom<Node: Atom, Modifier: AtomModifier>: Atom where Node.
2929
Key(atomKey: atom.key, modifierKey: modifier.key)
3030
}
3131

32-
/// Internal use, the hook for managing the state of this atom.
33-
public var hook: ModifiedHook<Node, Modifier> {
34-
ModifiedHook(atom: atom, modifier: modifier)
32+
/// Creates a new state that is an actual implementation of this atom.
33+
///
34+
/// - Returns: A state object that handles internal process and a value.
35+
public func makeState() -> ModifiedAtomState<Node, Modifier> {
36+
State(atom: atom, modifier: modifier)
3537
}
3638

3739
/// Returns a boolean value that determines whether it should notify the value update to

Sources/Atoms/Atom/ObservableObjectAtom.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import Combine
4848
/// }
4949
/// ```
5050
///
51-
public protocol ObservableObjectAtom: Atom where Hook == ObservableObjectHook<ObjectType> {
51+
public protocol ObservableObjectAtom: Atom where State == ObservableObjectAtomState<ObjectType> {
5252
/// The type of observable object that this atom produces.
5353
associatedtype ObjectType: ObservableObject
5454

@@ -67,7 +67,7 @@ public protocol ObservableObjectAtom: Atom where Hook == ObservableObjectHook<Ob
6767

6868
public extension ObservableObjectAtom {
6969
@MainActor
70-
var hook: Hook {
71-
Hook(object: object)
70+
func makeState() -> State {
71+
State(makeObject: object)
7272
}
7373
}

Sources/Atoms/Atom/PublisherAtom.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import Combine
3737
/// }
3838
/// ```
3939
///
40-
public protocol PublisherAtom: Atom where Hook == PublisherHook<Publisher> {
40+
public protocol PublisherAtom: Atom where State == PublisherAtomState<Publisher> {
4141
/// The type of publisher that this atom manages.
4242
associatedtype Publisher: Combine.Publisher
4343

@@ -57,7 +57,7 @@ public protocol PublisherAtom: Atom where Hook == PublisherHook<Publisher> {
5757

5858
public extension PublisherAtom {
5959
@MainActor
60-
var hook: Hook {
61-
Hook(publisher: publisher)
60+
func makeState() -> State {
61+
State(makePublisher: publisher)
6262
}
6363
}

Sources/Atoms/Atom/StateAtom.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
/// }
3737
/// ```
3838
///
39-
public protocol StateAtom: Atom where Hook == StateHook<Value> {
39+
public protocol StateAtom: Atom where State == StateAtomState<Value> {
4040
/// The type of state value that this atom produces.
4141
associatedtype Value
4242

@@ -77,12 +77,8 @@ public protocol StateAtom: Atom where Hook == StateHook<Value> {
7777

7878
public extension StateAtom {
7979
@MainActor
80-
var hook: Hook {
81-
Hook(
82-
defaultValue: defaultValue,
83-
willSet: willSet,
84-
didSet: didSet
85-
)
80+
func makeState() -> State {
81+
State(getDefaultValue: defaultValue)
8682
}
8783

8884
@MainActor

Sources/Atoms/Atom/TaskAtom.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
/// }
3535
/// ```
3636
///
37-
public protocol TaskAtom: Atom where Hook == TaskHook<Value> {
37+
public protocol TaskAtom: Atom where State == TaskAtomState<Value> {
3838
/// The type of value that this atom produces.
3939
associatedtype Value
4040

@@ -53,7 +53,7 @@ public protocol TaskAtom: Atom where Hook == TaskHook<Value> {
5353

5454
public extension TaskAtom {
5555
@MainActor
56-
var hook: Hook {
57-
Hook(value: value)
56+
func makeState() -> State {
57+
State(getValue: value)
5858
}
5959
}

Sources/Atoms/Atom/ThrowingTaskAtom.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
/// }
3737
/// ```
3838
///
39-
public protocol ThrowingTaskAtom: Atom where Hook == ThrowingTaskHook<Value> {
39+
public protocol ThrowingTaskAtom: Atom where State == ThrowingTaskState<Value> {
4040
/// The type of value that this atom produces.
4141
associatedtype Value
4242

@@ -57,7 +57,7 @@ public protocol ThrowingTaskAtom: Atom where Hook == ThrowingTaskHook<Value> {
5757

5858
public extension ThrowingTaskAtom {
5959
@MainActor
60-
var hook: Hook {
61-
Hook(value: value)
60+
func makeState() -> State {
61+
State(getValue: value)
6262
}
6363
}

Sources/Atoms/Atom/ValueAtom.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
/// }
3030
/// ```
3131
///
32-
public protocol ValueAtom: Atom where Hook == ValueHook<Value> {
32+
public protocol ValueAtom: Atom where State == ValueAtomState<Value> {
3333
/// The type of value that this atom produces.
3434
associatedtype Value
3535

@@ -48,7 +48,7 @@ public protocol ValueAtom: Atom where Hook == ValueHook<Value> {
4848

4949
public extension ValueAtom {
5050
@MainActor
51-
var hook: Hook {
52-
Hook(value: value)
51+
func makeState() -> State {
52+
State(getValue: value)
5353
}
5454
}

0 commit comments

Comments
 (0)