From 66a72ad24c6695546fd78cb17d7c90088a8e3604 Mon Sep 17 00:00:00 2001 From: Nathaniel Nicandro Date: Wed, 9 Sep 2020 14:21:49 -0500 Subject: [PATCH] Resurrect `jupyter-finalized-object` --- jupyter-base.el | 20 +++++++++++++------- jupyter-client.el | 6 +++--- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/jupyter-base.el b/jupyter-base.el index 59090f3..32abb39 100644 --- a/jupyter-base.el +++ b/jupyter-base.el @@ -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 diff --git a/jupyter-client.el b/jupyter-client.el index fc12f3d..b2338fd 100644 --- a/jupyter-client.el +++ b/jupyter-client.el @@ -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."