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.
struct State {
/// The state's identifiers.
var ids: (first: UUID, second: UUID?)
/// The state's identifier.
var id: UUID
/// The state value.
var value: Any?
/// Whether to update in the next iteration.
@ -35,14 +35,13 @@ public enum StateManager {
/// - Parameter id: The identifier.
/// - Returns: Whether the id is contained.
func contains(id: UUID) -> Bool {
ids.first == id || ids.second == id
id == self.id
}
/// Change the identifier to a new one.
/// - Parameter newID: The new identifier.
mutating func changeID(new newID: UUID) {
ids.second = ids.first
ids.first = newID
id = newID
}
}
@ -72,7 +71,7 @@ public enum StateManager {
static func setState(id: UUID, value: Any?) {
if saveState {
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
}
state[safe: index]?.value = value

View File

@ -1,5 +1,5 @@
import Foundation
import Meta
@testable import Meta
import Observation
import SampleBackends
@ -44,8 +44,9 @@ struct DemoView: View {
var view: Body {
Backend1.TestWidget1()
Backend1.Button(model.test) {
model.test = "\(Int.random(in: 1...10))"
app.addSceneElement("main")
Task {
app.addSceneElement("main")
}
}
TestView()
testContent
@ -66,10 +67,7 @@ struct TestView: View {
var view: Body {
Backend2.TestWidget4()
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("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) {
storage.fields["action"] = action
if updateProperties {
print("Update button (label = \(label))")
storage.fields["action"] = action
} else {
print("Do not update button (label = \(label))")
}