-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ObservableState.swift
197 lines (170 loc) · 5.17 KB
/
ObservableState.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#if canImport(Perception)
import Foundation
/// A type that emits notifications to observers when underlying data changes.
///
/// Conforming to this protocol signals to other APIs that the value type supports observation.
/// However, applying the ``ObservableState`` protocol by itself to a type doesn’t add observation
/// functionality to the type. Instead, always use the ``ObservableState()`` macro when adding
/// observation support to a type.
#if !os(visionOS)
public protocol ObservableState: Perceptible {
var _$id: ObservableStateID { get }
mutating func _$willModify()
}
#else
public protocol ObservableState: Observable {
var _$id: ObservableStateID { get }
mutating func _$willModify()
}
#endif
/// A unique identifier for a observed value.
public struct ObservableStateID: Equatable, Hashable, Sendable {
@usableFromInline
var location: UUID {
get { self.storage.id.location }
set {
if !isKnownUniquelyReferenced(&self.storage) {
self.storage = Storage(id: self.storage.id)
}
self.storage.id.location = newValue
}
}
private var storage: Storage
private init(storage: Storage) {
self.storage = storage
}
public init() {
self.init(storage: Storage(id: .location(UUID())))
}
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.storage === rhs.storage || lhs.storage.id == rhs.storage.id
}
public func hash(into hasher: inout Hasher) {
hasher.combine(self.storage.id)
}
@inlinable
public static func _$id<T>(for value: T) -> Self {
(value as? any ObservableState)?._$id ?? Self()
}
@inlinable
public static func _$id(for value: some ObservableState) -> Self {
value._$id
}
public func _$tag(_ tag: Int) -> Self {
Self(storage: Storage(id: .tag(tag, self.storage.id)))
}
@inlinable
public mutating func _$willModify() {
self.location = UUID()
}
private final class Storage: @unchecked Sendable {
fileprivate var id: ID
init(id: ID = .location(UUID())) {
self.id = id
}
enum ID: Equatable, Hashable, Sendable {
case location(UUID)
indirect case tag(Int, ID)
var location: UUID {
get {
switch self {
case let .location(location):
return location
case let .tag(_, id):
return id.location
}
}
set {
switch self {
case .location:
self = .location(newValue)
case .tag(let tag, var id):
id.location = newValue
self = .tag(tag, id)
}
}
}
}
}
}
@inlinable
public func _$isIdentityEqual<T: ObservableState>(
_ lhs: T, _ rhs: T
) -> Bool {
lhs._$id == rhs._$id
}
@inlinable
public func _$isIdentityEqual<ID: Hashable, T: ObservableState>(
_ lhs: IdentifiedArray<ID, T>,
_ rhs: IdentifiedArray<ID, T>
) -> Bool {
areOrderedSetsDuplicates(lhs.ids, rhs.ids)
}
@inlinable
public func _$isIdentityEqual<T: ObservableState>(
_ lhs: PresentationState<T>,
_ rhs: PresentationState<T>
) -> Bool {
lhs.wrappedValue?._$id == rhs.wrappedValue?._$id
}
@inlinable
public func _$isIdentityEqual<T: ObservableState>(
_ lhs: StackState<T>,
_ rhs: StackState<T>
) -> Bool {
areOrderedSetsDuplicates(lhs.ids, rhs.ids)
}
@inlinable
public func _$isIdentityEqual<C: Collection>(
_ lhs: C,
_ rhs: C
) -> Bool
where C.Element: ObservableState {
lhs.count == rhs.count && zip(lhs, rhs).allSatisfy { $0._$id == $1._$id }
}
// NB: This is a fast path so that String is not checked as a collection.
@inlinable
public func _$isIdentityEqual(_ lhs: String, _ rhs: String) -> Bool {
false
}
@inlinable
public func _$isIdentityEqual<T>(_ lhs: T, _ rhs: T) -> Bool {
guard !_isPOD(T.self) else { return false }
func openCollection<C: Collection>(_ lhs: C, _ rhs: Any) -> Bool {
guard C.Element.self is ObservableState.Type else {
return false
}
func openIdentifiable<Element: Identifiable>(_: Element.Type) -> Bool? {
guard
let lhs = lhs as? IdentifiedArrayOf<Element>,
let rhs = rhs as? IdentifiedArrayOf<Element>
else {
return nil
}
return areOrderedSetsDuplicates(lhs.ids, rhs.ids)
}
if let identifiable = C.Element.self as? any Identifiable.Type,
let result = openIdentifiable(identifiable)
{
return result
} else if let rhs = rhs as? C {
return lhs.count == rhs.count && zip(lhs, rhs).allSatisfy(_$isIdentityEqual)
} else {
return false
}
}
if let lhs = lhs as? any ObservableState, let rhs = rhs as? any ObservableState {
return lhs._$id == rhs._$id
} else if let lhs = lhs as? any Collection {
return openCollection(lhs, rhs)
} else {
return false
}
}
@inlinable
public func _$willModify<T>(_: inout T) {}
@inlinable
public func _$willModify<T: ObservableState>(_ value: inout T) {
value._$willModify()
}
#endif