|
| 1 | +// |
| 2 | +// StoredLocation.swift |
| 3 | +// OpenSwiftUI |
| 4 | +// |
| 5 | +// Audited for RELEASE_2021 |
| 6 | +// Status: WIP |
| 7 | +// ID: EBDC911C9EE054BAE3D86F947C24B7C3 |
| 8 | + |
| 9 | +internal import OpenGraphShims |
| 10 | +internal import COpenSwiftUI |
| 11 | + |
| 12 | +class StoredLocationBase<Value>: AnyLocation<Value>, Location { |
| 13 | + private struct LockedData { |
| 14 | + var currentValue: Value |
| 15 | + var savedValue: [Value] |
| 16 | + var cache: LocationProjectionCache |
| 17 | + |
| 18 | + init(currentValue: Value, savedValue: [Value], cache: LocationProjectionCache = LocationProjectionCache()) { |
| 19 | + self.currentValue = currentValue |
| 20 | + self.savedValue = savedValue |
| 21 | + self.cache = cache |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + fileprivate struct BeginUpdate: GraphMutation { |
| 26 | + weak var box: StoredLocationBase<Value>? |
| 27 | + |
| 28 | + func apply() { |
| 29 | + box?.beginUpdate() |
| 30 | + } |
| 31 | + |
| 32 | + func combine<Mutation: GraphMutation>(with mutation: Mutation) -> Bool { |
| 33 | + guard let otherBeginUpdate = mutation as? BeginUpdate, |
| 34 | + let box, |
| 35 | + let otherBox = otherBeginUpdate.box, |
| 36 | + box === otherBox |
| 37 | + else { |
| 38 | + return false |
| 39 | + } |
| 40 | + |
| 41 | + box.$data.withMutableData { data in |
| 42 | + _ = data.savedValue.removeFirst() |
| 43 | + } |
| 44 | + return true |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @UnsafeLockedPointer |
| 49 | + private var data: LockedData |
| 50 | + |
| 51 | + var _wasRead: Bool |
| 52 | + |
| 53 | + init(initialValue value: Value) { |
| 54 | + _wasRead = false |
| 55 | + _data = UnsafeLockedPointer(wrappedValue: LockedData(currentValue: value, savedValue: [])) |
| 56 | + super.init() |
| 57 | + } |
| 58 | + |
| 59 | + fileprivate var isValid: Bool { true } |
| 60 | + |
| 61 | + // MARK: - abstract method |
| 62 | + |
| 63 | + fileprivate var isUpdating: Bool { |
| 64 | + fatalError("abstract") |
| 65 | + } |
| 66 | + |
| 67 | + fileprivate func commit(transaction: Transaction, mutation: BeginUpdate) { |
| 68 | + fatalError("abstract") |
| 69 | + } |
| 70 | + |
| 71 | + fileprivate func notifyObservers() { |
| 72 | + fatalError("abstract") |
| 73 | + } |
| 74 | + |
| 75 | + // MARK: - AnyLocation |
| 76 | + |
| 77 | + override var wasRead: Bool { |
| 78 | + get { _wasRead } |
| 79 | + set { _wasRead = newValue } |
| 80 | + } |
| 81 | + |
| 82 | + override func get() -> Value { |
| 83 | + data.currentValue |
| 84 | + } |
| 85 | + |
| 86 | + override func set(_ value: Value, transaction: Transaction) { |
| 87 | + guard !isUpdating else { |
| 88 | + Log.runtimeIssues("Modifying state during view update, this will cause undefined behavior.") |
| 89 | + return |
| 90 | + } |
| 91 | + guard isValid else { |
| 92 | + $data.withMutableData { data in |
| 93 | + data.savedValue.removeAll() |
| 94 | + } |
| 95 | + return |
| 96 | + } |
| 97 | + let shouldCommit = $data.withMutableData { data in |
| 98 | + guard !compareValues(data.currentValue, value) else { |
| 99 | + return false |
| 100 | + } |
| 101 | + data.savedValue.append(data.currentValue) |
| 102 | + data.currentValue = value |
| 103 | + return true |
| 104 | + } |
| 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 | + } |
| 117 | + } |
| 118 | + |
| 119 | + override func projecting<P: Projection>(_ projection: P) -> AnyLocation<P.Projected> where Value == P.Base { |
| 120 | + data.cache.reference(for: projection, on: self) |
| 121 | + } |
| 122 | + |
| 123 | + override func update() -> (Value, Bool) { |
| 124 | + _wasRead = true |
| 125 | + return (updateValue, true) |
| 126 | + } |
| 127 | + |
| 128 | + // MARK: - final properties and methods |
| 129 | + |
| 130 | + deinit { |
| 131 | + $data.destroy() |
| 132 | + } |
| 133 | + |
| 134 | + final var updateValue: Value { |
| 135 | + $data.withMutableData { data in |
| 136 | + data.savedValue.first ?? data.currentValue |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private final func beginUpdate() { |
| 141 | + data.savedValue.removeFirst() |
| 142 | + notifyObservers() |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 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 | + } |
| 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 | + } |
| 176 | +} |
0 commit comments