Skip to content

Commit cfb94e2

Browse files
committed
Update StoredLocationBase implementation
1 parent 5e801db commit cfb94e2

File tree

4 files changed

+78
-7
lines changed

4 files changed

+78
-7
lines changed

Package.resolved

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/OpenSwiftUI/Core/Data/Location/AnyLocation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class AnyLocation<Value>: AnyLocationBase {
1616
}
1717
func get() -> Value { fatalError() }
1818
func set(_ value: Value, transaction: Transaction) { fatalError() }
19-
func projecting<P>(_ p: P) -> AnyLocation<P.Projected> where P: Projection, P.Base == Value {
19+
func projecting<P: Projection>(_ p: P) -> AnyLocation<P.Projected> where Value == P.Base {
2020
fatalError()
2121
}
2222
func update() -> (Value, Bool) { fatalError() }

Sources/OpenSwiftUI/Core/Data/Location/LocationBox.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class LocationBox<L: Location>: AnyLocation<L.Value> {
2727
location.set(value, transaction: transaction)
2828
}
2929

30-
override func projecting<P>(_ p: P) -> AnyLocation<P.Projected> where L.Value == P.Base, P : Projection {
31-
cache.reference(for: p, on: location)
30+
override func projecting<P: Projection>(_ projection: P) -> AnyLocation<P.Projected> where L.Value == P.Base {
31+
cache.reference(for: projection, on: location)
3232
}
3333

3434
override func update() -> (L.Value, Bool) {

Sources/OpenSwiftUI/Data/Model/State/StoredLocation.swift

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
// Status: WIP
77
// ID: EBDC911C9EE054BAE3D86F947C24B7C3
88

9-
class StoredLocationBase<Value>: AnyLocation<Value> {
9+
internal import OpenGraphShims
10+
internal import COpenSwiftUI
11+
12+
class StoredLocationBase<Value>: AnyLocation<Value>, Location {
1013
private struct LockedData {
1114
var currentValue: Value
1215
var savedValue: [Value]
@@ -53,12 +56,80 @@ class StoredLocationBase<Value>: AnyLocation<Value> {
5356
super.init()
5457
}
5558

59+
private var isValid: Bool {
60+
true
61+
}
62+
63+
// MARK: - abstract method
64+
65+
private var isUpdating: Bool {
66+
fatalError("abstract")
67+
}
68+
69+
private func commit(transaction: Transaction, mutation: BeginUpdate) {
70+
fatalError("abstract")
71+
}
72+
73+
private func notifyObservers() {
74+
fatalError("abstract")
75+
}
76+
77+
// MARK: - AnyLocation
78+
79+
override var wasRead: Bool {
80+
get { _wasRead }
81+
set { _wasRead = newValue }
82+
}
83+
84+
override func get() -> Value {
85+
data.currentValue
86+
}
87+
88+
override func set(_ value: Value, transaction: Transaction) {
89+
guard !isUpdating else {
90+
Log.runtimeIssues("Modifying state during view update, this will cause undefined behavior.")
91+
return
92+
}
93+
guard isValid else {
94+
$data.withMutableData { data in
95+
data.savedValue.removeAll()
96+
}
97+
return
98+
}
99+
let _ = $data.withMutableData { data in
100+
guard !compareValues(data.currentValue, value) else {
101+
return false
102+
}
103+
data.savedValue.append(data.currentValue)
104+
data.currentValue = value
105+
return true
106+
}
107+
// TODO
108+
}
109+
110+
override func projecting<P: Projection>(_ projection: P) -> AnyLocation<P.Projected> where Value == P.Base {
111+
data.cache.reference(for: projection, on: self)
112+
}
113+
114+
override func update() -> (Value, Bool) {
115+
_wasRead = true
116+
return (updateValue, true)
117+
}
118+
119+
// MARK: - final properties and methods
120+
56121
deinit {
57122
$data.destroy()
58123
}
59124

60-
private func beginUpdate() {
61-
// TODO
125+
final var updateValue: Value {
126+
$data.withMutableData { data in
127+
data.savedValue.first ?? data.currentValue
128+
}
129+
}
130+
131+
private final func beginUpdate() {
132+
data.savedValue.removeFirst()
62133
}
63134
}
64135

0 commit comments

Comments
 (0)