-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update _GraphInputs * remove mode parameter pass * Update DynamicPropertyCache * Update enum and optional’s fields implementation * Update CacheTests * Update FieldsLayout implementation * Update DynamicPropertyCache implementation * Remove internal structure * Update DynamicProperty * Update _DynamicPropertyBuffer * Update OpenGraph dependency version * Fix compiler warning
- Loading branch information
Showing
13 changed files
with
353 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Sources/OpenSwiftUI/DataAndStorage/EnvironmentValues/Internal/TODO/CachedEnvironment.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
struct CachedEnvironment {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
Sources/OpenSwiftUI/DataAndStorage/ModelData/DynamicProperty/DynamicPropertyBehaviors.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// | ||
// DynamicPropertyBehaviors.swift | ||
// OpenSwiftUI | ||
// | ||
// Created by Kyle on 2024/1/6. | ||
// Lastest Version: iOS 15.5 | ||
// Status: Complete | ||
|
||
struct DynamicPropertyBehaviors: OptionSet { | ||
let rawValue: UInt32 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
Sources/OpenSwiftUI/DataAndStorage/ModelData/DynamicProperty/DynamicPropertyCache.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
// | ||
// DynamicPropertyCache.swift | ||
// OpenSwiftUI | ||
// | ||
// Created by Kyle on 2024/1/10. | ||
// Lastest Version: iOS 15.5 | ||
// Status: Complete | ||
// ID: 49D2A32E637CD497C6DE29B8E060A506 | ||
|
||
internal import OpenGraphShims | ||
|
||
// MARK: - DynamicPropertyCache | ||
|
||
struct DynamicPropertyCache { | ||
private static var cache = MutableBox([ObjectIdentifier: Fields]()) | ||
|
||
static func fields(of type: Any.Type) -> Fields { | ||
if let fields = cache.value[ObjectIdentifier(type)] { | ||
return fields | ||
} | ||
let fields: Fields | ||
let typeID = OGTypeID(type) | ||
switch typeID.kind { | ||
case .enum, .optional: | ||
var taggedFields: [TaggedFields] = [] | ||
_ = typeID.forEachField(options: [._2, ._4]) { name, offset, fieldType in | ||
var fields: [Field] = [] | ||
let tupleType = OGTupleType(fieldType) | ||
for index in tupleType.indices { | ||
guard let dynamicPropertyType = tupleType.type(at: index) as? DynamicProperty.Type else { | ||
break | ||
} | ||
let offset = tupleType.offset(at: index) | ||
let field = Field(type: dynamicPropertyType, offset: offset, name: name) | ||
fields.append(field) | ||
} | ||
if !fields.isEmpty { | ||
let taggedField = TaggedFields(tag: offset, fields: fields) | ||
taggedFields.append(taggedField) | ||
} | ||
return true | ||
} | ||
fields = Fields(layout: .sum(type, taggedFields)) | ||
case .struct, .tuple: | ||
var fieldArray: [Field] = [] | ||
_ = typeID.forEachField(options: [._2]) { name, offset, fieldType in | ||
guard let dynamicPropertyType = fieldType as? DynamicProperty.Type else { | ||
return true | ||
} | ||
let field = Field(type: dynamicPropertyType, offset: offset, name: name) | ||
fieldArray.append(field) | ||
return true | ||
} | ||
fields = Fields(layout: .product(fieldArray)) | ||
default: | ||
fields = Fields(layout: .product([])) | ||
break | ||
} | ||
if fields.behaviors.contains(.init(rawValue: 3)) { | ||
Log.runtimeIssues("%s is marked async, but contains properties that require the main thread.", [_typeName(type)]) | ||
} | ||
cache.value[ObjectIdentifier(type)] = fields | ||
return fields | ||
} | ||
} | ||
|
||
// MARK: - DynamicPropertyCache.Fields | ||
|
||
extension DynamicPropertyCache { | ||
struct Fields { | ||
var layout: Layout | ||
var behaviors: DynamicPropertyBehaviors | ||
|
||
enum Layout { | ||
case product([Field]) | ||
case sum(Any.Type, [TaggedFields]) | ||
} | ||
|
||
init(layout: Layout) { | ||
var behaviors: UInt32 = 0 | ||
switch layout { | ||
case let .product(fields): | ||
for field in fields { | ||
behaviors |= field.type._propertyBehaviors | ||
} | ||
case let .sum(_, taggedFields): | ||
for taggedField in taggedFields { | ||
for field in taggedField.fields { | ||
behaviors |= field.type._propertyBehaviors | ||
} | ||
} | ||
} | ||
self.layout = layout | ||
self.behaviors = .init(rawValue: behaviors) | ||
} | ||
} | ||
} | ||
|
||
// MARK: - DynamicPropertyCache.Field | ||
|
||
extension DynamicPropertyCache { | ||
struct Field { | ||
var type: DynamicProperty.Type | ||
var offset: Int | ||
var name: UnsafePointer<Int8>? | ||
} | ||
} | ||
|
||
// MARK: - DynamicPropertyCache.TaggedFields | ||
|
||
extension DynamicPropertyCache { | ||
struct TaggedFields { | ||
var tag: Int | ||
var fields: [Field] | ||
} | ||
} | ||
|
||
// MARK: - EmbeddedDynamicPropertyBox | ||
|
||
private struct EmbeddedDynamicPropertyBox<Value: DynamicProperty>: DynamicPropertyBox { | ||
typealias Property = Value | ||
func destroy() {} | ||
func reset() {} | ||
func update(property: inout Property, phase _: _GraphInputs.Phase) -> Bool { | ||
property.update() | ||
return false | ||
} | ||
} | ||
|
||
extension DynamicProperty { | ||
public static func _makeProperty<Value>( | ||
in buffer: inout _DynamicPropertyBuffer, | ||
container: _GraphValue<Value>, | ||
fieldOffset: Int, | ||
inputs: inout _GraphInputs | ||
) { | ||
makeEmbeddedProperties( | ||
in: &buffer, | ||
container: container, | ||
fieldOffset: fieldOffset, | ||
inputs: &inputs | ||
) | ||
buffer.append( | ||
EmbeddedDynamicPropertyBox<Self>(), | ||
fieldOffset: fieldOffset | ||
) | ||
} | ||
|
||
static func makeEmbeddedProperties<Value>( | ||
in buffer: inout _DynamicPropertyBuffer, | ||
container: _GraphValue<Value>, | ||
fieldOffset: Int, | ||
inputs: inout _GraphInputs | ||
) -> () { | ||
let fields = DynamicPropertyCache.fields(of: self) | ||
buffer.addFields( | ||
fields, | ||
container: container, | ||
inputs: &inputs, | ||
baseOffset: fieldOffset | ||
) | ||
} | ||
} |
38 changes: 0 additions & 38 deletions
38
...SwiftUI/DataAndStorage/ModelData/DynamicProperty/Internal/TODO/DynamicPropertyCache.swift
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
...es/OpenSwiftUI/DataAndStorage/ModelData/DynamicProperty/TODO/_DynamicPropertyBuffer.swift
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.