@@ -28,53 +28,39 @@ import SwiftUI
28
28
/// ```
29
29
///
30
30
public struct AtomScope < Content: View > : View {
31
- private let inheritance : Inheritance
31
+ private let scopeID : ScopeID
32
32
private var observers = [ Observer] ( )
33
33
private var overrideContainer = OverrideContainer ( )
34
34
private let content : Content
35
35
36
+ @Environment ( \. store)
37
+ private var store
38
+
39
+ @StateObject
40
+ private var state = State ( )
41
+
36
42
/// Creates a new scope with the specified content.
37
43
///
38
44
/// - Parameters:
39
45
/// - id: An identifier represents this scope used for matching with scoped atoms.
40
46
/// - content: The descendant view content that provides scoped context for atoms.
41
47
public init < ID: Hashable > ( id: ID = DefaultScopeID ( ) , @ViewBuilder content: ( ) -> Content ) {
42
- let scopeID = ScopeID ( id)
43
- self . inheritance = . environment( scopeID: scopeID)
44
- self . content = content ( )
45
- }
46
-
47
- /// Creates a new scope with the specified content that will be allowed to use atoms by
48
- /// passing a view context to explicitly make the descendant views inherit store.
49
- ///
50
- /// - Parameters:
51
- /// - context: The parent view context that for inheriting store explicitly.
52
- /// - content: The descendant view content that provides scoped context for atoms.
53
- @available ( * , deprecated, message: " Use `AtomDerivedScope` instead " )
54
- public init (
55
- inheriting context: AtomViewContext ,
56
- @ViewBuilder content: ( ) -> Content
57
- ) {
58
- self . inheritance = . context( context)
48
+ self . scopeID = ScopeID ( id)
59
49
self . content = content ( )
60
50
}
61
51
62
52
/// The content and behavior of the view.
63
53
public var body : some View {
64
- switch inheritance {
65
- case . environment ( let scopeID) :
66
- WithEnvironment (
67
- scopeID : scopeID ,
68
- observers : observers ,
69
- overrideContainer : overrideContainer ,
70
- content : content
71
- )
54
+ let scopedStore = store ? . scoped (
55
+ scopeID: scopeID ,
56
+ scopeKey : state . scopeState . token . key ,
57
+ observers : observers ,
58
+ overrideContainer : overrideContainer
59
+ )
60
+
61
+ scopedStore ? . registerScope ( state : state . scopeState )
72
62
73
- case . context( let context) :
74
- AtomDerivedScope ( context) {
75
- content
76
- }
77
- }
63
+ return content. environment ( \. store, scopedStore)
78
64
}
79
65
80
66
/// Observes the state changes with a snapshot that captures the whole atom states and
@@ -83,19 +69,11 @@ public struct AtomScope<Content: View>: View {
83
69
/// Note that unlike ``AtomRoot/observe(_:)``, this observes only the state changes of atoms
84
70
/// initialized in this scope.
85
71
///
86
- /// - Note: It ignores the observers if this scope inherits the parent scope.
87
- ///
88
72
/// - Parameter onUpdate: A closure to handle a snapshot of recent updates.
89
73
///
90
74
/// - Returns: The self instance.
91
75
public func scopedObserve( _ onUpdate: @MainActor @escaping ( Snapshot ) -> Void ) -> Self {
92
- if case . context = inheritance {
93
- assertionFailure (
94
- " [Atoms] AtomScope now ignores the given scoped observers if it's inheriting an ancestor scope. This will be deprecated soon. "
95
- )
96
- return self
97
- }
98
- return mutating ( self ) { $0. observers. append ( Observer ( onUpdate: onUpdate) ) }
76
+ mutating ( self ) { $0. observers. append ( Observer ( onUpdate: onUpdate) ) }
99
77
}
100
78
101
79
/// Override the atoms used in this scope with the given value.
@@ -105,21 +83,13 @@ public struct AtomScope<Content: View>: View {
105
83
///
106
84
/// This only overrides atoms used in this scope and never be inherited to a nested scopes.
107
85
///
108
- /// - Note: It ignores the overrides if this scope inherits the parent scope.
109
- ///
110
86
/// - Parameters:
111
87
/// - atom: An atom to be overridden.
112
88
/// - value: A value to be used instead of the atom's value.
113
89
///
114
90
/// - Returns: The self instance.
115
91
public func scopedOverride< Node: Atom > ( _ atom: Node , with value: @MainActor @escaping ( Node ) -> Node . Produced ) -> Self {
116
- if case . context = inheritance {
117
- assertionFailure (
118
- " [Atoms] AtomScope now ignores the given scoped overrides if it's inheriting an ancestor scope. This will be deprecated soon. "
119
- )
120
- return self
121
- }
122
- return mutating ( self ) { $0. overrideContainer. addOverride ( for: atom, with: value) }
92
+ mutating ( self ) { $0. overrideContainer. addOverride ( for: atom, with: value) }
123
93
}
124
94
125
95
/// Override the atoms used in this scope with the given value.
@@ -131,21 +101,13 @@ public struct AtomScope<Content: View>: View {
131
101
///
132
102
/// This only overrides atoms used in this scope and never be inherited to a nested scopes.
133
103
///
134
- /// - Note: It ignores the overrides if this scope inherits the parent scope.
135
- ///
136
104
/// - Parameters:
137
105
/// - atomType: An atom type to be overridden.
138
106
/// - value: A value to be used instead of the atom's value.
139
107
///
140
108
/// - Returns: The self instance.
141
109
public func scopedOverride< Node: Atom > ( _ atomType: Node . Type , with value: @MainActor @escaping ( Node ) -> Node . Produced ) -> Self {
142
- if case . context = inheritance {
143
- assertionFailure (
144
- " [Atoms] AtomScope now ignores the given scoped overrides if it's inheriting an ancestor scope. This will be deprecated soon. "
145
- )
146
- return self
147
- }
148
- return mutating ( self ) { $0. overrideContainer. addOverride ( for: atomType, with: value) }
110
+ mutating ( self ) { $0. overrideContainer. addOverride ( for: atomType, with: value) }
149
111
}
150
112
}
151
113
@@ -154,35 +116,4 @@ private extension AtomScope {
154
116
final class State : ObservableObject {
155
117
let scopeState = ScopeState ( )
156
118
}
157
-
158
- enum Inheritance {
159
- case environment( scopeID: ScopeID )
160
- case context( AtomViewContext )
161
- }
162
-
163
- struct WithEnvironment : View {
164
- let scopeID : ScopeID
165
- let observers : [ Observer ]
166
- let overrideContainer : OverrideContainer
167
- let content : Content
168
-
169
- @Environment ( \. store)
170
- private var store
171
-
172
- @StateObject
173
- private var state = State ( )
174
-
175
- var body : some View {
176
- let scopedStore = store? . scoped (
177
- scopeID: scopeID,
178
- scopeKey: state. scopeState. token. key,
179
- observers: observers,
180
- overrideContainer: overrideContainer
181
- )
182
-
183
- scopedStore? . registerScope ( state: state. scopeState)
184
-
185
- return content. environment ( \. store, scopedStore)
186
- }
187
- }
188
119
}
0 commit comments