Run custom hooks after a kernel connection is made.

Introduce new user custom variable `ein:on-kernel-connect-functions' which is an
abnormal hook called after a kernel connection is made. All functions in the
hook are called with the connected kernel as an argument.

This replaces the old, unused `ein:kernel-run-after-start-hook' function.
This commit is contained in:
John Miller 2020-03-01 16:30:35 -07:00
parent 10d7f10179
commit 28b9f46215
3 changed files with 20 additions and 5 deletions

View file

@ -112,3 +112,11 @@ Scenario: Test the undo-tree-incompatible logic
Given new python notebook
And I call "turn-on-undo-tree-mode"
Then the value of "undo-tree-mode" is nil
@kernel-on-connect
Scenario: Test ein:on-kernel-connect-functions abnormal hooks
Given I set the kernel connect message
Given new python notebook
And I wait for the smoke to clear
And I switch to buffer "*Messages*"
Then I should see "Hello ein."

View file

@ -2,6 +2,10 @@
(lambda ()
(insert-char 37)))
(When "^I set the kernel connect message$"
(lambda ()
(add-to-list 'ein:on-kernel-connect-functions #'(lambda (_) (message "Hello ein.")))))
(When "^I type session port \\([0-9]+\\)$"
(lambda (port)
(ein:process-refresh-processes)

View file

@ -62,6 +62,12 @@
:type 'list
:group 'ein)
(defcustom ein:on-kernel-connect-functions nil
"Abnormal hook that is run after a websocket connection is made
to a jupyter kernel. Functions defined here must accept a single
argument, which is the kernel that was just connected."
:type 'list
:group 'ein)
;;; Initialization and connection.
@ -293,7 +299,8 @@ See https://github.com/ipython/ipython/pull/3307"
(let* ((websocket (websocket-client-data ws))
(kernel (ein:$websocket-kernel websocket)))
(when (ein:kernel-live-p kernel)
(ein:kernel-run-after-start-hook kernel)
(ein:log 'verbose "ein:start-single-websocket: Running on-connect abnormal hooks.")
(run-hook-with-args 'ein:on-kernel-connect-functions kernel)
(when cb
(funcall cb kernel)))
(ein:log 'verbose "WS opened: %s" (websocket-url ws))))
@ -307,10 +314,6 @@ See https://github.com/ipython/ipython/pull/3307"
(defun ein:kernel-on-connect (kernel content -metadata-not-used-)
(ein:log 'info "Kernel connect_request_reply received."))
(defun ein:kernel-run-after-start-hook (kernel)
(mapc #'ein:funcall-packed
(ein:$kernel-after-start-hook kernel)))
(defun ein:kernel-disconnect (kernel)
"Close websocket connection to running kernel, but do not
delete the kernel on the server side"