Function: jsonrpc--async-request-1

jsonrpc--async-request-1 is a byte-compiled function defined in jsonrpc.el.gz.

Signature

(jsonrpc--async-request-1 CONNECTION METHOD PARAMS &rest ARGS &key SUCCESS-FN ERROR-FN TIMEOUT-FN (TIMEOUT jsonrpc-default-request-timeout) (DEFERRED nil) (SYNC-REQUEST nil))

Documentation

Helper for jsonrpc-request and jsonrpc-async-request.

Return a list (ID TIMER). ID is the new request's ID, or nil if the request was deferred. TIMER is a timer object set (or nil, if TIMEOUT is nil).

Source Code

;; Defined in /usr/src/emacs/lisp/jsonrpc.el.gz
(cl-defun jsonrpc--async-request-1 (connection
                                    method
                                    params
                                    &rest args
                                    &key success-fn error-fn timeout-fn
                                    (timeout jsonrpc-default-request-timeout)
                                    (deferred nil)
                                    (sync-request nil))
  "Helper for `jsonrpc-request' and `jsonrpc-async-request'.

Return a list (ID TIMER).  ID is the new request's ID, or nil if
the request was deferred.  TIMER is a timer object set (or nil, if
TIMEOUT is nil)."
  (pcase-let* ((buf (current-buffer)) (point (point))
               (`(,_ ,timer ,old-id)
                (and deferred (gethash (list deferred buf)
                                       (jsonrpc--deferred-actions connection))))
               (id (or old-id (cl-incf (jsonrpc--next-request-id connection))))
               (maybe-timer
                (lambda ()
                  (when timeout
                    (or timer
                        (setq
                         timer
                         (run-with-timer
                          timeout nil
                          (lambda ()
                            (jsonrpc--remove connection id (list deferred buf))
                            (jsonrpc--event
                             connection 'internal
                             :log-text (format "timed-out request '%s'" method)
                             :id id)
                            (when timeout-fn (funcall timeout-fn))))))))))
    (when deferred
      (if (jsonrpc-connection-ready-p connection deferred)
          ;; Server is ready, we jump below and send it immediately.
          (remhash (list deferred buf) (jsonrpc--deferred-actions connection))
        ;; Otherwise, save in `jsonrpc--deferred-actions' and exit non-locally
        (unless old-id
          (jsonrpc--event
           connection 'internal
           :log-text (format "deferring request '%s'" method)
           :id id))
        (puthash (list deferred buf)
                 (list (lambda ()
                         (when (buffer-live-p buf)
                           (with-current-buffer buf
                             (save-excursion (goto-char point)
                                             (apply #'jsonrpc--async-request-1
                                                    connection
                                                    method params args)))))
                       (funcall maybe-timer) id)
                 (jsonrpc--deferred-actions connection))
        (cl-return-from jsonrpc--async-request-1 (list id timer))))
    ;; Really send it thru the wire
    ;;
    (jsonrpc-connection-send connection
                             :id id
                             :method method
                             :params params)
    ;; Setup some control structures
    ;;
    (when sync-request
      (push (list id) (jsonrpc--sync-request-alist connection)))

    (jsonrpc--schedule
     connection id method
     (or success-fn
         (lambda (&rest _ignored)
           (jsonrpc--event
            connection 'internal
            :log-text (format "success ignored")
            :id id)))
     (or error-fn
         (jsonrpc-lambda (&key code message &allow-other-keys)
           (jsonrpc--event
            connection 'internal
            :log-text (format "error %s ignored: %s ignored"
                              code message)
            :id id)))
     (funcall maybe-timer))
    (list id timer)))