Skip to content

Commit

Permalink
refactor: clean up action runner
Browse files Browse the repository at this point in the history
  • Loading branch information
mishamyrt committed Jul 13, 2024
1 parent f1039f4 commit a8008f7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
13 changes: 7 additions & 6 deletions Sources/Runner/ActionQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ActionQueue {
var isRunning = false
var interval: TimeInterval?

init(forGroup name: String, after interval: TimeInterval?) {
init(name: String, interval: TimeInterval? = nil) {
self.name = name
self.interval = interval
}
Expand All @@ -17,23 +17,24 @@ class ActionQueue {
return
}
if isRunning {
logger.info("debounced action \(formatMessage(action))")
logger.debug("debounced action \(formatMessage(action))")
return
}
isRunning = true
logger.debug("lock \(self.name.magenta) queue")
self.runAction(action)
DispatchQueue.main.asyncAfter(deadline: .now() + debounceInterval) {
logger.debug("unlock \(self.name.magenta)")
logger.debug("unlock \(self.name.magenta) queue")
self.isRunning = false
}
}

private func formatMessage(_ action: Action) -> String {
let result = "\(action.source.cyan):\(action.kind.blue)"
let event = "\(action.source.cyan):\(action.kind.blue)"
guard let target = action.target else {
return result
return event
}
return "on \(name.magenta) - \(result) with \(target.yellow)"
return "on \(name.magenta) - \(event) with \(target.yellow)"
}

private func runAction(_ action: Action) {
Expand Down
13 changes: 4 additions & 9 deletions Sources/Runner/ActionRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@ class ActionRunner: EventListener {
init(with handler: ConfigHandler) {
self.handler = handler
for (name, group) in handler.groupMap {
queues[name] = ActionQueue(forGroup: name, after: group.debounce)
queues[name] = ActionQueue(
name: name,
interval: group.debounce
)
}
}

func format(handler: Action) -> String {
let result = "\(handler.source.cyan):\(handler.kind.blue)"
guard let target = handler.target else {
return result
}
return result + " with \(target.yellow)"
}

func handle(_ event: Event) {
guard let action = handler.findAction(
source: event.source,
Expand Down
4 changes: 2 additions & 2 deletions testdata/basic.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
actions:
- on: app:activated
with: com.google.Chrome
run: echo 1
run: echo "chrome"
- on: app:activated
with: com.microsoft.VSCode
run: echo 2
run: echo "vscode"

0 comments on commit a8008f7

Please sign in to comment.