Emacs library to control Basecamp
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.6KB

  1. ;;; libbcel-util.el --- Misc utility functions -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2019 Damien Cassou
  3. ;; Author: Damien Cassou <damien@cassou.me>
  4. ;; Url: https://gitlab.petton.fr/bcel/libbcel
  5. ;; Package-requires: ((emacs "26.1"))
  6. ;; Version: 0.4.0
  7. ;; This program is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; This program is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Various utility functions for others to use.
  19. ;;; Code:
  20. (defun libbcel-util-async-mapcar (mapfn list callback)
  21. "Apply MAPFN to each element of LIST and pass result to CALLBACK.
  22. MAPFN is a function taking 2 arguments: the element to map and a
  23. callback to call when the mapping is done."
  24. (if (not list)
  25. (funcall callback nil)
  26. (let ((result (make-vector (length list) nil))
  27. (count 0))
  28. (cl-loop for index below (length list)
  29. for item in list
  30. do (let ((index index) (item item))
  31. (funcall
  32. mapfn
  33. item
  34. (lambda (item-result)
  35. (setf (seq-elt result index) item-result)
  36. (cl-incf count)
  37. (when (eq count (length list))
  38. ;; use `run-at-time' to ensure that CALLBACK is
  39. ;; consistently called asynchronously even if MAPFN is
  40. ;; synchronous:
  41. (run-at-time
  42. 0 nil
  43. callback
  44. (seq-concatenate 'list result))))))))))
  45. (defun libbcel-util-async-mapc (mapfn list callback)
  46. "Same as `navigel-async-mapcar' but for side-effects only.
  47. MAPFN is a function taking 2 arguments: an element of LIST and a
  48. callback. MAPFN should call the callback with no argument when
  49. done computing.
  50. CALLBACK is a function of no argument that is called when done
  51. computing for the all elements of LIST."
  52. (libbcel-util-async-mapcar
  53. (lambda (item callback) (funcall mapfn item (lambda () (funcall callback nil))))
  54. list
  55. (lambda (_result) (funcall callback))))
  56. (provide 'libbcel-util)
  57. ;;; libbcel-util.el ends here