Skip to content

Commit a22c7df

Browse files
authored
Remove APIs deprecated in 0.7.x (#192)
1 parent e94744b commit a22c7df

File tree

1 file changed

+20
-89
lines changed

1 file changed

+20
-89
lines changed

Sources/Atoms/AtomScope.swift

Lines changed: 20 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -28,53 +28,39 @@ import SwiftUI
2828
/// ```
2929
///
3030
public struct AtomScope<Content: View>: View {
31-
private let inheritance: Inheritance
31+
private let scopeID: ScopeID
3232
private var observers = [Observer]()
3333
private var overrideContainer = OverrideContainer()
3434
private let content: Content
3535

36+
@Environment(\.store)
37+
private var store
38+
39+
@StateObject
40+
private var state = State()
41+
3642
/// Creates a new scope with the specified content.
3743
///
3844
/// - Parameters:
3945
/// - id: An identifier represents this scope used for matching with scoped atoms.
4046
/// - content: The descendant view content that provides scoped context for atoms.
4147
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)
5949
self.content = content()
6050
}
6151

6252
/// The content and behavior of the view.
6353
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)
7262

73-
case .context(let context):
74-
AtomDerivedScope(context) {
75-
content
76-
}
77-
}
63+
return content.environment(\.store, scopedStore)
7864
}
7965

8066
/// Observes the state changes with a snapshot that captures the whole atom states and
@@ -83,19 +69,11 @@ public struct AtomScope<Content: View>: View {
8369
/// Note that unlike ``AtomRoot/observe(_:)``, this observes only the state changes of atoms
8470
/// initialized in this scope.
8571
///
86-
/// - Note: It ignores the observers if this scope inherits the parent scope.
87-
///
8872
/// - Parameter onUpdate: A closure to handle a snapshot of recent updates.
8973
///
9074
/// - Returns: The self instance.
9175
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)) }
9977
}
10078

10179
/// Override the atoms used in this scope with the given value.
@@ -105,21 +83,13 @@ public struct AtomScope<Content: View>: View {
10583
///
10684
/// This only overrides atoms used in this scope and never be inherited to a nested scopes.
10785
///
108-
/// - Note: It ignores the overrides if this scope inherits the parent scope.
109-
///
11086
/// - Parameters:
11187
/// - atom: An atom to be overridden.
11288
/// - value: A value to be used instead of the atom's value.
11389
///
11490
/// - Returns: The self instance.
11591
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) }
12393
}
12494

12595
/// Override the atoms used in this scope with the given value.
@@ -131,21 +101,13 @@ public struct AtomScope<Content: View>: View {
131101
///
132102
/// This only overrides atoms used in this scope and never be inherited to a nested scopes.
133103
///
134-
/// - Note: It ignores the overrides if this scope inherits the parent scope.
135-
///
136104
/// - Parameters:
137105
/// - atomType: An atom type to be overridden.
138106
/// - value: A value to be used instead of the atom's value.
139107
///
140108
/// - Returns: The self instance.
141109
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) }
149111
}
150112
}
151113

@@ -154,35 +116,4 @@ private extension AtomScope {
154116
final class State: ObservableObject {
155117
let scopeState = ScopeState()
156118
}
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-
}
188119
}

0 commit comments

Comments
 (0)