branch: externals/plz
commit 43231ac43c26a4e50b393ba57db56c88b1ed5090
Author: Adam Porter <[email protected]>
Commit: Adam Porter <[email protected]>
WIP: Use timer for response parsing
---
plz.el | 47 ++++++++++++++++++++++++++++++++++++-----------
1 file changed, 36 insertions(+), 11 deletions(-)
diff --git a/plz.el b/plz.el
index dbf6a1f5be..b318a33c0d 100644
--- a/plz.el
+++ b/plz.el
@@ -702,17 +702,42 @@ Includes active and queued requests."
;;;;; Private
-(defun plz--sentinel (process-or-buffer status)
+(defun plz--sentinel (process status)
+ ;; FIXME: Update docstring for arguments.
"Process buffer of curl output in PROCESS-OR-BUFFER.
If PROCESS-OR-BUFFER if a process, uses its buffer; if a buffer,
uses it. STATUS should be the process's event string (see info
node `(elisp) Sentinels'). Kills the buffer before returning."
;; Inspired by and some code copied from `elfeed-curl--sentinel'.
- (let* ((buffer (cl-etypecase process-or-buffer
- (process (process-buffer process-or-buffer))
- (buffer process-or-buffer)))
- (finally (buffer-local-value 'plz-finally buffer))
- sync)
+ (let ((buffer (process-buffer process)))
+ (with-current-buffer buffer
+ (pcase status
+ ((or 0 "finished\n")
+ ;; Curl exited normally: check HTTP status code.
+ (if plz-sync
+ (plz--timer process buffer status)
+ (run-at-time 0 nil #'plz--timer process buffer status)))
+
+ ((or (and (pred numberp) code)
+ (rx "exited abnormally with code " (let code (group (1+ digit)))))
+ ;; Curl error.
+ (ignore code)
+ (if plz-sync
+ (plz--timer process buffer status)
+ (run-at-time 0 nil #'plz--timer process buffer status)))
+
+ ((and (or "killed\n" "interrupt\n") status)
+ ;; Curl process killed or interrupted.
+ (if plz-sync
+ (plz--timer process buffer status)
+ (run-at-time 0 nil #'plz--timer process buffer status)))))))
+
+(defun plz--timer (process buffer status)
+ "Process HTTP response in BUFFER.
+To be called from a timer run in `plz--sentinel'."
+ (let ((finally (buffer-local-value 'plz-finally buffer))
+ sync)
+ (cl-assert process nil "PROCESS IS NIL FOR BUFFER:%S" buffer)
(unwind-protect
(with-current-buffer buffer
(setf sync plz-sync)
@@ -733,7 +758,7 @@ node `(elisp) Sentinels'). Kills the buffer before
returning."
;; (for now, anyway).
(let ((err (make-plz-error :response (plz--response))))
(pcase-exhaustive plz-else
- (`nil (process-put process-or-buffer :plz-result err))
+ (`nil (process-put process :plz-result err))
((pred functionp) (funcall plz-else err)))))))
((or (and (pred numberp) code)
@@ -745,7 +770,7 @@ node `(elisp) Sentinels'). Kills the buffer before
returning."
(curl-error-message (alist-get curl-exit-code
plz-curl-errors))
(err (make-plz-error :curl-error (cons curl-exit-code
curl-error-message))))
(pcase-exhaustive plz-else
- (`nil (process-put process-or-buffer :plz-result err))
+ (`nil (process-put process :plz-result err))
((pred functionp) (funcall plz-else err)))))
((and (or "killed\n" "interrupt\n") status)
@@ -755,16 +780,16 @@ node `(elisp) Sentinels'). Kills the buffer before
returning."
("interrupt\n" "curl process interrupted")))
(err (make-plz-error :message message)))
(pcase-exhaustive plz-else
- (`nil (process-put process-or-buffer :plz-result err))
+ (`nil (process-put process :plz-result err))
((pred functionp) (funcall plz-else err))))))
(error
;; Error signaled by a function called to process HTTP response:
;; rather than signaling an error from within the sentinel,
;; return or call the ELSE function with a plz-error struct.
(let ((err (make-plz-error :message (format "plz--sentinel: Error
signaled: %S REQUEST-ARGS:%S"
- err (process-get
process-or-buffer :plz-args)))))
+ err (process-get
process :plz-args)))))
(pcase-exhaustive plz-else
- (`nil (process-put process-or-buffer :plz-result err))
+ (`nil (process-put process :plz-result err))
((pred functionp) (funcall plz-else err)))))))
(when finally
(funcall finally))