Resurrect jupyter-finalized-object

This commit is contained in:
Nathaniel Nicandro 2020-09-09 14:21:49 -05:00
parent dcdda2bf92
commit 66a72ad24c
2 changed files with 16 additions and 10 deletions

View file

@ -371,14 +371,20 @@ class corresponding to the symbol `jupyter-instance-tracker'."
(cl-assert (get sym 'jupyter-instance-tracker) t)
(hash-table-keys table))))
(defvar jupyter-finalizers (make-hash-table :weakness 'key :test #'eq))
(defclass jupyter-finalized-object ()
((finalizers :type list :initform nil))
:documentation "A list of finalizers."
:documentation "A base class for cleaning up resources.
Adds the method `jupyter-add-finalizer' which maintains a list of
finalizer functions to be called when the object is garbage
collected.")
(defun jupyter-add-finalizer (obj fn)
"Add FN to the finalizers of OBJ."
(declare (indent 1))
(cl-check-type fn function)
(cl-callf append (gethash obj jupyter-finalizers)
(list (make-finalizer fn))))
(cl-defgeneric jupyter-add-finalizer ((obj jupyter-finalized-object) finalizer)
"Cleanup resources automatically.
FINALIZER if a function to be added to a list of finalizers that
will be called when OBJ is garbage collected."
(cl-check-type finalizer function)
(push (make-finalizer finalizer) (oref obj finalizers)))
;;; Session object definition

View file

@ -176,7 +176,8 @@ requests like the above example.")
;; Define channel classes for method dispatching based on the channel type
(defclass jupyter-kernel-client (jupyter-instance-tracker)
(defclass jupyter-kernel-client (jupyter-instance-tracker
jupyter-finalized-object)
((tracking-symbol :initform 'jupyter--clients)
(execution-state
:type string
@ -295,8 +296,7 @@ method is called."
(jupyter-add-finalizer client
(lambda ()
(when (buffer-live-p buffer)
(kill-buffer buffer))
(jupyter-stop-channels client)))))
(kill-buffer buffer))))))
(cl-defmethod jupyter-kernel-alive-p ((client jupyter-kernel-client))
"Return non-nil if the kernel CLIENT is connected to is alive."