From 7af6cea46b21816222db843d0b484420868afdc6 Mon Sep 17 00:00:00 2001 From: Kyle Date: Sat, 28 Sep 2024 22:19:51 +0800 Subject: [PATCH 1/4] Update Graph to 2024 --- Sources/OpenSwiftUI/Core/Graph/Graph.swift | 9 --- .../OpenSwiftUI/Core/Graph/GraphValue.swift | 51 ---------------- .../Core/View/ConditionalContent.swift | 1 + Sources/OpenSwiftUI/Core/View/ViewGraph.swift | 1 + .../DynamicProperty/DynamicProperty.swift | 1 + Sources/OpenSwiftUI/View/View/AnyView.swift | 1 + Sources/OpenSwiftUICore/Graph/Graph.swift | 14 +++++ .../OpenSwiftUICore/Graph/GraphValue.swift | 60 +++++++++++++++++++ 8 files changed, 78 insertions(+), 60 deletions(-) delete mode 100644 Sources/OpenSwiftUI/Core/Graph/Graph.swift delete mode 100644 Sources/OpenSwiftUI/Core/Graph/GraphValue.swift create mode 100644 Sources/OpenSwiftUICore/Graph/Graph.swift create mode 100644 Sources/OpenSwiftUICore/Graph/GraphValue.swift diff --git a/Sources/OpenSwiftUI/Core/Graph/Graph.swift b/Sources/OpenSwiftUI/Core/Graph/Graph.swift deleted file mode 100644 index b53be35..0000000 --- a/Sources/OpenSwiftUI/Core/Graph/Graph.swift +++ /dev/null @@ -1,9 +0,0 @@ -// -// Graph.swift -// OpenSwiftUI -// -// Audited for RELEASE_2021 -// Status: Complete - -/// A transient reference to a view graph. -public struct _Graph {} diff --git a/Sources/OpenSwiftUI/Core/Graph/GraphValue.swift b/Sources/OpenSwiftUI/Core/Graph/GraphValue.swift deleted file mode 100644 index b6c5129..0000000 --- a/Sources/OpenSwiftUI/Core/Graph/GraphValue.swift +++ /dev/null @@ -1,51 +0,0 @@ -// -// GraphValue.swift -// OpenSwiftUI -// -// Audited for RELEASE_2021 -// Status: Complete - -internal import OpenGraphShims - -/// A transient reference to a value in the view hierarchy's dataflow -/// graph. "Transient" means that these values must never be stored, -/// only passed around while initializing views. -public struct _GraphValue: Equatable { - var value: Attribute - - init(_ value: Attribute) { - self.value = value - } - - init(_ rule: R) where R.Value == Value { - value = Attribute(rule) - } - - init(_ rule: R) where R.Value == Value { - value = Attribute(rule) - } - - func unsafeBitCast(to type: V.Type) -> _GraphValue { - _GraphValue(value.unsafeBitCast(to: type)) - } - - /// Returns the value created by fetching the property at `keyPath` - /// of `self`. - public subscript(keyPath: KeyPath) -> _GraphValue { - _GraphValue(value[keyPath: keyPath]) - } - - subscript(offset body: (inout Value) -> PointerOffset) -> _GraphValue { - _GraphValue(value[offset: body]) - } - - public static func == (a: _GraphValue, b: _GraphValue) -> Bool { - a.value == b.value - } -} - -extension Attribute { - func unsafeBitCast(to type: V.Type) -> Attribute { - unsafeOffset(at: 0, as: V.self) - } -} diff --git a/Sources/OpenSwiftUI/Core/View/ConditionalContent.swift b/Sources/OpenSwiftUI/Core/View/ConditionalContent.swift index 480e0aa..1411513 100644 --- a/Sources/OpenSwiftUI/Core/View/ConditionalContent.swift +++ b/Sources/OpenSwiftUI/Core/View/ConditionalContent.swift @@ -7,6 +7,7 @@ // ID: 1A625ACC143FD8524C590782FD8F4F8C internal import OpenGraphShims +@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore /// View content that shows one of two possible children. @frozen diff --git a/Sources/OpenSwiftUI/Core/View/ViewGraph.swift b/Sources/OpenSwiftUI/Core/View/ViewGraph.swift index 7627547..9f8a9c8 100644 --- a/Sources/OpenSwiftUI/Core/View/ViewGraph.swift +++ b/Sources/OpenSwiftUI/Core/View/ViewGraph.swift @@ -8,6 +8,7 @@ internal import OpenGraphShims import Foundation +@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore final class ViewGraph: GraphHost { @inline(__always) diff --git a/Sources/OpenSwiftUI/Data/Model/DynamicProperty/DynamicProperty.swift b/Sources/OpenSwiftUI/Data/Model/DynamicProperty/DynamicProperty.swift index 3583279..134534e 100644 --- a/Sources/OpenSwiftUI/Data/Model/DynamicProperty/DynamicProperty.swift +++ b/Sources/OpenSwiftUI/Data/Model/DynamicProperty/DynamicProperty.swift @@ -7,6 +7,7 @@ // ID: 49D2A32E637CD497C6DE29B8E060A506 internal import OpenGraphShims +@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore /// An interface for a stored variable that updates an external property of a /// view. diff --git a/Sources/OpenSwiftUI/View/View/AnyView.swift b/Sources/OpenSwiftUI/View/View/AnyView.swift index 7e0448b..a885632 100644 --- a/Sources/OpenSwiftUI/View/View/AnyView.swift +++ b/Sources/OpenSwiftUI/View/View/AnyView.swift @@ -8,6 +8,7 @@ internal import OpenGraphShims internal import COpenSwiftUI +@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore @frozen public struct AnyView: PrimitiveView { diff --git a/Sources/OpenSwiftUICore/Graph/Graph.swift b/Sources/OpenSwiftUICore/Graph/Graph.swift new file mode 100644 index 0000000..e23cd7c --- /dev/null +++ b/Sources/OpenSwiftUICore/Graph/Graph.swift @@ -0,0 +1,14 @@ +// +// Graph.swift +// OpenSwiftUI +// +// Audited for RELEASE_2024 +// Status: Complete + +/// A transient reference to a view graph. +public struct _Graph { + package init(){} +} + +@available(*, unavailable) +extension _Graph : Swift.Sendable {} diff --git a/Sources/OpenSwiftUICore/Graph/GraphValue.swift b/Sources/OpenSwiftUICore/Graph/GraphValue.swift new file mode 100644 index 0000000..d5b3221 --- /dev/null +++ b/Sources/OpenSwiftUICore/Graph/GraphValue.swift @@ -0,0 +1,60 @@ +// +// GraphValue.swift +// OpenSwiftUI +// +// Audited for RELEASE_2024 +// Status: Complete + +public import OpenGraphShims + +/// A transient reference to a value in the view hierarchy's dataflow +/// graph. "Transient" means that these values must never be stored, +/// only passed around while initializing views. +public struct _GraphValue: Equatable { + package var value: Attribute + + @_spi(ForOpenSwiftUIOnly) + public init(_ value: Attribute) { + self.value = value + } + + @_spi(ForOpenSwiftUIOnly) + public init(_ value: U) where Value == U.Value, U: Rule { + self.init(Attribute(value)) + } + + @_spi(ForOpenSwiftUIOnly) + public init(_ value: U) where Value == U.Value, U: StatefulRule { + self.init(Attribute(value)) + } + + public subscript(keyPath: KeyPath) -> _GraphValue { + _GraphValue(value[keyPath: keyPath]) + } + + package subscript(offset subject: (inout Value) -> PointerOffset) -> _GraphValue { + _GraphValue(value[offset: subject]) + } + + package func unsafeCast(to _: T.Type = T.self) -> _GraphValue { + _GraphValue(value.unsafeCast(to: T.self)) + } + + package func unsafeBitCast(to _: T.Type) -> _GraphValue { + _GraphValue(value.unsafeBitCast(to: T.self)) + + } + + public static func == (a: _GraphValue, b: _GraphValue) -> Bool { + a.value == b.value + } +} + +@available(*, unavailable) +extension _GraphValue: Sendable {} + +extension Attribute { + func unsafeBitCast(to type: V.Type) -> Attribute { + unsafeOffset(at: 0, as: V.self) + } +} From e4035e494f35c03ccd18a0a685b6e324530458b1 Mon Sep 17 00:00:00 2001 From: Kyle Date: Sat, 28 Sep 2024 23:58:51 +0800 Subject: [PATCH 2/4] Update GraphMutation --- Package.resolved | 2 +- .../Core/Graph/GraphDelegate.swift | 14 ++++- .../OpenSwiftUI/Core/Graph/GraphHost.swift | 6 +- .../Core/Graph/GraphInputsModifier.swift | 2 +- .../Core/Graph/GraphMutation.swift | 55 ---------------- .../Data/Model/State/StoredLocation.swift | 2 +- .../Integration/UIKit/UIHostingView.swift | 2 +- .../OpenSwiftUICore/Graph/GraphMutation.swift | 62 +++++++++++++++++++ 8 files changed, 81 insertions(+), 64 deletions(-) delete mode 100644 Sources/OpenSwiftUI/Core/Graph/GraphMutation.swift create mode 100644 Sources/OpenSwiftUICore/Graph/GraphMutation.swift diff --git a/Package.resolved b/Package.resolved index d079f1a..164540f 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "093c1851811a8423bb946386b7901adf094aa28258f12ae335ef9ee3a59018c7", + "originHash" : "b56337b3dd1894393ef8978297a221dd102010b985af35ef055a726c560435e6", "pins" : [ { "identity" : "opengraph", diff --git a/Sources/OpenSwiftUI/Core/Graph/GraphDelegate.swift b/Sources/OpenSwiftUI/Core/Graph/GraphDelegate.swift index 70725ac..b9476ed 100644 --- a/Sources/OpenSwiftUI/Core/Graph/GraphDelegate.swift +++ b/Sources/OpenSwiftUI/Core/Graph/GraphDelegate.swift @@ -2,11 +2,21 @@ // GraphDelegate.swift // OpenSwiftUI // -// Audited for RELEASE_2021 +// Audited for RELEASE_2024 // Status: Complete +//@_spi(ForOpenSwiftUIOnly) +/*public*/ protocol GraphDelegate: AnyObject { - func updateGraph(body: (GraphHost) -> V) -> V + func updateGraph(body: (GraphHost) -> T) -> T func graphDidChange() func preferencesDidChange() + func beginTransaction() +} + +@_spi(ForOpenSwiftUIOnly) +extension GraphDelegate { + public func beginTransaction() { + // TODO + } } diff --git a/Sources/OpenSwiftUI/Core/Graph/GraphHost.swift b/Sources/OpenSwiftUI/Core/Graph/GraphHost.swift index 3a2efe6..3b34c28 100644 --- a/Sources/OpenSwiftUI/Core/Graph/GraphHost.swift +++ b/Sources/OpenSwiftUI/Core/Graph/GraphHost.swift @@ -245,7 +245,7 @@ class GraphHost { asyncTransaction( transaction, mutation: EmptyGraphMutation(), - style: ._1, + style: .deferred, mayDeferUpdate: true ) } @@ -288,8 +288,8 @@ class GraphHost { guard let parent = host.parentHost else { asyncTransaction( Transaction(), - mutation: CustomGraphMutation(body: body), - style: ._1, + mutation: CustomGraphMutation(body), + style: .deferred, mayDeferUpdate: true ) return diff --git a/Sources/OpenSwiftUI/Core/Graph/GraphInputsModifier.swift b/Sources/OpenSwiftUI/Core/Graph/GraphInputsModifier.swift index 4d0f695..9f73126 100644 --- a/Sources/OpenSwiftUI/Core/Graph/GraphInputsModifier.swift +++ b/Sources/OpenSwiftUI/Core/Graph/GraphInputsModifier.swift @@ -2,7 +2,7 @@ // _GraphInputsModifier.swift // OpenSwiftUI // -// Audited for RELEASE_2021 +// Audited for RELEASE_2024 // Status: Complete /// Protocol for modifiers that only modify their children's inputs. diff --git a/Sources/OpenSwiftUI/Core/Graph/GraphMutation.swift b/Sources/OpenSwiftUI/Core/Graph/GraphMutation.swift deleted file mode 100644 index 3d0c1c8..0000000 --- a/Sources/OpenSwiftUI/Core/Graph/GraphMutation.swift +++ /dev/null @@ -1,55 +0,0 @@ -// -// GraphMutation.swift -// OpenSwiftUI -// -// Audited for RELEASE_2021 -// Status: Complete - -internal import OpenGraphShims - -protocol GraphMutation { - func apply() - mutating func combine(with: Mutation) -> Bool -} - -struct EmptyGraphMutation: GraphMutation { - func apply() {} - func combine(with _: Mutation) -> Bool where Mutation: GraphMutation { - Mutation.self == EmptyGraphMutation.self - } -} - -// FIXME: #39 -#if canImport(Darwin) -struct InvalidatingGraphMutation: GraphMutation { - let attribute: OGWeakAttribute - - func apply() { - attribute.attribute?.invalidateValue() - } - - func combine(with mutation: some GraphMutation) -> Bool { - guard let mutation = mutation as? InvalidatingGraphMutation else { - return false - } - return mutation.attribute == attribute - } -} -#endif - -struct CustomGraphMutation: GraphMutation { - let body: () -> Void - - func apply() { - body() - } - - func combine(with _: some GraphMutation) -> Bool { - false - } -} - -enum _GraphMutation_Style { - case _0 - case _1 -} diff --git a/Sources/OpenSwiftUI/Data/Model/State/StoredLocation.swift b/Sources/OpenSwiftUI/Data/Model/State/StoredLocation.swift index 50fc7b2..9e72951 100644 --- a/Sources/OpenSwiftUI/Data/Model/State/StoredLocation.swift +++ b/Sources/OpenSwiftUI/Data/Model/State/StoredLocation.swift @@ -165,7 +165,7 @@ final class StoredLocation: StoredLocationBase, @unchecked Sendabl host?.asyncTransaction( transaction, mutation: mutation, - style: ._1, + style: .deferred, mayDeferUpdate: true ) } diff --git a/Sources/OpenSwiftUI/Integration/UIKit/UIHostingView.swift b/Sources/OpenSwiftUI/Integration/UIKit/UIHostingView.swift index bf313df..3364d70 100644 --- a/Sources/OpenSwiftUI/Integration/UIKit/UIHostingView.swift +++ b/Sources/OpenSwiftUI/Integration/UIKit/UIHostingView.swift @@ -64,7 +64,7 @@ open class _UIHostingView: UIView where Content: View { viewGraph.asyncTransaction( transaction, mutation: mutation, - style: ._1, + style: .deferred, mayDeferUpdate: true ) } diff --git a/Sources/OpenSwiftUICore/Graph/GraphMutation.swift b/Sources/OpenSwiftUICore/Graph/GraphMutation.swift new file mode 100644 index 0000000..ce21d1c --- /dev/null +++ b/Sources/OpenSwiftUICore/Graph/GraphMutation.swift @@ -0,0 +1,62 @@ +// +// GraphMutation.swift +// OpenSwiftUI +// +// Audited for RELEASE_2024 +// Status: Complete +// ID: F9F204BD2F8DB167A76F17F3FB1B3335 + +internal import OpenGraphShims + +package protocol GraphMutation { + typealias Style = _GraphMutation_Style + func apply() + mutating func combine(with other: T) -> Bool where T: GraphMutation +} + +package enum _GraphMutation_Style: Hashable { + case immediate + case deferred +} + +/*private*/ package struct EmptyGraphMutation: GraphMutation { + package init() {} + package func apply() {} + package func combine(with other: T) -> Bool where T: GraphMutation { + T.self == EmptyGraphMutation.self + } +} + +package struct CustomGraphMutation: GraphMutation { + let body: () -> Void + + package init(_ body: @escaping () -> Void) { + self.body = body + } + + package func apply() { + body() + } + + package func combine(with other: T) -> Bool where T : GraphMutation { + false + } +} + +// FIXME: #39 +#if canImport(Darwin) +struct InvalidatingGraphMutation: GraphMutation { + let attribute: OGWeakAttribute + + func apply() { + attribute.attribute?.invalidateValue() + } + + func combine(with mutation: some GraphMutation) -> Bool { + guard let mutation = mutation as? InvalidatingGraphMutation else { + return false + } + return mutation.attribute == attribute + } +} +#endif From ac37052d9f9cee7451d4c96321ce9dadf23c867a Mon Sep 17 00:00:00 2001 From: Kyle Date: Sun, 29 Sep 2024 00:00:05 +0800 Subject: [PATCH 3/4] Bump OG version and update API --- Package.resolved | 2 +- .../Attribute/InvalidatableAttribute.swift | 2 +- .../Core/Attribute/RemovableAttribute.swift | 4 ++-- .../Core/BodyAccessor/BodyAccessorRule.swift | 6 +++--- .../OpenSwiftUI/Core/Graph/GraphHost.swift | 4 ++-- .../OpenSwiftUI/Core/Graph/GraphInputs.swift | 6 +++--- .../OpenSwiftUI/Core/View/View/ViewList.swift | 6 +++--- .../Core/View/View/ViewOutputs.swift | 4 ++-- Sources/OpenSwiftUI/Core/View/ViewGraph.swift | 6 +++--- .../Data/Environment/CachedEnvironment.swift | 4 ++-- .../DynamicProperty/DynamicProperty.swift | 12 +++++------ .../Data/Preference/PreferenceBridge.swift | 20 +++++++++---------- .../Data/Preference/PreferencesOutputs.swift | 10 +++++----- .../View/Debug/ChangedBodyProperty.swift | 2 +- .../Modifier/AppearanceActionModifier.swift | 4 ++-- Sources/OpenSwiftUI/View/View/AnyView.swift | 6 +++--- .../OpenSwiftUICore/Graph/GraphMutation.swift | 2 +- 17 files changed, 50 insertions(+), 50 deletions(-) diff --git a/Package.resolved b/Package.resolved index 164540f..6f044af 100644 --- a/Package.resolved +++ b/Package.resolved @@ -7,7 +7,7 @@ "location" : "https://github.com/OpenSwiftUIProject/OpenGraph", "state" : { "branch" : "main", - "revision" : "7f22fb5948bb6bc77603f5496a9881622041d3bb" + "revision" : "93e713f1357fc04ca917635d09460dbf42b663b6" } } ], diff --git a/Sources/OpenSwiftUI/Core/Attribute/InvalidatableAttribute.swift b/Sources/OpenSwiftUI/Core/Attribute/InvalidatableAttribute.swift index af23a01..9940649 100644 --- a/Sources/OpenSwiftUI/Core/Attribute/InvalidatableAttribute.swift +++ b/Sources/OpenSwiftUI/Core/Attribute/InvalidatableAttribute.swift @@ -10,5 +10,5 @@ internal import OpenGraphShims // TODO: PlatformViewChild in _A513612C07DFA438E70B9FA90719B40D protocol InvalidatableAttribute: _AttributeBody { - static func willInvalidate(attribute: OGAttribute) + static func willInvalidate(attribute: AnyAttribute) } diff --git a/Sources/OpenSwiftUI/Core/Attribute/RemovableAttribute.swift b/Sources/OpenSwiftUI/Core/Attribute/RemovableAttribute.swift index 21f54b4..7b5f2a8 100644 --- a/Sources/OpenSwiftUI/Core/Attribute/RemovableAttribute.swift +++ b/Sources/OpenSwiftUI/Core/Attribute/RemovableAttribute.swift @@ -8,6 +8,6 @@ internal import OpenGraphShims protocol RemovableAttribute: _AttributeBody { - static func willRemove(attribute: OGAttribute) - static func didReinsert(attribute: OGAttribute) + static func willRemove(attribute: AnyAttribute) + static func didReinsert(attribute: AnyAttribute) } diff --git a/Sources/OpenSwiftUI/Core/BodyAccessor/BodyAccessorRule.swift b/Sources/OpenSwiftUI/Core/BodyAccessor/BodyAccessorRule.swift index ee513ff..10292b8 100644 --- a/Sources/OpenSwiftUI/Core/BodyAccessor/BodyAccessorRule.swift +++ b/Sources/OpenSwiftUI/Core/BodyAccessor/BodyAccessorRule.swift @@ -9,7 +9,7 @@ internal import OpenGraphShims protocol BodyAccessorRule { static var container: Any.Type { get } - static func value(as: Value.Type, attribute: OGAttribute) -> Value? - static func buffer(as: Value.Type, attribute: OGAttribute) -> _DynamicPropertyBuffer? - static func metaProperties(as: Value.Type, attribute: OGAttribute) -> [(String, OGAttribute)] + static func value(as: Value.Type, attribute: AnyAttribute) -> Value? + static func buffer(as: Value.Type, attribute: AnyAttribute) -> _DynamicPropertyBuffer? + static func metaProperties(as: Value.Type, attribute: AnyAttribute) -> [(String, AnyAttribute)] } diff --git a/Sources/OpenSwiftUI/Core/Graph/GraphHost.swift b/Sources/OpenSwiftUI/Core/Graph/GraphHost.swift index 3b34c28..a4f32b5 100644 --- a/Sources/OpenSwiftUI/Core/Graph/GraphHost.swift +++ b/Sources/OpenSwiftUI/Core/Graph/GraphHost.swift @@ -29,7 +29,7 @@ class GraphHost { static var currentHost: GraphHost { #if canImport(Darwin) - if let currentAttribute = OGAttribute.current { + if let currentAttribute = AnyAttribute.current { currentAttribute.graph.graphHost() } else if let currentSubgraph = OGSubgraph.current { currentSubgraph.graph.graphHost() @@ -230,7 +230,7 @@ class GraphHost { // TODO: } - final func graphInvalidation(from attribute: OGAttribute?) { + final func graphInvalidation(from attribute: AnyAttribute?) { #if canImport(Darwin) guard let attribute else { graphDelegate?.graphDidChange() diff --git a/Sources/OpenSwiftUI/Core/Graph/GraphInputs.swift b/Sources/OpenSwiftUI/Core/Graph/GraphInputs.swift index 411740b..ca763c8 100644 --- a/Sources/OpenSwiftUI/Core/Graph/GraphInputs.swift +++ b/Sources/OpenSwiftUI/Core/Graph/GraphInputs.swift @@ -9,7 +9,7 @@ public struct _GraphInputs { private var changedDebugProperties: _ViewDebug.Properties private var options: _GraphInputs.Options #if canImport(Darwin) // FIXME: See #39 - var mergedInputs: Set + var mergedInputs: Set #endif #if canImport(Darwin) @@ -20,7 +20,7 @@ public struct _GraphInputs { transaction: Attribute, changedDebugProperties: _ViewDebug.Properties = [], options: _GraphInputs.Options = [], - mergedInputs: Set = []) { + mergedInputs: Set = []) { self.customInputs = customInputs self.time = time self.cachedEnvironment = cachedEnvironment @@ -38,7 +38,7 @@ public struct _GraphInputs { transaction: Attribute, changedDebugProperties: _ViewDebug.Properties = [], options: _GraphInputs.Options = []/*, - mergedInputs: Set = []*/) { + mergedInputs: Set = []*/) { self.customInputs = customInputs self.time = time self.cachedEnvironment = cachedEnvironment diff --git a/Sources/OpenSwiftUI/Core/View/View/ViewList.swift b/Sources/OpenSwiftUI/Core/View/View/ViewList.swift index 9dafc75..c4d0952 100644 --- a/Sources/OpenSwiftUI/Core/View/View/ViewList.swift +++ b/Sources/OpenSwiftUI/Core/View/View/ViewList.swift @@ -191,7 +191,7 @@ package struct _ViewList_ID { private struct Explicit: Equatable { let id: AnyHashable #if canImport(Darwin) - let owner: OGAttribute + let owner: AnyAttribute #endif let isUnary: Bool } @@ -203,7 +203,7 @@ package struct _ViewList_ID { } #if canImport(Darwin) - mutating func bind(explicitID: AnyHashable, owner: OGAttribute, isUnary: Bool) { + mutating func bind(explicitID: AnyHashable, owner: AnyAttribute, isUnary: Bool) { explicitIDs.append(.init(id: explicitID, owner: owner, isUnary: isUnary)) } #endif @@ -226,7 +226,7 @@ final package class _ViewList_IndirectMap { final package let subgraph: OGSubgraph #if canImport(Darwin) - final package var map: [OGAttribute: OGAttribute] + final package var map: [AnyAttribute: AnyAttribute] #endif init(subgraph: OGSubgraph) { diff --git a/Sources/OpenSwiftUI/Core/View/View/ViewOutputs.swift b/Sources/OpenSwiftUI/Core/View/View/ViewOutputs.swift index c8fe0d0..4f8e0e0 100644 --- a/Sources/OpenSwiftUI/Core/View/View/ViewOutputs.swift +++ b/Sources/OpenSwiftUI/Core/View/View/ViewOutputs.swift @@ -30,7 +30,7 @@ public struct _ViewOutputs { func detachIndirectOutputs() { #if canImport(Darwin) struct ResetPreference: PreferenceKeyVisitor { - var dst: OGAttribute + var dst: AnyAttribute func visit(key: Key.Type) { let graphHost = dst.graph.graphHost() let source = graphHost.intern(Key.defaultValue, id: .zero) @@ -63,7 +63,7 @@ public struct _ViewOutputs { @inline(__always) func forEach(body: ( _ key: AnyPreferenceKey.Type, - _ value: OGAttribute + _ value: AnyAttribute ) throws -> Void ) rethrows { try preferences.forEach(body: body) diff --git a/Sources/OpenSwiftUI/Core/View/ViewGraph.swift b/Sources/OpenSwiftUI/Core/View/ViewGraph.swift index 9f8a9c8..ed0d3a6 100644 --- a/Sources/OpenSwiftUI/Core/View/ViewGraph.swift +++ b/Sources/OpenSwiftUI/Core/View/ViewGraph.swift @@ -15,10 +15,10 @@ final class ViewGraph: GraphHost { static var current: ViewGraph { GraphHost.currentHost as! ViewGraph } let rootViewType: Any.Type - let makeRootView: (OGAttribute, _ViewInputs) -> _ViewOutputs + let makeRootView: (AnyAttribute, _ViewInputs) -> _ViewOutputs weak var delegate: ViewGraphDelegate? var centersRootView: Bool = true - let rootView: OGAttribute + let rootView: AnyAttribute @Attribute var rootTransform: ViewTransform @Attribute var zeroPoint: ViewOrigin // TODO @@ -53,7 +53,7 @@ final class ViewGraph: GraphHost { set { setPreferenceBridge(to: newValue, isInvalidating: false) } } #if canImport(Darwin) // FIXME: See #39 - var bridgedPreferences: [(AnyPreferenceKey.Type, OGAttribute)] = [] + var bridgedPreferences: [(AnyPreferenceKey.Type, AnyAttribute)] = [] #endif // TODO diff --git a/Sources/OpenSwiftUI/Data/Environment/CachedEnvironment.swift b/Sources/OpenSwiftUI/Data/Environment/CachedEnvironment.swift index 46f9353..bda4e96 100644 --- a/Sources/OpenSwiftUI/Data/Environment/CachedEnvironment.swift +++ b/Sources/OpenSwiftUI/Data/Environment/CachedEnvironment.swift @@ -13,7 +13,7 @@ struct CachedEnvironment { var environment: Attribute private var items: [Item] #if canImport(Darwin) - private var constants: [HashableConstant: OGAttribute] + private var constants: [HashableConstant: AnyAttribute] #endif private var animatedFrame: AnimatedFrame? // private var resolvedFgStyles: [ResolvedFgStyle : Swift<_ShapeStyle_Resolved.ResolvedFg>] @@ -72,7 +72,7 @@ extension CachedEnvironment { private struct Item { var key: PartialKeyPath #if canImport(Darwin) // See #39 - var value: OGAttribute + var value: AnyAttribute #endif } } diff --git a/Sources/OpenSwiftUI/Data/Model/DynamicProperty/DynamicProperty.swift b/Sources/OpenSwiftUI/Data/Model/DynamicProperty/DynamicProperty.swift index 134534e..6f1d4f3 100644 --- a/Sources/OpenSwiftUI/Data/Model/DynamicProperty/DynamicProperty.swift +++ b/Sources/OpenSwiftUI/Data/Model/DynamicProperty/DynamicProperty.swift @@ -179,18 +179,18 @@ extension StaticBody: BodyAccessorRule { Accessor.Container.self } - static func value(as _: Value.Type, attribute: OGAttribute) -> Value? { + static func value(as _: Value.Type, attribute: AnyAttribute) -> Value? { guard container == Value.self else { return nil } return unsafeBitCast(attribute.info.body.assumingMemoryBound(to: Self.self).pointee.container, to: Value.self) } - static func buffer(as _: Value.Type, attribute _: OGAttribute) -> _DynamicPropertyBuffer? { + static func buffer(as _: Value.Type, attribute _: AnyAttribute) -> _DynamicPropertyBuffer? { nil } - static func metaProperties(as _: Value.Type, attribute: OGAttribute) -> [(String, OGAttribute)] { + static func metaProperties(as _: Value.Type, attribute: AnyAttribute) -> [(String, AnyAttribute)] { guard container == Value.self else { return [] } @@ -253,21 +253,21 @@ extension DynamicBody: BodyAccessorRule { Accessor.Container.self } - static func value(as _: Value.Type, attribute: OGAttribute) -> Value? { + static func value(as _: Value.Type, attribute: AnyAttribute) -> Value? { guard container == Value.self else { return nil } return unsafeBitCast(attribute.info.body.assumingMemoryBound(to: Self.self).pointee.container, to: Value.self) } - static func buffer(as _: Value.Type, attribute: OGAttribute) -> _DynamicPropertyBuffer? { + static func buffer(as _: Value.Type, attribute: AnyAttribute) -> _DynamicPropertyBuffer? { guard container == Value.self else { return nil } return attribute.info.body.assumingMemoryBound(to: Self.self).pointee.links } - static func metaProperties(as _: Value.Type, attribute: OGAttribute) -> [(String, OGAttribute)] { + static func metaProperties(as _: Value.Type, attribute: AnyAttribute) -> [(String, AnyAttribute)] { guard container == Value.self else { return [] } diff --git a/Sources/OpenSwiftUI/Data/Preference/PreferenceBridge.swift b/Sources/OpenSwiftUI/Data/Preference/PreferenceBridge.swift index a7a11d3..9a585fe 100644 --- a/Sources/OpenSwiftUI/Data/Preference/PreferenceBridge.swift +++ b/Sources/OpenSwiftUI/Data/Preference/PreferenceBridge.swift @@ -19,7 +19,7 @@ final class PreferenceBridge { struct BridgedPreference { var key: AnyPreferenceKey.Type - var combiner: OGWeakAttribute + var combiner: AnyWeakAttribute } init() { @@ -27,16 +27,16 @@ final class PreferenceBridge { } #if canImport(Darwin) // FIXME: See #39 - func addValue(_ value: OGAttribute, for keyType: AnyPreferenceKey.Type) { + func addValue(_ value: AnyAttribute, for keyType: AnyPreferenceKey.Type) { struct AddValue: PreferenceKeyVisitor { - var combiner: OGAttribute - var value: OGAttribute + var combiner: AnyAttribute + var value: AnyAttribute func visit(key _: Key.Type) { combiner.mutateBody( as: PreferenceCombiner.self, invalidating: true ) { combiner in - combiner.attributes.append(WeakAttribute(base: OGWeakAttribute(value))) + combiner.attributes.append(WeakAttribute(base: AnyWeakAttribute(value))) } } } @@ -51,10 +51,10 @@ final class PreferenceBridge { viewGraph.graphInvalidation(from: value) } - func removeValue(_ value: OGAttribute, for keyType: AnyPreferenceKey.Type, isInvalidating: Bool) { + func removeValue(_ value: AnyAttribute, for keyType: AnyPreferenceKey.Type, isInvalidating: Bool) { struct RemoveValue: PreferenceKeyVisitor { - var combiner: OGAttribute - var value: OGAttribute + var combiner: AnyAttribute + var value: AnyAttribute var changed = false mutating func visit(key _: Key.Type) { combiner.mutateBody( @@ -149,7 +149,7 @@ final class PreferenceBridge { func wrapOutputs(_ outputs: inout PreferencesOutputs, inputs: _ViewInputs) { struct MakeCombiner: PreferenceKeyVisitor { - var result: OGAttribute? + var result: AnyAttribute? mutating func visit(key _: Key.Type) where Key: PreferenceKey { result = Attribute(PreferenceCombiner(attributes: [])).identifier @@ -178,7 +178,7 @@ final class PreferenceBridge { if !requestedPreferences.contains(key) { requestedPreferences.add(key) } - bridgedPreferences.append(BridgedPreference(key: key, combiner: OGWeakAttribute(combiner))) + bridgedPreferences.append(BridgedPreference(key: key, combiner: AnyWeakAttribute(combiner))) outputs[anyKey: key] = combiner } } diff --git a/Sources/OpenSwiftUI/Data/Preference/PreferencesOutputs.swift b/Sources/OpenSwiftUI/Data/Preference/PreferencesOutputs.swift index 4159698..543ba61 100644 --- a/Sources/OpenSwiftUI/Data/Preference/PreferencesOutputs.swift +++ b/Sources/OpenSwiftUI/Data/Preference/PreferencesOutputs.swift @@ -31,7 +31,7 @@ struct PreferencesOutputs { } } - subscript(anyKey keyType: AnyPreferenceKey.Type) -> OGAttribute? { + subscript(anyKey keyType: AnyPreferenceKey.Type) -> AnyAttribute? { get { preferences.first { $0.key == keyType }?.value } set { if keyType == _AnyPreferenceKey.self { @@ -56,7 +56,7 @@ struct PreferencesOutputs { @inline(__always) func forEach(body: ( _ key: AnyPreferenceKey.Type, - _ value: OGAttribute + _ value: AnyAttribute ) throws -> Void ) rethrows { try preferences.forEach { try body($0.key, $0.value) } @@ -65,9 +65,9 @@ struct PreferencesOutputs { @inline(__always) func first(where predicate: ( _ key: AnyPreferenceKey.Type, - _ value: OGAttribute + _ value: AnyAttribute ) throws -> Bool - ) rethrows -> (key: AnyPreferenceKey.Type, value: OGAttribute)? { + ) rethrows -> (key: AnyPreferenceKey.Type, value: AnyAttribute)? { try preferences .first { try predicate($0.key, $0.value) } .map { ($0.key, $0.value) } @@ -91,7 +91,7 @@ extension PreferencesOutputs { private struct KeyValue { var key: AnyPreferenceKey.Type #if canImport(Darwin) // FIXME: See #39 - var value: OGAttribute + var value: AnyAttribute #endif } } diff --git a/Sources/OpenSwiftUI/View/Debug/ChangedBodyProperty.swift b/Sources/OpenSwiftUI/View/Debug/ChangedBodyProperty.swift index 9243a5a..02c8979 100644 --- a/Sources/OpenSwiftUI/View/Debug/ChangedBodyProperty.swift +++ b/Sources/OpenSwiftUI/View/Debug/ChangedBodyProperty.swift @@ -164,7 +164,7 @@ func changedBodyProperties(of type: Body.Type) -> [String] { continue } var properties: [String] = [] - let attribute = OGAttribute(rawValue: nodeID) + let attribute = AnyAttribute(rawValue: nodeID) let metaProperties = selfType.metaProperties(as: type, attribute: attribute) if !metaProperties.isEmpty, let inputs = dict["inputs"] as? [[String: Any]] { for metaProperty in metaProperties { diff --git a/Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift b/Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift index aa4f14f..3db2e37 100644 --- a/Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift +++ b/Sources/OpenSwiftUI/View/Modifier/AppearanceActionModifier.swift @@ -101,7 +101,7 @@ extension AppearanceEffect: StatefulRule { // MARK: AppearanceEffect + RemovableAttribute extension AppearanceEffect: RemovableAttribute { - static func willRemove(attribute: OGAttribute) { + static func willRemove(attribute: AnyAttribute) { let appearancePointer = UnsafeMutableRawPointer(mutating: attribute.info.body) .assumingMemoryBound(to: AppearanceEffect.self) guard appearancePointer.pointee.lastValue != nil else { @@ -110,7 +110,7 @@ extension AppearanceEffect: RemovableAttribute { appearancePointer.pointee.disappeared() } - static func didReinsert(attribute: OGAttribute) { + static func didReinsert(attribute: AnyAttribute) { let appearancePointer = UnsafeMutableRawPointer(mutating: attribute.info.body) .assumingMemoryBound(to: AppearanceEffect.self) guard let nodeAttribute = appearancePointer.pointee.node.attribute else { diff --git a/Sources/OpenSwiftUI/View/View/AnyView.swift b/Sources/OpenSwiftUI/View/View/AnyView.swift index a885632..bf2be53 100644 --- a/Sources/OpenSwiftUI/View/View/AnyView.swift +++ b/Sources/OpenSwiftUI/View/View/AnyView.swift @@ -191,7 +191,7 @@ private struct AnyViewContainer: StatefulRule, AsyncAttribute { func makeItem(_ storage: AnyViewStorageBase, uniqueId: UInt32) -> AnyViewInfo { #if canImport(Darwin) - let current = OGAttribute.current! + let current = AnyAttribute.current! let childGraph = OGSubgraph(graph: parentSubgraph.graph) parentSubgraph.addChild(childGraph) return childGraph.apply { @@ -268,7 +268,7 @@ private struct AnyViewList: StatefulRule, AsyncAttribute { final class Item: _ViewList_Subgraph { let type: Any.Type #if canImport(Darwin) - let owner: OGAttribute + let owner: AnyAttribute #endif @Attribute var list: ViewList let id: UniqueID @@ -276,7 +276,7 @@ private struct AnyViewList: StatefulRule, AsyncAttribute { let allItems: MutableBox<[Unmanaged]> #if canImport(Darwin) - init(type: Any.Type, owner: OGAttribute, list: Attribute, id: UniqueID, isUnary: Bool, subgraph: OGSubgraph, allItems: MutableBox<[Unmanaged]>) { + init(type: Any.Type, owner: AnyAttribute, list: Attribute, id: UniqueID, isUnary: Bool, subgraph: OGSubgraph, allItems: MutableBox<[Unmanaged]>) { self.type = type self.owner = owner _list = list diff --git a/Sources/OpenSwiftUICore/Graph/GraphMutation.swift b/Sources/OpenSwiftUICore/Graph/GraphMutation.swift index ce21d1c..67a4863 100644 --- a/Sources/OpenSwiftUICore/Graph/GraphMutation.swift +++ b/Sources/OpenSwiftUICore/Graph/GraphMutation.swift @@ -46,7 +46,7 @@ package struct CustomGraphMutation: GraphMutation { // FIXME: #39 #if canImport(Darwin) struct InvalidatingGraphMutation: GraphMutation { - let attribute: OGWeakAttribute + let attribute: AnyWeakAttribute func apply() { attribute.attribute?.invalidateValue() From 5b216e2e095511409eaa30c5889d354a8c0ae145 Mon Sep 17 00:00:00 2001 From: Kyle Date: Sun, 29 Sep 2024 02:05:46 +0800 Subject: [PATCH 4/4] Add GraphReusable --- .../OpenSwiftUI/Core/Graph/GraphInput.swift | 139 +++++++++++++++++- .../OpenSwiftUI/Core/Graph/GraphInputs.swift | 123 ---------------- .../Core/Graph/GraphInputsModifier.swift | 11 -- Sources/OpenSwiftUICore/Graph/Graph.swift | 2 +- .../OpenSwiftUICore/Graph/GraphMutation.swift | 2 +- .../OpenSwiftUICore/Graph/GraphReusable.swift | 131 +++++++++++++++++ .../OpenSwiftUICore/Graph/GraphValue.swift | 2 +- 7 files changed, 270 insertions(+), 140 deletions(-) delete mode 100644 Sources/OpenSwiftUI/Core/Graph/GraphInputs.swift delete mode 100644 Sources/OpenSwiftUI/Core/Graph/GraphInputsModifier.swift create mode 100644 Sources/OpenSwiftUICore/Graph/GraphReusable.swift diff --git a/Sources/OpenSwiftUI/Core/Graph/GraphInput.swift b/Sources/OpenSwiftUI/Core/Graph/GraphInput.swift index 3f34279..81845c7 100644 --- a/Sources/OpenSwiftUI/Core/Graph/GraphInput.swift +++ b/Sources/OpenSwiftUI/Core/Graph/GraphInput.swift @@ -1,8 +1,141 @@ // -// GraphInput.swift -// OpenSwiftUI +// GraphInputs.swift +// OpenSwiftUICore // // Audited for RELEASE_2021 // Status: Complete -protocol GraphInput: PropertyKey {} +package protocol GraphInput: PropertyKey { + static var isTriviallyReusable: Swift.Bool { get } + static func makeReusable(indirectMap: IndirectAttributeMap, value: inout Value) + static func tryToReuse(_ value: Value, by other: Value, indirectMap: IndirectAttributeMap, testOnly: Bool) -> Bool +} + +internal import OpenGraphShims + +public struct _GraphInputs { + var customInputs: PropertyList + var time: Attribute