From 80c430c546a5fee59e69db14fd021f11a193751e Mon Sep 17 00:00:00 2001 From: david-swift Date: Thu, 19 Sep 2024 22:32:53 +0200 Subject: [PATCH] Fix signal for asynchronous updates --- Sources/Model/Data Flow/Signal.swift | 12 +++++++++--- Sources/View/StateWrapper.swift | 6 ++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Sources/Model/Data Flow/Signal.swift b/Sources/Model/Data Flow/Signal.swift index 4a1b184..e5e154c 100644 --- a/Sources/Model/Data Flow/Signal.swift +++ b/Sources/Model/Data Flow/Signal.swift @@ -8,12 +8,14 @@ import Foundation /// A type that signalizes an action. -public struct Signal: Sendable { +public struct Signal: Model, Sendable { /// An action is signalized by toggling a boolean to `true` and back to `false`. - @State var boolean = false + var boolean = false /// A signal has a unique identifier. public let id: UUID = .init() + /// The model data. + public var model: ModelData? /// Whether the action has caused an update. public var update: Bool { boolean } @@ -23,7 +25,11 @@ public struct Signal: Sendable { /// Activate a signal. public func signal() { - boolean = true + setModel { $0.boolean = true } + } + + /// Destroy a signal. + mutating func destroySignal() { boolean = false } diff --git a/Sources/View/StateWrapper.swift b/Sources/View/StateWrapper.swift index 91573ff..fae0b2d 100644 --- a/Sources/View/StateWrapper.swift +++ b/Sources/View/StateWrapper.swift @@ -55,6 +55,12 @@ struct StateWrapper: ConvenienceWidget { return } await content().updateStorage(storage, data: data, updateProperties: updateProperties, type: type) + for property in state { + if var value = property.value.content.value as? Signal, value.update { + value.destroySignal() + property.value.content.value = value + } + } } /// Get a view storage.