Add main actor trampolines

This commit is contained in:
Max Goedjen
2022-06-07 22:02:49 -07:00
parent 84dd9403c3
commit 0eab79c4c4
6 changed files with 26 additions and 5 deletions

View File

@@ -0,0 +1,17 @@
import Foundation
func mainActorWrapped(_ f: @escaping @MainActor () -> Void) -> () -> Void {
return {
DispatchQueue.main.async {
f()
}
}
}
func mainActorWrapped<T: Sendable>(_ f: @escaping @MainActor (T) -> Void) -> (T) -> Void {
return { x in
DispatchQueue.main.async {
f(x)
}
}
}

View File

@@ -43,7 +43,7 @@ struct CreateSecretView<StoreType: SecretStoreModifiable>: View {
showing = false
}
.keyboardShortcut(.cancelAction)
Button("Create", action: save)
Button("Create", action: mainActorWrapped(save))
.disabled(name.isEmpty)
.keyboardShortcut(.defaultAction)
}

View File

@@ -33,7 +33,7 @@ struct DeleteSecretView<StoreType: SecretStoreModifiable>: View {
}
HStack {
Spacer()
Button("Delete", action: delete)
Button("Delete", action: mainActorWrapped(delete))
.disabled(confirm != secret.name)
.keyboardShortcut(.delete)
Button("Don't Delete") {

View File

@@ -28,7 +28,7 @@ struct RenameSecretView<StoreType: SecretStoreModifiable>: View {
}
HStack {
Spacer()
Button("Rename", action: rename)
Button("Rename", action: mainActorWrapped(rename))
.disabled(newName.count == 0)
.keyboardShortcut(.return)
Button("Cancel") {

View File

@@ -31,8 +31,8 @@ struct StoreListView: View {
store: store,
secret: secret,
activeSecret: $activeSecret,
deletedSecret: self.secretDeleted,
renamedSecret: self.secretRenamed
deletedSecret: mainActorWrapped(self.secretDeleted),
renamedSecret: mainActorWrapped(self.secretRenamed)
)
}
}