branch: externals/listen commit 1e03676563fb805811b8b8038e5524360221916c Author: Adam Porter <a...@alphapapa.net> Commit: Adam Porter <a...@alphapapa.net>
Fix: (listen--send MPV) Improve event processing This is a more robust and should prevent occasional json-end-of-file errors that were happening. --- listen-mpv.el | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/listen-mpv.el b/listen-mpv.el index c0a596660f..46069f753d 100755 --- a/listen-mpv.el +++ b/listen-mpv.el @@ -128,18 +128,45 @@ Stops playing, clears playlist, adds FILE, and plays it." (with-current-buffer (process-buffer network-process) (let ((json (json-encode `(("command" ,command ,@args) ("request_id" . ,request-id))))) + ;; (message "SENDING: %S" json) (process-send-string network-process json) (process-send-string network-process "\n") + (goto-char (point-max)) (with-local-quit (accept-process-output network-process 2)) - (goto-char (point-min)) - (let ((json-false nil)) - (prog1 (cl-loop for result = (json-read) - while result - when (equal request-id (map-elt result 'request_id)) - return result) - (unless listen-debug-p - (erase-buffer)))))))) + (save-excursion + (goto-char (point-min)) + (let ((json-false nil)) + (cl-loop + ;; do (message "BUFFER-CONTENTS:%S POS:%s BUFFER-SIZE:%s EOBP:%s" + ;; (buffer-string) (point) (buffer-size) (eobp)) + until (or (eobp) (looking-at-p (rx (0+ space) eos))) + for start-pos = (point) + for result = (condition-case-unless-debug err + (json-read) + (error + (error "listen--send: JSON-READ signaled error: %S BUFFER-CONTENTS:%S POS:%s BUFFER-SIZE:%s EOBP:%s" + err (buffer-string) (point) (buffer-size) (eobp)))) + while result + for value = (pcase (map-elt result 'request_id) + ((pred (equal request-id)) + ;; Event is the one we're looking for: delete the event from the + ;; buffer and return it. + (unless listen-debug-p + (delete-region start-pos (point))) + result) + ('nil + ;; Event has no request ID: delete it from the buffer. + (unless listen-debug-p + (delete-region start-pos (point))) + nil) + (_ + ;; Event is for a different request: ignore it (this probably + ;; won't happen in practice, since we process commands + ;; synchronously, but it's good to be careful). + nil)) + when value + return value))))))) (cl-defmethod listen--seek ((player listen-player-mpv) seconds) "Seek PLAYER to SECONDS."