Add main actor trampolines

This commit is contained in:
Max Goedjen 2022-06-07 22:02:49 -07:00
parent 84dd9403c3
commit 0eab79c4c4
No known key found for this signature in database
GPG Key ID: E58C21DD77B9B8E8
6 changed files with 26 additions and 5 deletions

View File

@ -50,6 +50,7 @@
50B8550D24138C4F009958AC /* DeleteSecretView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50B8550C24138C4F009958AC /* DeleteSecretView.swift */; };
50BB046B2418AAAE00D6E079 /* EmptyStoreView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50BB046A2418AAAE00D6E079 /* EmptyStoreView.swift */; };
50C385A52407A76D00AF2719 /* SecretDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50C385A42407A76D00AF2719 /* SecretDetailView.swift */; };
50C511B0285064DB00704B27 /* MainActorWrappers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50C511AF285064DB00704B27 /* MainActorWrappers.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -147,6 +148,7 @@
50B8550C24138C4F009958AC /* DeleteSecretView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeleteSecretView.swift; sourceTree = "<group>"; };
50BB046A2418AAAE00D6E079 /* EmptyStoreView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyStoreView.swift; sourceTree = "<group>"; };
50C385A42407A76D00AF2719 /* SecretDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecretDetailView.swift; sourceTree = "<group>"; };
50C511AF285064DB00704B27 /* MainActorWrappers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainActorWrappers.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -187,6 +189,7 @@
isa = PBXGroup;
children = (
50033AC227813F1700253856 /* BundleIDs.swift */,
50C511AF285064DB00704B27 /* MainActorWrappers.swift */,
);
path = Helpers;
sourceTree = "<group>";
@ -479,6 +482,7 @@
50571E0324393C2600F76F6C /* JustUpdatedChecker.swift in Sources */,
5079BA0F250F29BF00EA86F4 /* StoreListView.swift in Sources */,
50617DD223FCEFA90099B055 /* PreviewStore.swift in Sources */,
50C511B0285064DB00704B27 /* MainActorWrappers.swift in Sources */,
5066A6F7251829B1004B5A36 /* ShellConfigurationController.swift in Sources */,
50033AC327813F1700253856 /* BundleIDs.swift in Sources */,
508A58B3241ED2180069DC07 /* AgentStatusChecker.swift in Sources */,

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)
)
}
}