diff options
| author | Basil L. Contovounesios | 2019-07-22 21:49:47 +0100 |
|---|---|---|
| committer | Basil L. Contovounesios | 2019-08-02 16:33:30 +0300 |
| commit | cf569e520ee080b5a913d37d363a5ab5fc38d982 (patch) | |
| tree | aa6da586e8c60fcf0cd62470ef9c26fcc1093b30 | |
| parent | 60eb0a4834305e1c2b31b1e817875f3d8d0be5f5 (diff) | |
| download | emacs-cf569e520ee080b5a913d37d363a5ab5fc38d982.tar.gz emacs-cf569e520ee080b5a913d37d363a5ab5fc38d982.zip | |
DRY in gravatar.el
For discussion, see the following thread:
https://lists.gnu.org/archive/html/emacs-devel/2019-07/msg00528.html
* lisp/image/gravatar.el (gravatar-data->image): Remove.
(gravatar-retrieve, gravatar-retrieve-synchronously): Reuse
url-fetch-from-cache and gravatar-retrieved to reduce duplication.
(gravatar-retrieved): Only cache buffer if url-current-object is
non-nil and return result of callback. This affords reusing this
function in cached URL buffers.
| -rw-r--r-- | lisp/image/gravatar.el | 48 |
1 files changed, 17 insertions, 31 deletions
diff --git a/lisp/image/gravatar.el b/lisp/image/gravatar.el index ea746b71d7b..fb539bcdbdc 100644 --- a/lisp/image/gravatar.el +++ b/lisp/image/gravatar.el | |||
| @@ -95,14 +95,6 @@ Valid sizes range from 1 to 2048 inclusive." | |||
| 95 | (search-forward "\n\n" nil t) | 95 | (search-forward "\n\n" nil t) |
| 96 | (buffer-substring (point) (point-max))))) | 96 | (buffer-substring (point) (point-max))))) |
| 97 | 97 | ||
| 98 | (defun gravatar-data->image () | ||
| 99 | "Get data of current buffer and return an image. | ||
| 100 | If no image available, return 'error." | ||
| 101 | (let ((data (gravatar-get-data))) | ||
| 102 | (if data | ||
| 103 | (create-image data nil t) | ||
| 104 | 'error))) | ||
| 105 | |||
| 106 | ;;;###autoload | 98 | ;;;###autoload |
| 107 | (defun gravatar-retrieve (mail-address callback &optional cbargs) | 99 | (defun gravatar-retrieve (mail-address callback &optional cbargs) |
| 108 | "Asynchronously retrieve a gravatar for MAIL-ADDRESS. | 100 | "Asynchronously retrieve a gravatar for MAIL-ADDRESS. |
| @@ -112,11 +104,8 @@ where GRAVATAR is either an image descriptor, or the symbol | |||
| 112 | (let ((url (gravatar-build-url mail-address))) | 104 | (let ((url (gravatar-build-url mail-address))) |
| 113 | (if (url-cache-expired url gravatar-cache-ttl) | 105 | (if (url-cache-expired url gravatar-cache-ttl) |
| 114 | (url-retrieve url #'gravatar-retrieved (list callback cbargs) t) | 106 | (url-retrieve url #'gravatar-retrieved (list callback cbargs) t) |
| 115 | (apply callback | 107 | (with-current-buffer (url-fetch-from-cache url) |
| 116 | (with-temp-buffer | 108 | (gravatar-retrieved () callback cbargs))))) |
| 117 | (url-cache-extract (url-cache-create-filename url)) | ||
| 118 | (gravatar-data->image)) | ||
| 119 | cbargs)))) | ||
| 120 | 109 | ||
| 121 | ;;;###autoload | 110 | ;;;###autoload |
| 122 | (defun gravatar-retrieve-synchronously (mail-address) | 111 | (defun gravatar-retrieve-synchronously (mail-address) |
| @@ -124,26 +113,23 @@ where GRAVATAR is either an image descriptor, or the symbol | |||
| 124 | Value is either an image descriptor, or the symbol `error' if the | 113 | Value is either an image descriptor, or the symbol `error' if the |
| 125 | retrieval failed." | 114 | retrieval failed." |
| 126 | (let ((url (gravatar-build-url mail-address))) | 115 | (let ((url (gravatar-build-url mail-address))) |
| 127 | (if (url-cache-expired url gravatar-cache-ttl) | 116 | (with-current-buffer (if (url-cache-expired url gravatar-cache-ttl) |
| 128 | (with-current-buffer (url-retrieve-synchronously url) | 117 | (url-retrieve-synchronously url) |
| 129 | (when gravatar-automatic-caching | 118 | (url-fetch-from-cache url)) |
| 130 | (url-store-in-cache (current-buffer))) | 119 | (gravatar-retrieved () #'identity)))) |
| 131 | (prog1 (gravatar-data->image) | ||
| 132 | (kill-buffer (current-buffer)))) | ||
| 133 | (with-temp-buffer | ||
| 134 | (url-cache-extract (url-cache-create-filename url)) | ||
| 135 | (gravatar-data->image))))) | ||
| 136 | 120 | ||
| 137 | (defun gravatar-retrieved (status cb &optional cbargs) | 121 | (defun gravatar-retrieved (status cb &optional cbargs) |
| 138 | "Callback function used by `gravatar-retrieve'." | 122 | "Handle Gravatar response data in current buffer. |
| 139 | ;; Store gravatar? | 123 | Return the result of (apply CB DATA CBARGS), where DATA is either |
| 140 | (when gravatar-automatic-caching | 124 | an image descriptor, or the symbol `error' on failure. |
| 141 | (url-store-in-cache (current-buffer))) | 125 | This function is intended as a callback for `url-retrieve'." |
| 142 | (if (plist-get status :error) | 126 | (let ((data (unless (plist-get status :error) |
| 143 | ;; Error happened. | 127 | (gravatar-get-data)))) |
| 144 | (apply cb 'error cbargs) | 128 | (and url-current-object ; Only cache if not already cached. |
| 145 | (apply cb (gravatar-data->image) cbargs)) | 129 | gravatar-automatic-caching |
| 146 | (kill-buffer (current-buffer))) | 130 | (url-store-in-cache)) |
| 131 | (prog1 (apply cb (if data (create-image data nil t) 'error) cbargs) | ||
| 132 | (kill-buffer)))) | ||
| 147 | 133 | ||
| 148 | (provide 'gravatar) | 134 | (provide 'gravatar) |
| 149 | 135 | ||