From a8008f786ed4f919e8923007f124b56a6e779019 Mon Sep 17 00:00:00 2001 From: mishamyrt Date: Sat, 13 Jul 2024 16:02:56 +0300 Subject: [PATCH] refactor: clean up action runner --- Sources/Runner/ActionQueue.swift | 13 +++++++------ Sources/Runner/ActionRunner.swift | 13 ++++--------- testdata/basic.yaml | 4 ++-- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/Sources/Runner/ActionQueue.swift b/Sources/Runner/ActionQueue.swift index 1847043..9bf4643 100644 --- a/Sources/Runner/ActionQueue.swift +++ b/Sources/Runner/ActionQueue.swift @@ -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 } @@ -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) { diff --git a/Sources/Runner/ActionRunner.swift b/Sources/Runner/ActionRunner.swift index 070926f..ed73ca7 100644 --- a/Sources/Runner/ActionRunner.swift +++ b/Sources/Runner/ActionRunner.swift @@ -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, diff --git a/testdata/basic.yaml b/testdata/basic.yaml index ced223d..a84df50 100644 --- a/testdata/basic.yaml +++ b/testdata/basic.yaml @@ -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"