@@ -22,7 +22,7 @@ import ViewEnvironment
22
22
/// environment as it flows between two nodes.
23
23
///
24
24
@_spi ( ViewEnvironmentWiring)
25
- public struct ViewEnvironmentPropagationNode : ViewEnvironmentCustomizing {
25
+ public class ViewEnvironmentPropagationNode : ViewEnvironmentCustomizing , ViewEnvironmentObserving {
26
26
public typealias EnvironmentAncestorProvider = ( ) -> ViewEnvironmentPropagating ?
27
27
28
28
public typealias EnvironmentDescendantsProvider = ( ) -> [ ViewEnvironmentPropagating ]
@@ -39,14 +39,28 @@ public struct ViewEnvironmentPropagationNode: ViewEnvironmentCustomizing {
39
39
didSet { setNeedsEnvironmentUpdate ( ) }
40
40
}
41
41
42
+ public var environmentDidChangeObserver : ( ( ViewEnvironment ) -> Void ) ? {
43
+ didSet { setNeedsEnvironmentUpdate ( ) }
44
+ }
45
+
46
+ public var applyEnvironment : ( ViewEnvironment ) -> Void {
47
+ didSet { setNeedsEnvironmentUpdate ( ) }
48
+ }
49
+
50
+ private var needsEnvironmentUpdate : Bool = true
51
+
42
52
public init (
43
53
environmentAncestor: @escaping EnvironmentAncestorProvider = { nil } ,
44
54
environmentDescendants: @escaping EnvironmentDescendantsProvider = { [ ] } ,
45
- customizeEnvironment: @escaping ( inout ViewEnvironment ) -> Void = { _ in }
55
+ customizeEnvironment: @escaping ( inout ViewEnvironment ) -> Void = { _ in } ,
56
+ environmentDidChange: ( ( ViewEnvironment ) -> Void ) ? = nil ,
57
+ applyEnvironment: @escaping ( ViewEnvironment ) -> Void = { _ in }
46
58
) {
47
59
self . environmentAncestorProvider = environmentAncestor
48
60
self . environmentDescendantsProvider = environmentDescendants
49
61
self . customizeEnvironment = customizeEnvironment
62
+ self . environmentDidChangeObserver = environmentDidChange
63
+ self . applyEnvironment = applyEnvironment
50
64
}
51
65
52
66
public var environmentAncestor : ViewEnvironmentPropagating ? { environmentAncestorProvider ( ) }
@@ -58,6 +72,26 @@ public struct ViewEnvironmentPropagationNode: ViewEnvironmentCustomizing {
58
72
}
59
73
60
74
public func setNeedsEnvironmentUpdate( ) {
75
+ needsEnvironmentUpdate = true
76
+
61
77
setNeedsEnvironmentUpdateOnAppropriateDescendants ( )
62
78
}
79
+
80
+ public func environmentDidChange( ) {
81
+ guard let didChange = environmentDidChangeObserver else { return }
82
+
83
+ didChange ( environment)
84
+ }
85
+
86
+ public func applyEnvironmentIfNeeded( ) {
87
+ guard needsEnvironmentUpdate else { return }
88
+
89
+ needsEnvironmentUpdate = false
90
+
91
+ apply ( environment: environment)
92
+ }
93
+
94
+ public func apply( environment: ViewEnvironment ) {
95
+ applyEnvironment ( environment)
96
+ }
63
97
}
0 commit comments