Skip to content

Commit

Permalink
Update HostPreferenceKey call
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Apr 1, 2024
1 parent 146ffcf commit ee5fdc5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
12 changes: 11 additions & 1 deletion Sources/OpenSwiftUI/Data/Preference/HostPreferencesKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@
// ID: 7429200566949B8FB892A77E01A988C8

struct HostPreferencesKey: PreferenceKey {
private static var nodeId: UInt32 = .zero
static var defaultValue: PreferenceList {
PreferenceList()
}

static func reduce(value: inout PreferenceList, nextValue: () -> PreferenceList) {
value.merge(nextValue())
}
}

extension HostPreferencesKey {
private static var nodeId: UInt32 = .zero

@inline(__always)
static func makeNodeID() -> UInt32 {
defer { nodeId &+= 1 }
Expand Down
56 changes: 25 additions & 31 deletions Sources/OpenSwiftUI/Data/Preference/PreferenceList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,31 @@ struct PreferenceList: CustomStringConvertible {
description.append("]")
return description
}

@inline(__always)
mutating func merge(_ other: PreferenceList) {
guard let otherFirst = other.first else {
return
}
guard let selfFirst = first else {
first = otherFirst
return
}
first = nil
selfFirst.forEach { node in
if let mergedNode = node.combine(from: otherFirst, next: first) {
first = mergedNode
} else {
first = node.copy(next: first)
}
}
otherFirst.forEach { node in
guard node.find(from: selfFirst) == nil else {
return
}
first = node.copy(next: first)
}
}
}

extension PreferenceList {
Expand Down Expand Up @@ -175,34 +200,3 @@ private class _PreferenceNode<Key: PreferenceKey>: PreferenceNode {
"\(Key.self) = \(value)"
}
}

extension HostPreferencesKey {
static var defaultValue: PreferenceList {
PreferenceList()
}

static func reduce(value: inout PreferenceList, nextValue: () -> PreferenceList) {
let newValue = nextValue()
guard let newFirst = newValue.first else {
return
}
guard let first = value.first else {
value.first = newFirst
return
}
value.first = nil
first.forEach { node in
if let mergedNode = node.combine(from: newFirst, next: value.first) {
value.first = mergedNode
} else {
value.first = node.copy(next: value.first)
}
}
newFirst.forEach { node in
guard node.find(from: first) == nil else {
return
}
value.first = node.copy(next: value.first)
}
}
}

0 comments on commit ee5fdc5

Please sign in to comment.