Skip to content

Commit

Permalink
Mark Command.run as async
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitabobko committed Sep 17, 2023
1 parent 69f0d12 commit 6f8e0d6
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 13 deletions.
14 changes: 11 additions & 3 deletions src/command/BashCommand.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
struct BashCommand: Command {
let bashCommand: String

func run() {
let process = try! Process.run(URL(filePath: "/bin/bash"), arguments: ["-c", bashCommand])
process.waitUntilExit()
func run() async {
precondition(Thread.current.isMainThread)
await withCheckedContinuation { (continuation: CheckedContinuation<(), Never>) in
let process = Process()
process.executableURL = URL(filePath: "/bin/bash")
process.arguments = ["-c", bashCommand]
process.terminationHandler = { _ in
continuation.resume()
}
try! process.run()
}
}
}
2 changes: 1 addition & 1 deletion src/command/Command.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
protocol Command {
func run()
func run() async
}
5 changes: 3 additions & 2 deletions src/command/CompositeCommand.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
struct CompositeCommand: Command {
let subCommands: [Command]

func run() {
func run() async {
precondition(Thread.current.isMainThread)
for command in subCommands {
command.run()
await command.run()
}
}
}
3 changes: 2 additions & 1 deletion src/command/FocusCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ struct FocusCommand: Command {
case parent, child, floating, tiling, toggle_tiling_floating
}

func run() {
func run() async {
precondition(Thread.current.isMainThread)
// todo
}
}
3 changes: 2 additions & 1 deletion src/command/LayoutCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ struct LayoutCommand: Command {
case floating
}

func run() {
func run() async {
precondition(Thread.current.isMainThread)
// todo
}
}
3 changes: 2 additions & 1 deletion src/command/ModeCommand.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
struct ModeCommand: Command {
let idToActivate: String

func run() {
func run() async {
precondition(Thread.current.isMainThread)
for (modeId, mode) in config.modes {
if modeId == idToActivate {
mode.activate()
Expand Down
3 changes: 2 additions & 1 deletion src/command/MoveThroughCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ struct MoveThroughCommand: Command {
case left, down, up, right
}

func run() {
func run() async {
precondition(Thread.current.isMainThread)

}
}
3 changes: 2 additions & 1 deletion src/command/ReloadConfigCommand.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
struct ReloadConfigCommand: Command {
func run() {
func run() async {
precondition(Thread.current.isMainThread)
reloadConfig()
refresh()
}
Expand Down
3 changes: 2 additions & 1 deletion src/command/WorkspaceCommand.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
struct WorkspaceCommand : Command {
let workspaceName: String

func run() {
func run() async {
precondition(Thread.current.isMainThread)
switchToWorkspace(Workspace.get(byName: workspaceName))
}
}
4 changes: 3 additions & 1 deletion src/config/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ class HotkeyBinding {
}

func activate() {
hotKey = HotKey(key: key, modifiers: modifiers, keyUpHandler: command.run)
hotKey = HotKey(key: key, modifiers: modifiers, keyUpHandler: { [self] in
Task { await command.run() }
})
}

func deactivate() {
Expand Down
1 change: 1 addition & 0 deletions src/refresh.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Foundation
/// It's one of the most important function of the whole application.
/// The function is called as a feedback response on every user input
func refresh(startSession: Bool = true, endSession: Bool = true) {
precondition(Thread.current.isMainThread)
debug("refresh (startSession=\(startSession), endSession=\(endSession)) \(Date.now.formatted(date: .abbreviated, time: .standard))")
if startSession {
NSWorkspace.activeApp = nil
Expand Down

0 comments on commit 6f8e0d6

Please sign in to comment.