Skip to content

Commit 5c44338

Browse files
committed
Add aio-contrib.el
1 parent a7c14f1 commit 5c44338

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

aio-contrib.el

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
;;; aio-contrib.el --- extra functions for aio -*- lexical-binding: t; -*-
2+
3+
;; This is free and unencumbered software released into the public domain.
4+
5+
;;; Commentary:
6+
7+
;; These are additional, useful functions that do not make sense to
8+
;; include in the main aio package. This file has additional
9+
;; dependencies beyond that of aio.
10+
11+
;;; Code:
12+
13+
(require 'aio)
14+
(require 'elfeed)
15+
(require 'filenotify)
16+
(require 'hashcash)
17+
(require 'skewer-mode)
18+
19+
;; Note: Threads are too broken to be useful in at least Emacs 26.1.
20+
;; So, by extension, this function isn't useful either, which is why
21+
;; it's here in contrib. Hopefully someday it will be, though.
22+
(defun aio-make-thread (function &optional name)
23+
"Like `make-thread', but FUNCTION's return value is delivered by promise.
24+
25+
Returns a cons of (promise . thread). Calling `aio-await' on the
26+
returned promise is analogous to using `thread-join'."
27+
(let* ((promise (aio-promise))
28+
(wrapper (lambda ()
29+
(aio-with-promise promise
30+
(funcall function))))
31+
(thread (make-thread wrapper name)))
32+
(cons promise thread)))
33+
34+
(defun aio-skewer-eval (string &rest args)
35+
"Like `skewer-eval' but the result is delivered by a promise."
36+
(let* ((promise (aio-promise))
37+
(callback (lambda (result)
38+
(aio-resolve promise (lambda () result)))))
39+
(prog1 promise
40+
(apply #'skewer-eval string callback args))))
41+
42+
(defun aio-elfeed-curl-enqueue (url &rest args)
43+
"Like `elfeed-curl-enqueue' but delivered by a promise.
44+
45+
The result is a plist with the following keys:
46+
:success -- the callback argument (t or nil)
47+
:headers -- `elfeed-curl-headers'
48+
:status-code -- `elfeed-curl-status-code'
49+
:error-message -- `elfeed-curl-error-message'
50+
:location -- `elfeed-curl-location'
51+
:content -- (buffer-string)"
52+
(let* ((promise (aio-promise))
53+
(cb (lambda (success)
54+
(let ((result (list :success success
55+
:headers elfeed-curl-headers
56+
:status-code elfeed-curl-status-code
57+
:error-message elfeed-curl-error-message
58+
:location elfeed-curl-location
59+
:content (buffer-string))))
60+
(aio-resolve promise (lambda () result))))))
61+
(prog1 promise
62+
(apply #'elfeed-curl-enqueue url cb args))))
63+
64+
(defun aio-file-notify-add-watch (file flags)
65+
"Like `file-notify-add-watch' but delivered by a promise."
66+
(let* ((promise (aio-promise))
67+
(cb (lambda (event) (aio-resolve promise (lambda () event)))))
68+
(prog1 promise
69+
(file-notify-add-watch file flags cb))))
70+
71+
(defun aio-hashcash-generate-payment (str val)
72+
"Like `hashcash-generate-payment' but delivered by a promise."
73+
;; Note: In Emacs 26.1 this only works when byte compiled due to a
74+
;; bug in hashcash.el.
75+
(let* ((promise (aio-promise))
76+
(cb (lambda (_proc result)
77+
(aio-resolve promise (lambda () result)))))
78+
(prog1 promise
79+
(hashcash-generate-payment-async str val cb))))
80+
81+
(provide 'aio-contrib)
82+
83+
;;; aio-contrib.el ends here

0 commit comments

Comments
 (0)