Skip to content

Commit 95734e7

Browse files
committed
Add support for defining an atom scope by passing a store object
1 parent 65b7e1f commit 95734e7

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

Sources/Atoms/AtomScope.swift

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,55 @@ import SwiftUI
3737
/// ```
3838
///
3939
public struct AtomScope<Content: View>: View {
40-
private let context: AtomViewContext?
41-
private var observers = [Observer]()
40+
private let store: StoreContext?
4241
private let content: Content
42+
private var observers = [Observer]()
4343

4444
@Environment(\.store)
45-
private var inheritedStore
45+
private var environmentStore
46+
47+
/// Creates a new scope with the specified content.
48+
///
49+
/// - Parameters:
50+
/// - content: The view content that inheriting from the parent.
51+
public init(@ViewBuilder content: () -> Content) {
52+
self.store = nil
53+
self.content = content()
54+
}
55+
56+
/// Creates a new scope with the specified content that will be allowed to use atoms by
57+
/// passing a view context to explicitly make the descendant views inherit store.
58+
///
59+
/// - Parameters:
60+
/// - context: The parent view context that for inheriting store explicitly.
61+
/// - content: The view content that inheriting from the parent.
62+
public init(
63+
_ context: AtomViewContext,
64+
@ViewBuilder content: () -> Content
65+
) {
66+
self.store = context._store
67+
self.content = content()
68+
}
4669

47-
/// Creates an 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 an internal store.
70+
/// Creates a new scope with the specified content that will be allowed to use atoms by
71+
/// passing a store object.
4972
///
5073
/// - Parameters:
51-
/// - context: The parent view context that for inheriting an internal store explicitly.
52-
/// Default is nil.
74+
/// - store: An object that stores the state of atoms.
5375
/// - content: The view content that inheriting from the parent.
5476
public init(
55-
_ context: AtomViewContext? = nil,
77+
_ store: AtomStore,
5678
@ViewBuilder content: () -> Content
5779
) {
58-
self.context = context
80+
self.store = StoreContext(store)
5981
self.content = content()
6082
}
6183

6284
/// The content and behavior of the view.
6385
public var body: some View {
6486
content.environment(
6587
\.store,
66-
(context?._store ?? inheritedStore).scoped(observers: observers)
88+
(store ?? environmentStore).scoped(observers: observers)
6789
)
6890
}
6991

0 commit comments

Comments
 (0)