@@ -22,7 +22,7 @@ class StoredLocationBase<Value>: AnyLocation<Value>, Location {
22
22
}
23
23
}
24
24
25
- private struct BeginUpdate : GraphMutation {
25
+ fileprivate struct BeginUpdate : GraphMutation {
26
26
weak var box : StoredLocationBase < Value > ?
27
27
28
28
func apply( ) {
@@ -56,21 +56,19 @@ class StoredLocationBase<Value>: AnyLocation<Value>, Location {
56
56
super. init ( )
57
57
}
58
58
59
- private var isValid : Bool {
60
- true
61
- }
59
+ fileprivate var isValid : Bool { true }
62
60
63
61
// MARK: - abstract method
64
62
65
- private var isUpdating : Bool {
63
+ fileprivate var isUpdating : Bool {
66
64
fatalError ( " abstract " )
67
65
}
68
66
69
- private func commit( transaction: Transaction , mutation: BeginUpdate ) {
67
+ fileprivate func commit( transaction: Transaction , mutation: BeginUpdate ) {
70
68
fatalError ( " abstract " )
71
69
}
72
70
73
- private func notifyObservers( ) {
71
+ fileprivate func notifyObservers( ) {
74
72
fatalError ( " abstract " )
75
73
}
76
74
@@ -96,15 +94,26 @@ class StoredLocationBase<Value>: AnyLocation<Value>, Location {
96
94
}
97
95
return
98
96
}
99
- let _ = $data. withMutableData { data in
97
+ let shouldCommit = $data. withMutableData { data in
100
98
guard !compareValues( data. currentValue, value) else {
101
99
return false
102
100
}
103
101
data. savedValue. append ( data. currentValue)
104
102
data. currentValue = value
105
103
return true
106
104
}
107
- // TODO
105
+ guard shouldCommit else {
106
+ return
107
+ }
108
+ var newTransaction = transaction
109
+ newTransaction. override ( . current)
110
+ performOnMainThread { [ weak self] in
111
+ guard let self else {
112
+ return
113
+ }
114
+ let update = BeginUpdate ( box: self )
115
+ commit ( transaction: newTransaction, mutation: update)
116
+ }
108
117
}
109
118
110
119
override func projecting< P: Projection > ( _ projection: P ) -> AnyLocation < P . Projected > where Value == P . Base {
@@ -130,10 +139,38 @@ class StoredLocationBase<Value>: AnyLocation<Value>, Location {
130
139
131
140
private final func beginUpdate( ) {
132
141
data. savedValue. removeFirst ( )
142
+ notifyObservers ( )
133
143
}
134
144
}
135
145
136
- // TODO
137
146
final class StoredLocation < Value> : StoredLocationBase < Value > {
147
+ weak var host : GraphHost ?
148
+ @WeakAttribute var signal : Void ?
149
+
150
+ init ( initialValue value: Value , host: GraphHost ? , signal: WeakAttribute < Void > ) {
151
+ self . host = host
152
+ _signal = signal
153
+ super. init ( initialValue: value)
154
+ }
138
155
156
+ override fileprivate var isValid : Bool {
157
+ host? . isValid ?? false
158
+ }
159
+
160
+ override fileprivate var isUpdating : Bool {
161
+ host? . isUpdating ?? false
162
+ }
163
+
164
+ override fileprivate func commit( transaction: Transaction , mutation: StoredLocationBase < Value > . BeginUpdate ) {
165
+ host? . asyncTransaction (
166
+ transaction,
167
+ mutation: mutation,
168
+ style: . _1,
169
+ mayDeferUpdate: true
170
+ )
171
+ }
172
+
173
+ override fileprivate func notifyObservers( ) {
174
+ $signal? . invalidateValue ( )
175
+ }
139
176
}
0 commit comments