-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathaio-plz.el
79 lines (60 loc) · 2.57 KB
/
aio-plz.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
;;; aio-plz.el --- Async HTTP library -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Adam Porter
;; Author: Adam Porter <[email protected]>
;; Keywords: comm
;; Package-Requires: ((emacs "26.3") (aio "1.0") (plz "0.6"))
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This library provides an `aio' wrapper for `plz'.
;;; Code:
;;;; Requirements
(require 'aio)
(require 'plz)
;;;; Variables
;;;; Customization
;;;; Commands
;;;; Functions
(defun aio-plz (method url &rest rest)
"FIXME: Docstring."
(declare (indent defun))
(cl-assert (not (plist-member rest :then)) nil "Argument THEN is not allowed for `aio-plz'")
(cl-assert (not (plist-member rest :else)) nil "Argument ELSE is not allowed for `aio-plz'")
(let ((promise (aio-promise)))
(setf (plist-get rest :then)
(lambda (result)
(aio-resolve promise (lambda () result)))
(plist-get rest :else)
(lambda (plz-error)
(aio-resolve promise (lambda ()
;; FIXME: When removing `plz-curl-error' and `plz-http-error',
;; also remove this string from the error data.
(signal 'plz-error (list "error" plz-error))))))
(prog1 promise
(condition-case err
(apply #'plz method url rest)
(error (aio-resolve promise (lambda ()
(signal (car err) (cdr err)))))))))
(defun aio-plz-run (queue)
"FIXME: Docstring."
(cl-assert (not (plz-queue-finally queue)) nil "Queue already has a FINALLY function: %S" queue)
(let ((promise (aio-promise)))
(setf (plz-queue-finally queue)
(lambda ()
(aio-resolve promise (lambda () t))))
(prog1 promise
(condition-case err
(plz-run queue)
(error (aio-resolve promise (lambda ()
(signal (car err) (cdr err)))))))))
;;;; Footer
(provide 'aio-plz)
;;; aio-plz.el ends here