Remove second identifier in state storage

This commit is contained in:
david-swift 2024-07-10 10:27:02 +02:00
parent b8f36b7600
commit da69a573fc
3 changed files with 11 additions and 14 deletions

View File

@ -24,8 +24,8 @@ public enum StateManager {
/// Information about a piece of state. /// Information about a piece of state.
struct State { struct State {
/// The state's identifiers. /// The state's identifier.
var ids: (first: UUID, second: UUID?) var id: UUID
/// The state value. /// The state value.
var value: Any? var value: Any?
/// Whether to update in the next iteration. /// Whether to update in the next iteration.
@ -35,14 +35,13 @@ public enum StateManager {
/// - Parameter id: The identifier. /// - Parameter id: The identifier.
/// - Returns: Whether the id is contained. /// - Returns: Whether the id is contained.
func contains(id: UUID) -> Bool { func contains(id: UUID) -> Bool {
ids.first == id || ids.second == id id == self.id
} }
/// Change the identifier to a new one. /// Change the identifier to a new one.
/// - Parameter newID: The new identifier. /// - Parameter newID: The new identifier.
mutating func changeID(new newID: UUID) { mutating func changeID(new newID: UUID) {
ids.second = ids.first id = newID
ids.first = newID
} }
} }
@ -72,7 +71,7 @@ public enum StateManager {
static func setState(id: UUID, value: Any?) { static func setState(id: UUID, value: Any?) {
if saveState { if saveState {
guard let index = state.firstIndex(where: { $0.contains(id: id) }) else { guard let index = state.firstIndex(where: { $0.contains(id: id) }) else {
state.append(.init(ids: (first: id, second: nil), value: value)) state.append(.init(id: id, value: value))
return return
} }
state[safe: index]?.value = value state[safe: index]?.value = value

View File

@ -1,5 +1,5 @@
import Foundation import Foundation
import Meta @testable import Meta
import Observation import Observation
import SampleBackends import SampleBackends
@ -44,9 +44,10 @@ struct DemoView: View {
var view: Body { var view: Body {
Backend1.TestWidget1() Backend1.TestWidget1()
Backend1.Button(model.test) { Backend1.Button(model.test) {
model.test = "\(Int.random(in: 1...10))" Task {
app.addSceneElement("main") app.addSceneElement("main")
} }
}
TestView() TestView()
testContent testContent
} }
@ -66,11 +67,8 @@ struct TestView: View {
var view: Body { var view: Body {
Backend2.TestWidget4() Backend2.TestWidget4()
Backend1.Button(test) { Backend1.Button(test) {
Task {
try await Task.sleep(nanoseconds: 100_000_000)
test = "\(Int.random(in: 1...10))" test = "\(Int.random(in: 1...10))"
} }
}
Backend1.Menu("Hi") { Backend1.Menu("Hi") {
Backend1.Menu("World") { Backend1.Menu("World") {
} }

View File

@ -60,9 +60,9 @@ public enum Backend1 {
} }
public func update<Storage>(_ storage: ViewStorage, modifiers: [(any AnyView) -> any AnyView], updateProperties: Bool, type: Storage.Type) { public func update<Storage>(_ storage: ViewStorage, modifiers: [(any AnyView) -> any AnyView], updateProperties: Bool, type: Storage.Type) {
storage.fields["action"] = action
if updateProperties { if updateProperties {
print("Update button (label = \(label))") print("Update button (label = \(label))")
storage.fields["action"] = action
} else { } else {
print("Do not update button (label = \(label))") print("Do not update button (label = \(label))")
} }