Browse Source

Move 2 utility functions to new libbcel-util.el

pull/4/head
Damien Cassou 3 months ago
parent
commit
6fed8e4652
Signed by: Damien Cassou <damien@cassou.me> GPG Key ID: B68746238E59B548
2 changed files with 73 additions and 51 deletions
  1. +71
    -0
      libbcel-util.el
  2. +2
    -51
      libbcel.el

+ 71
- 0
libbcel-util.el View File

@@ -0,0 +1,71 @@
;;; libbcel-util.el --- Misc utility functions -*- lexical-binding: t; -*-

;; Copyright (C) 2019 Damien Cassou

;; Author: Damien Cassou <damien@cassou.me>
;; Url: https://gitlab.petton.fr/bcel/libbcel
;; Package-requires: ((emacs "26.1"))
;; Version: 0.4.0

;; 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:

;; Various utility functions for others to use.

;;; Code:

(defun libbcel-util-async-mapcar (mapfn list callback)
"Apply MAPFN to each element of LIST and pass result to CALLBACK.

MAPFN is a function taking 2 arguments: the element to map and a
callback to call when the mapping is done."
(if (not list)
(funcall callback nil)
(let ((result (make-vector (length list) nil))
(count 0))
(cl-loop for index below (length list)
for item in list
do (let ((index index) (item item))
(funcall
mapfn
item
(lambda (item-result)
(setf (seq-elt result index) item-result)
(cl-incf count)
(when (eq count (length list))
;; use `run-at-time' to ensure that CALLBACK is
;; consistently called asynchronously even if MAPFN is
;; synchronous:
(run-at-time
0 nil
callback
(seq-concatenate 'list result))))))))))

(defun libbcel-util-async-mapc (mapfn list callback)
"Same as `navigel-async-mapcar' but for side-effects only.

MAPFN is a function taking 2 arguments: an element of LIST and a
callback. MAPFN should call the callback with no argument when
done computing.

CALLBACK is a function of no argument that is called when done
computing for the all elements of LIST."
(libbcel-util-async-mapcar
(lambda (item callback) (funcall mapfn item (lambda () (funcall callback nil))))
list
(lambda (_result) (funcall callback))))

(provide 'libbcel-util)
;;; libbcel-util.el ends here

+ 2
- 51
libbcel.el View File

@@ -28,6 +28,7 @@

;;; Code:

(require 'libbcel-util)
(require 'libbcel-oauth)
(require 'libbcel-client)
(require 'libbcel-structs)
@@ -41,56 +42,6 @@
:group 'external)

;;; Private variables


;;; Private functions



(defun libbcel--async-mapcar (mapfn list callback)
"Apply MAPFN to each element of LIST and pass result to CALLBACK.

MAPFN is a function taking 2 arguments: the element to map and a
callback to call when the mapping is done."
(if (not list)
(funcall callback nil)
(let ((result (make-vector (length list) nil))
(count 0))
(cl-loop for index below (length list)
for item in list
do (let ((index index) (item item))
(funcall
mapfn
item
(lambda (item-result)
(setf (seq-elt result index) item-result)
(cl-incf count)
(when (eq count (length list))
;; use `run-at-time' to ensure that CALLBACK is
;; consistently called asynchronously even if MAPFN is
;; synchronous:
(run-at-time
0 nil
callback
(seq-concatenate 'list result))))))))))

(defun libbcel--async-mapc (mapfn list callback)
"Same as `navigel-async-mapcar' but for side-effects only.

MAPFN is a function taking 2 arguments: an element of LIST and a
callback. MAPFN should call the callback with no argument when
done computing.

CALLBACK is a function of no argument that is called when done
computing for the all elements of LIST."
(libbcel--async-mapcar
(lambda (item callback) (funcall mapfn item (lambda () (funcall callback nil))))
list
(lambda (_result) (funcall callback))))

;;; Public functions

(defun libbcel-completing-read (prompt entities &optional transformer)
@@ -119,7 +70,7 @@ Pass PROMPT, the elements of ENTITY and TRANSFORMER to
(libbcel-completing-read prompt entities transformer)))))

(cl-defmethod libbcel-nav-children ((entities list) callback)
(libbcel--async-mapcar
(libbcel-util-async-mapcar
#'libbcel-nav-children
entities
callback))

Loading…
Cancel
Save