Make appear observer and inspector wrapper async

This commit is contained in:
david-swift 2024-09-19 13:07:44 +02:00
parent 4b2127c55c
commit 1a5cc43ef1
2 changed files with 11 additions and 11 deletions

View File

@ -9,7 +9,7 @@
struct AppearObserver: ConvenienceWidget { struct AppearObserver: ConvenienceWidget {
/// The custom code to edit the widget. /// The custom code to edit the widget.
var modify: @Sendable (ViewStorage) -> Void var modify: @Sendable (ViewStorage) async -> Void
/// The wrapped view. /// The wrapped view.
var content: AnyView var content: AnyView
@ -23,7 +23,7 @@ struct AppearObserver: ConvenienceWidget {
type: Data.Type type: Data.Type
) async -> ViewStorage where Data: ViewRenderData { ) async -> ViewStorage where Data: ViewRenderData {
let storage = await content.storage(data: data, type: type) let storage = await content.storage(data: data, type: type)
modify(storage) await modify(storage)
return storage return storage
} }
@ -50,15 +50,15 @@ extension AnyView {
/// Run a function on the widget when it appears for the first time. /// Run a function on the widget when it appears for the first time.
/// - Parameter closure: The function. /// - Parameter closure: The function.
/// - Returns: A view. /// - Returns: A view.
public func inspectOnAppear(_ closure: @Sendable @escaping (ViewStorage) -> Void) -> AnyView { public func inspectOnAppear(_ closure: @Sendable @escaping (ViewStorage) async -> Void) -> AnyView {
AppearObserver(modify: closure, content: self) AppearObserver(modify: closure, content: self)
} }
/// Run a function when the view appears for the first time. /// Run a function when the view appears for the first time.
/// - Parameter closure: The function. /// - Parameter closure: The function.
/// - Returns: A view. /// - Returns: A view.
public func onAppear(_ closure: @Sendable @escaping () -> Void) -> AnyView { public func onAppear(_ closure: @Sendable @escaping () async -> Void) -> AnyView {
inspectOnAppear { _ in closure() } inspectOnAppear { _ in await closure() }
} }
} }

View File

@ -9,7 +9,7 @@
struct InspectorWrapper: ConvenienceWidget { struct InspectorWrapper: ConvenienceWidget {
/// The custom code to edit the widget. /// The custom code to edit the widget.
var modify: @Sendable (ViewStorage, Bool) -> Void var modify: @Sendable (ViewStorage, Bool) async -> Void
/// The wrapped view. /// The wrapped view.
var content: AnyView var content: AnyView
@ -23,7 +23,7 @@ struct InspectorWrapper: ConvenienceWidget {
type: Data.Type type: Data.Type
) async -> ViewStorage where Data: ViewRenderData { ) async -> ViewStorage where Data: ViewRenderData {
let storage = await content.storage(data: data, type: type) let storage = await content.storage(data: data, type: type)
modify(storage, true) await modify(storage, true)
return storage return storage
} }
@ -40,7 +40,7 @@ struct InspectorWrapper: ConvenienceWidget {
type: Data.Type type: Data.Type
) async where Data: ViewRenderData { ) async where Data: ViewRenderData {
await content.updateStorage(storage, data: data, updateProperties: updateProperties, type: type) await content.updateStorage(storage, data: data, updateProperties: updateProperties, type: type)
modify(storage, updateProperties) await modify(storage, updateProperties)
} }
} }
@ -51,15 +51,15 @@ extension AnyView {
/// Run a custom code accessing the view's storage when initializing and updating the view. /// Run a custom code accessing the view's storage when initializing and updating the view.
/// - Parameter modify: Modify the storage. The boolean indicates whether state in parent views changed. /// - Parameter modify: Modify the storage. The boolean indicates whether state in parent views changed.
/// - Returns: A view. /// - Returns: A view.
public func inspect(_ modify: @Sendable @escaping (ViewStorage, Bool) -> Void) -> AnyView { public func inspect(_ modify: @Sendable @escaping (ViewStorage, Bool) async -> Void) -> AnyView {
InspectorWrapper(modify: modify, content: self) InspectorWrapper(modify: modify, content: self)
} }
/// Run a function when the view gets updated. /// Run a function when the view gets updated.
/// - Parameter onUpdate: The function. /// - Parameter onUpdate: The function.
/// - Returns: A view. /// - Returns: A view.
public func onUpdate(_ onUpdate: @Sendable @escaping () -> Void) -> AnyView { public func onUpdate(_ onUpdate: @Sendable @escaping () async -> Void) -> AnyView {
inspect { _, _ in onUpdate() } inspect { _, _ in await onUpdate() }
} }
} }