Subject: 29.1; multisession does not seem to work with maps (e.g. alists or hash tables)

From: Adam Porter <[email-masked]>
Date: Mon, 15 Jan 2024 13:28:34 -0600

I've noticed that `multisession' does not seem to work with maps,
including alists and hash tables, although it does not seem to mention
this in the documentation.

Specifically, setting an initial value in an empty alist does cause the 
value file to be written to disk.  But afterward, updating the map 
leaves the file on disk un-updated, so after restarting Emacs, only the 
original value is available.

I found no tests in multisession-tests.el that would seem to cover these
use cases, so here are a couple that indeed fail, showing that it does
not work with them:

   (ert-deftest multi-test-files-alist-values ()
     (ert-with-temp-file dir
       :directory t
       (let ((user-init-file "/tmp/sfoo.el")
             (multisession-storage 'files)
             (multisession-directory dir)
             last-sequence)
         (define-multisession-variable multisession--alist nil)
         (should (eq (multisession-value multisession--alist) nil)) ; 
Comment out for interactive testing.

         ;; There seems to be no way to "unload" a multisession variable
         ;; within a session, so we test the sequence number to ensure
         ;; that the value was rewritten.

         (setf (alist-get 'KEY (multisession-value multisession--alist)) 1)
         (should (eq (alist-get 'KEY (multisession-value 
multisession--alist)) 1))
         ;; The file should have been written now, setting the sequence.
         (should (setf last-sequence (multisession--cached-sequence 
multisession--alist)))

         (setf (alist-get 'KEY (multisession-value multisession--alist)) 2)
         (should (eq (alist-get 'KEY (multisession-value 
multisession--alist)) 2))
         ;; The file should have been written again now, so the sequence
         ;; should have changed.
         (should-not (equal last-sequence (multisession--cached-sequence 
multisession--alist))))))

   (ert-deftest multi-test-files-hash-table-values ()
     (ert-with-temp-file dir
       :directory t
       (let ((user-init-file "/tmp/sfoo.el")
             (multisession-storage 'files)
             (multisession-directory dir)
             last-sequence)
         (define-multisession-variable multisession--hash-table 
(make-hash-table))
         (should (hash-table-p (multisession-value 
multisession--hash-table)))

         (setf (map-elt (multisession-value multisession--hash-table) 
'KEY) 1)
         (should (eq (map-elt (multisession-value 
multisession--hash-table) 'KEY) 1))
         ;; The file should have been written now, setting the sequence.
         (should (setf last-sequence (multisession--cached-sequence 
multisession--hash-table)))

         (setf (map-elt (multisession-value multisession--hash-table) 
'KEY) 2)
         (should (eq (map-elt (multisession-value 
multisession--hash-table) 'KEY) 2))
         ;; The file should have been written again now, so the sequence
         ;; should have changed.
         (should-not (equal last-sequence (multisession--cached-sequence
         multisession--hash-table))))))

I don't know how to fix this.  But AFAICT the persist.el library on
GNU ELPA does work with maps, so it may have some good examples.

Thanks,
Adam