Cache default nodes

This makes a significant difference in CPU usage when e.g. displaying
the defaults in a panel.
This commit is contained in:
Milan Zamazal 2022-06-25 07:11:20 +02:00
parent 9ca7a06af6
commit b80fbab73a
1 changed files with 14 additions and 9 deletions

View File

@ -31,11 +31,13 @@
(defvar pw-lib--objects '()) (defvar pw-lib--objects '())
(defvar pw-lib--bindings nil) (defvar pw-lib--bindings nil)
(defvar pw-lib--defaults nil)
(defun pw-lib-refresh () (defun pw-lib-refresh ()
"Clear cache of objects retrieved from PipeWire." "Clear cache of objects retrieved from PipeWire."
(setq pw-lib--objects (pw-access-objects pw-lib--accessor) (setq pw-lib--objects (pw-access-objects pw-lib--accessor)
pw-lib--bindings nil)) pw-lib--bindings nil
pw-lib--defaults nil))
(defun pw-lib-objects (&optional type) (defun pw-lib-objects (&optional type)
"Return a list of PipeWire objects. "Return a list of PipeWire objects.
@ -136,14 +138,17 @@ default sink or source as reported by PipeWire and ID is the
corresponding PipeWire node numeric id. corresponding PipeWire node numeric id.
Note that PipeWire data is cached, if you need its up-to-date Note that PipeWire data is cached, if you need its up-to-date
version, call `pw-lib-refresh' first." version, call `pw-lib-refresh' first."
(let ((defaults (pw-access-defaults pw-lib--accessor)) (unless pw-lib--defaults
(nodes (mapcar #'(lambda (o) (let ((defaults (pw-access-defaults pw-lib--accessor))
(cons (pw-lib-object-value o "node.name") (pw-lib-object-id o))) (nodes (mapcar #'(lambda (o)
(pw-lib-objects "Node")))) (cons (pw-lib-object-value o "node.name") (pw-lib-object-id o)))
(cl-remove-if-not #'cdr (pw-lib-objects "Node"))))
(mapcar #'(lambda (d) (setq pw-lib--defaults
(cons (car d) (cdr (assoc (cdr d) nodes)))) (cl-remove-if-not #'cdr
defaults)))) (mapcar #'(lambda (d)
(cons (car d) (cdr (assoc (cdr d) nodes))))
defaults)))))
pw-lib--defaults)
(defun pw-lib--default-node (key) (defun pw-lib--default-node (key)
(pw-lib-get-object (cdr (assoc key (pw-lib-default-nodes))))) (pw-lib-get-object (cdr (assoc key (pw-lib-default-nodes)))))