Skip to content

Commit

Permalink
Update _EnvironmentKeyTransformModifier implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Mar 10, 2024
1 parent fe65fe0 commit 924d8be
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"location" : "https://github.com/OpenSwiftUIProject/OpenGraph",
"state" : {
"branch" : "main",
"revision" : "8baf226a2dd579d70febed94aa5bb23a04e1e56e"
"revision" : "ed01e7196afe56bf953f2f14b0b463208dda9c8d"
}
},
{
Expand Down
16 changes: 16 additions & 0 deletions Sources/OpenSwiftUI/Core/Attribute/AsyncAttribute.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// AsyncAttribute.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP

internal import OpenGraphShims

protocol AsyncAttribute: _AttributeBody {}

extension Attribute {
func syncMainIfReferences<V>(do body: (Value) -> V) -> V {
fatalError("TODO")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP
// Status: Blocked by syncMainIfReferences
// ID: 1DBD4F024EFF0E73A70DB6DD05D5B548

internal import OpenGraphShims

@frozen
public struct _EnvironmentKeyTransformModifier<Value>: PrimitiveViewModifier, _GraphInputsModifier {
public var keyPath: WritableKeyPath<EnvironmentValues, Value>
Expand All @@ -17,12 +19,22 @@ public struct _EnvironmentKeyTransformModifier<Value>: PrimitiveViewModifier, _G
self.transform = transform
}

public static func _makeInputs(modifier: _GraphValue<_EnvironmentKeyTransformModifier<Value>>, inputs: inout _GraphInputs) {

public static func _makeInputs(modifier: _GraphValue<Self>, inputs: inout _GraphInputs) {
let childEnvironment = ChildEnvironment(
modifier: modifier.value,
environment: inputs.cachedEnvironment.wrappedValue.environment,
oldKeyPath: nil
)
let attribute = Attribute(childEnvironment)
let cachedEnvironment = CachedEnvironment(attribute)
inputs.cachedEnvironment = MutableBox(cachedEnvironment)
inputs.changedDebugProperties.insert(.environment)
}
}

extension View {
/// Transforms the environment value of the specified key path with the
/// given function.
@inlinable
public func transformEnvironment<V>(
_ keyPath: WritableKeyPath<EnvironmentValues, V>,
Expand All @@ -35,6 +47,47 @@ extension View {
}
}

private struct ChildEnvironment {
private struct ChildEnvironment<Value>: StatefulRule, AsyncAttribute {
@Attribute
private var modifier: _EnvironmentKeyTransformModifier<Value>
private var _environment: Attribute<EnvironmentValues>
private var oldValue: Value?
private var oldKeyPath: WritableKeyPath<EnvironmentValues, Value>?

init(modifier: Attribute<_EnvironmentKeyTransformModifier<Value>>,
environment: Attribute<EnvironmentValues>,
oldValue: Value? = nil,
oldKeyPath: WritableKeyPath<EnvironmentValues, Value>?
) {
_modifier = modifier
_environment = environment
self.oldValue = oldValue
self.oldKeyPath = oldKeyPath
}

var description: String {
"EnvironmentTransform: EnvironmentValues"
}

typealias Value = EnvironmentValues

mutating func updateValue() {
var (environment, environmentChanged) = _environment.changedValue()
let keyPath = modifier.keyPath
var newValue = environment[keyPath: keyPath]
_modifier.syncMainIfReferences { modifier in
modifier.transform(&newValue)
}
guard !environmentChanged,
let valueChanged = oldValue.map({ compareValues($0, newValue, mode: ._2) }), !valueChanged,
let keyPathChanged = oldKeyPath.map({ $0 == keyPath }), !keyPathChanged,
hasValue
else {
environment[keyPath: keyPath] = newValue
value = environment
oldValue = newValue
oldKeyPath = keyPath
return
}
}
}
20 changes: 10 additions & 10 deletions Sources/OpenSwiftUI/View/Core/Debug/TODO/ViewDebug.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ extension _ViewDebug {
self.rawValue = rawValue
}

public static let type = Property(rawValue: 1 << Property.type.rawValue)
public static let value = Property(rawValue: 1 << Property.value.rawValue)
public static let transform = Property(rawValue: 1 << Property.transform.rawValue)
public static let position = Property(rawValue: 1 << Property.position.rawValue)
public static let size = Property(rawValue: 1 << Property.size.rawValue)
public static let environment = Property(rawValue: 1 << Property.environment.rawValue)
public static let phase = Property(rawValue: 1 << Property.phase.rawValue)
public static let layoutComputer = Property(rawValue: 1 << Property.layoutComputer.rawValue)
public static let displayList = Property(rawValue: 1 << Property.displayList.rawValue)
public static let all = Property(rawValue: 0xFFFF_FFFF)
public static let type = Properties(rawValue: 1 << Property.type.rawValue)
public static let value = Properties(rawValue: 1 << Property.value.rawValue)
public static let transform = Properties(rawValue: 1 << Property.transform.rawValue)
public static let position = Properties(rawValue: 1 << Property.position.rawValue)
public static let size = Properties(rawValue: 1 << Property.size.rawValue)
public static let environment = Properties(rawValue: 1 << Property.environment.rawValue)
public static let phase = Properties(rawValue: 1 << Property.phase.rawValue)
public static let layoutComputer = Properties(rawValue: 1 << Property.layoutComputer.rawValue)
public static let displayList = Properties(rawValue: 1 << Property.displayList.rawValue)
public static let all = Properties(rawValue: 0xFFFF_FFFF)
}
}

Expand Down

0 comments on commit 924d8be

Please sign in to comment.