Revise preference implementation

This commit is contained in:
Carson Katri 2022-07-05 16:31:00 -04:00
parent 46f5db40f0
commit 11c7a98e87
4 changed files with 16 additions and 15 deletions

View File

@ -179,13 +179,12 @@ struct ReconcilePass: FiberReconcilerPass {
}
if let preferences = node.fiber?.preferences {
node.fiber?.outputs.transformPreferences?(preferences)
if let parentPreferences = node.fiber?.preferenceParent?.preferences {
parentPreferences.merge(preferences)
}
if let action = node.fiber?.outputs.preferenceAction {
action(preferences)
}
if let parentPreferences = node.fiber?.preferenceParent?.preferences {
parentPreferences.merge(preferences)
}
}
var alternateSibling = node.fiber?.alternate?.sibling

View File

@ -79,9 +79,9 @@ public extension _PreferenceValue {
public final class _PreferenceStore: CustomDebugStringConvertible {
/// The values of the `_PreferenceStore` on the last update.
private var previousValues: [String: _PreferenceValueStorage]
private var previousValues: [ObjectIdentifier: _PreferenceValueStorage]
/// The backing values of the `_PreferenceStore`.
private var values: [String: _PreferenceValueStorage]
private var values: [ObjectIdentifier: _PreferenceValueStorage]
weak var parent: _PreferenceStore?
@ -89,7 +89,7 @@ public final class _PreferenceStore: CustomDebugStringConvertible {
"Preferences (\(ObjectIdentifier(self))): \(values)"
}
init(values: [String: _PreferenceValueStorage] = [:]) {
init(values: [ObjectIdentifier: _PreferenceValueStorage] = [:]) {
previousValues = [:]
self.values = values
}
@ -98,12 +98,13 @@ public final class _PreferenceStore: CustomDebugStringConvertible {
public func value<Key>(forKey key: Key.Type = Key.self) -> _PreferenceValue<Key>
where Key: PreferenceKey
{
let keyID = ObjectIdentifier(key)
let storage: _PreferenceValueStorage
if let existing = values[String(reflecting: key)] {
if let existing = values[keyID] {
storage = existing
} else {
storage = .init(key)
values[String(reflecting: key)] = storage
values[keyID] = storage
}
return _PreferenceValue(storage: storage)
}
@ -114,16 +115,17 @@ public final class _PreferenceStore: CustomDebugStringConvertible {
func previousValue<Key>(forKey key: Key.Type = Key.self) -> _PreferenceValue<Key>
where Key: PreferenceKey
{
_PreferenceValue(storage: previousValues[String(reflecting: key)] ?? .init(key))
_PreferenceValue(storage: previousValues[ObjectIdentifier(key)] ?? .init(key))
}
public func insert<Key>(_ value: Key.Value, forKey key: Key.Type = Key.self)
where Key: PreferenceKey
{
if !values.keys.contains(String(reflecting: key)) {
values[String(reflecting: key)] = .init(key)
let keyID = ObjectIdentifier(key)
if !values.keys.contains(keyID) {
values[keyID] = .init(key)
}
values[String(reflecting: key)]?.valueList.append(value)
values[keyID]?.valueList.append(value)
parent?.insert(value, forKey: key)
}

View File

@ -39,7 +39,7 @@ public struct _PreferenceTransformModifier<Key>: _PreferenceWritingModifierProto
.init(
inputs: inputs,
preferenceStore: inputs.preferenceStore ?? .init(),
transformPreferences: {
preferenceAction: {
var value = $0.value(forKey: Key.self).value
inputs.content.transform(&value)
$0.insert(value, forKey: Key.self)

View File

@ -32,7 +32,7 @@ public struct _PreferenceWritingModifier<Key>: _PreferenceWritingModifierProtoco
.init(
inputs: inputs,
preferenceStore: inputs.preferenceStore ?? .init(),
transformPreferences: { $0.insert(inputs.content.value, forKey: Key.self) }
preferenceAction: { $0.insert(inputs.content.value, forKey: Key.self) }
)
}
}