diff options
| author | Craig Jennings <c@cjennings.net> | 2026-04-22 06:24:56 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-04-22 06:24:56 -0500 |
| commit | c0a96481c78b790666fe2d6b1a7fa9b652f02343 (patch) | |
| tree | 8eb93908c68bf0f38658d06f6a29ee420df084f9 | |
| parent | b5fe103e6c447d1293233e15250d585d524ecc68 (diff) | |
| download | chime-c0a96481c78b790666fe2d6b1a7fa9b652f02343.tar.gz chime-c0a96481c78b790666fe2d6b1a7fa9b652f02343.zip | |
refactor: extract async result helpers from chime--fetch-and-process
chime--fetch-and-process inlined two near-identical 7-line failure-handling blocks inside the async callback. They differed only in the log prefix ("Async error" for errors surfaced by the async process, "Error processing events" for errors thrown by the callback). Lift the shared body into chime--record-async-failure (err prefix) so the sequence lives in one place, and lift the success path into chime--handle-async-success (callback events) so the callback lambda becomes pure dispatch.
The outer function drops from 44 lines to 22.
| -rw-r--r-- | chime.el | 53 |
1 files changed, 26 insertions, 27 deletions
@@ -1744,6 +1744,27 @@ reaches `chime-max-consecutive-failures'. Only warns once at the threshold." chime--consecutive-async-failures) :warning))) +(defun chime--record-async-failure (err prefix) + "Record an async failure ERR. PREFIX names the failure category in the log. +Increments the consecutive-failure counter, sends a debug log when the +debug module is loaded, writes a silent log line, may emit the +persistent-failure warning, and switches the modeline to its error state." + (cl-incf chime--consecutive-async-failures) + (when (featurep 'chime-debug) + (chime--debug-log-async-error err)) + (chime--log-silently "Chime: %s: %s" prefix (error-message-string err)) + (chime--maybe-warn-persistent-failures) + (chime--set-modeline-error-state "Event check failed — check *Messages* buffer")) + +(defun chime--handle-async-success (callback events) + "Process a successful async fetch. Invoke CALLBACK with EVENTS. +Resets the consecutive-failure counter and sends a debug-completion log +when the debug module is loaded." + (setq chime--consecutive-async-failures 0) + (when (featurep 'chime-debug) + (chime--debug-log-async-complete events)) + (funcall callback events)) + (defun chime--fetch-and-process (callback) "Asynchronously fetch events from agenda and invoke CALLBACK with them. Manages async process state and last-check-time internally. @@ -1759,35 +1780,13 @@ Does nothing if a check is already in progress." (lambda (events) (setq chime--process nil) (setq chime--last-check-time (current-time)) - ;; Handle errors from async process (condition-case err - (progn - ;; Check if events is an error signal from async process - (if (and (listp events) - (eq (car events) 'async-signal)) - (progn - ;; Async process returned an error - (cl-incf chime--consecutive-async-failures) - (when (featurep 'chime-debug) - (chime--debug-log-async-error (cdr events))) - (chime--log-silently "Chime: Async error: %s" - (error-message-string (cdr events))) - (chime--maybe-warn-persistent-failures) - (chime--set-modeline-error-state "Event check failed — check *Messages* buffer")) - ;; Success - process events normally - (setq chime--consecutive-async-failures 0) - (when (featurep 'chime-debug) - (chime--debug-log-async-complete events)) - (funcall callback events))) + (if (and (listp events) + (eq (car events) 'async-signal)) + (chime--record-async-failure (cdr events) "Async error") + (chime--handle-async-success callback events)) (error - ;; Error occurred in callback processing - (cl-incf chime--consecutive-async-failures) - (when (featurep 'chime-debug) - (chime--debug-log-async-error err)) - (chime--log-silently "Chime: Error processing events: %s" - (error-message-string err)) - (chime--maybe-warn-persistent-failures) - (chime--set-modeline-error-state "Event check failed — check *Messages* buffer"))))))))) + (chime--record-async-failure err "Error processing events"))))))))) (defun chime--log-silently (format-string &rest args) "Append formatted message to *Messages* buffer without echoing. |
