ein-query.el 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. ;;; ein-query.el --- jQuery like interface on to of url-retrieve
  2. ;; Copyright (C) 2012- Takafumi Arakaki
  3. ;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
  4. ;; This file is NOT part of GNU Emacs.
  5. ;; ein-query.el is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; ein-query.el is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with ein-query.el. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;;; Code:
  18. (eval-when-compile (require 'cl))
  19. (require 'request)
  20. (require 'ein-core)
  21. (require 'ein-log)
  22. ;;; Utils
  23. (defun ein:safe-funcall-packed (packed &rest args)
  24. (when packed
  25. (ein:log-ignore-errors (apply #'ein:funcall-packed packed args))))
  26. ;;; Variables
  27. (defcustom ein:query-timeout
  28. (if (eq request-backend 'url-retrieve) 1000 nil)
  29. "Default query timeout for HTTP access in millisecond.
  30. Setting this to `nil' means no timeout.
  31. If you have ``curl`` command line program, it is automatically set to
  32. `nil' as ``curl`` is reliable than `url-retrieve' therefore no need for
  33. a workaround (see below).
  34. If you do the same operation before the timeout, old operation
  35. will be canceled \(see also `ein:query-singleton-ajax').
  36. .. note:: This value exists because it looks like `url-retrieve'
  37. occasionally fails to finish \(start?) querying. Timeout is
  38. used to let user notice that their operation is not finished.
  39. It also prevent opening a lot of useless process buffers.
  40. You will see them when closing Emacs if there is no timeout.
  41. If you know how to fix the problem with `url-retrieve', please
  42. let me know or send pull request at github!
  43. \(Related bug report in Emacs bug tracker:
  44. http://debbugs.gnu.org/cgi/bugreport.cgi?bug=11469)"
  45. :type '(choice (integer :tag "Timeout [ms]" 5000)
  46. (const :tag "No timeout" nil))
  47. :group 'ein)
  48. ;;; Functions
  49. (defvar ein:query-running-process-table (make-hash-table :test 'equal))
  50. (defun* ein:query-singleton-ajax (key url &rest settings
  51. &key
  52. (timeout ein:query-timeout)
  53. &allow-other-keys)
  54. "Cancel the old process if there is a process associated with
  55. KEY, then call `request' with URL and SETTINGS. KEY is compared by
  56. `equal'."
  57. (ein:query-gc-running-process-table)
  58. (when timeout
  59. (setq settings (plist-put settings :timeout (/ timeout 1000.0))))
  60. (ein:aif (gethash key ein:query-running-process-table)
  61. (unless (request-response-done-p it)
  62. (request-abort it))) ; This will run callbacks
  63. (let ((response (apply #'request url settings)))
  64. (puthash key response ein:query-running-process-table)
  65. response))
  66. (defun ein:query-gc-running-process-table ()
  67. "Garbage collect dead processes in `ein:query-running-process-table'."
  68. (maphash
  69. (lambda (key buffer)
  70. (when (request-response-done-p buffer)
  71. (remhash key ein:query-running-process-table)))
  72. ein:query-running-process-table))
  73. ;;; Cookie
  74. (defalias 'ein:query-get-cookie 'request-cookie-string)
  75. (provide 'ein-query)
  76. ;;; ein-query.el ends here