Async await and double conversion.

This commit is contained in:
Max Goedjen
2021-10-30 15:36:50 -07:00
parent 88b7ef82da
commit a748099270
3 changed files with 26 additions and 29 deletions

View File

@@ -6,33 +6,27 @@ import SecretKit
struct LaunchAgentController {
func install(completion: (() -> Void)? = nil) {
func install() async {
Logger().debug("Installing agent")
_ = setEnabled(false)
// This is definitely a bit of a "seems to work better" thing but:
// Seems to more reliably hit if these are on separate runloops, otherwise it seems like it sometimes doesn't kill old
// and start new?
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
_ = setEnabled(true)
completion?()
}
await Task.sleep(UInt64(Measurement(value: 0.1, unit: UnitDuration.seconds).converted(to: .nanoseconds).value))
_ = setEnabled(true)
}
func forceLaunch(completion: ((Bool) -> Void)?) {
func forceLaunch() async throws {
Logger().debug("Agent is not running, attempting to force launch")
let url = Bundle.main.bundleURL.appendingPathComponent("Contents/Library/LoginItems/SecretAgent.app")
let config = NSWorkspace.OpenConfiguration()
config.activates = false
NSWorkspace.shared.openApplication(at: url, configuration: config) { app, error in
DispatchQueue.main.async {
completion?(error == nil)
}
if let error = error {
Logger().error("Error force launching \(error.localizedDescription)")
} else {
Logger().debug("Agent force launched")
}
do {
try await NSWorkspace.shared.openApplication(at: url, configuration: config)
Logger().debug("Agent force launched")
} catch {
Logger().error("Error force launching \(error.localizedDescription)")
throw error
}
}