Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Make appear observer and inspector wrapper async
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Sep 19, 2024
1 parent 4b2127c commit 1a5cc43
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions Sources/View/AppearObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
struct AppearObserver: ConvenienceWidget {

/// The custom code to edit the widget.
var modify: @Sendable (ViewStorage) -> Void
var modify: @Sendable (ViewStorage) async -> Void
/// The wrapped view.
var content: AnyView

Expand All @@ -23,7 +23,7 @@ struct AppearObserver: ConvenienceWidget {
type: Data.Type
) async -> ViewStorage where Data: ViewRenderData {
let storage = await content.storage(data: data, type: type)
modify(storage)
await modify(storage)
return storage
}

Expand All @@ -50,15 +50,15 @@ extension AnyView {
/// Run a function on the widget when it appears for the first time.
/// - Parameter closure: The function.
/// - 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)
}

/// Run a function when the view appears for the first time.
/// - Parameter closure: The function.
/// - Returns: A view.
public func onAppear(_ closure: @Sendable @escaping () -> Void) -> AnyView {
inspectOnAppear { _ in closure() }
public func onAppear(_ closure: @Sendable @escaping () async -> Void) -> AnyView {
inspectOnAppear { _ in await closure() }
}

}
12 changes: 6 additions & 6 deletions Sources/View/InspectorWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
struct InspectorWrapper: ConvenienceWidget {

/// The custom code to edit the widget.
var modify: @Sendable (ViewStorage, Bool) -> Void
var modify: @Sendable (ViewStorage, Bool) async -> Void
/// The wrapped view.
var content: AnyView

Expand All @@ -23,7 +23,7 @@ struct InspectorWrapper: ConvenienceWidget {
type: Data.Type
) async -> ViewStorage where Data: ViewRenderData {
let storage = await content.storage(data: data, type: type)
modify(storage, true)
await modify(storage, true)
return storage
}

Expand All @@ -40,7 +40,7 @@ struct InspectorWrapper: ConvenienceWidget {
type: Data.Type
) async where Data: ViewRenderData {
await content.updateStorage(storage, data: data, updateProperties: updateProperties, type: type)
modify(storage, updateProperties)
await modify(storage, updateProperties)
}

}
Expand All @@ -51,15 +51,15 @@ extension AnyView {
/// 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.
/// - 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)
}

/// Run a function when the view gets updated.
/// - Parameter onUpdate: The function.
/// - Returns: A view.
public func onUpdate(_ onUpdate: @Sendable @escaping () -> Void) -> AnyView {
inspect { _, _ in onUpdate() }
public func onUpdate(_ onUpdate: @Sendable @escaping () async -> Void) -> AnyView {
inspect { _, _ in await onUpdate() }
}

}

0 comments on commit 1a5cc43

Please sign in to comment.