Commit 7e4ae57f authored by Ludovic Courtès's avatar Ludovic Courtès
Browse files

Make the current inferior a <proxy-state> property.

* guix-jupyter-kernel.scm (%inferior-property): New variable.
(proxy-state-inferior, set-proxy-state-inferior)
(ensure-proxy-state-inferior): New procedures.
(%inferior): Remove.
(create-environment): Call 'proxy-state-inferior' instead of refering to
%INFERIOR.
(reply-execute-request): Call 'ensure-proxy-state-inferior' before
calling 'create-environment'.
(reply-complete-request): Call 'ensure-proxy-state-inferior' before
calling 'inferior-available-packages'.  Return the new state.
parent aedb7e6f
......@@ -80,6 +80,29 @@
(set-proxy-state-property state %default-environment-property
name))
(define %inferior-property
;; Property to access the inferior associated with this proxy.
(list 'inferior 'property))
(define (proxy-state-inferior state)
"Return the inferior associated with STATE, or #f if there is none."
(proxy-state-property state %inferior-property))
(define (set-proxy-state-inferior state inferior)
"Associate STATE with INFERIOR and return the new proxy state. If there
was already an inferior associated with STATE, close it."
(let ((previous (proxy-state-inferior state)))
(when previous
(close-inferior previous))
(set-proxy-state-property state %inferior-property
inferior)))
(define (ensure-proxy-state-inferior state)
"Return a new state based on STATE that has an associated inferior."
(if (proxy-state-inferior state)
state
(set-proxy-state-inferior state (open-default-inferior))))
;;
;; HTML.
;;
......@@ -106,10 +129,6 @@
(kernel-info-reply->json %kernel-info-reply))))
state)
(define %inferior
;; FIXME: This could be expensive!
(delay (open-default-inferior)))
(define (manifest->shtml manifest name)
"Return SHTML representing the contents of MANIFEST."
`(div
......@@ -151,7 +170,8 @@ NAME with MANIFEST."
of package specifications such as \"guile@2.2\". Send appropriate messages
to KERNEL as a reply to MESSAGE, and return STATE suitably adjusted."
(define manifest
(specifications->manifest (force %inferior) specs))
(specifications->manifest (proxy-state-inferior state)
specs))
(define store
(proxy-state-store state))
......@@ -236,20 +256,21 @@ stripped."
(pub-busy kernel message)
(match (string-tokenize line)
((";;guix" "environment" name "<-" specs ...)
(match (lookup-proxied name state)
(#f
(create-environment name specs state
#:kernel kernel
#:message message))
((? kernel? proxy)
(format/log "terminating existing '~a' environment~%"
name)
(unmonitor-client proxy)
(terminate-proxied-kernel proxy)
(create-environment name specs
(unregister-proxied name state)
#:kernel kernel
#:message message))))
(let ((state (ensure-proxy-state-inferior state)))
(match (lookup-proxied name state)
(#f
(create-environment name specs state
#:kernel kernel
#:message message))
((? kernel? proxy)
(format/log "terminating existing '~a' environment~%"
name)
(unmonitor-client proxy)
(terminate-proxied-kernel proxy)
(create-environment name specs
(unregister-proxied name state)
#:kernel kernel
#:message message)))))
((";;guix" "environment" name)
(match (lookup-proxied name state)
(#f
......@@ -330,24 +351,29 @@ Return STATE."
((";;guix" command)
(send-completion-reply '("environment")
(- cursor (string-length command))
cursor))
cursor)
state)
((";;guix" "environment" prefix)
(match (proxy-state-proxied state)
(((names . _) ...)
(send-completion-reply (filter (cut string-prefix? prefix <>)
names)
(- cursor (string-length prefix))
cursor))))
cursor)
state)))
((";;guix" "environment" _ "<-" _ ... prefix)
(match (inferior-available-packages (force %inferior))
(((names . _) ...)
(send-completion-reply (filter (cut string-prefix? prefix <>)
names)
(- cursor (string-length prefix))
cursor))))
(let* ((state (ensure-proxy-state-inferior state))
(inferior (proxy-state-inferior state)))
(match (inferior-available-packages inferior)
(((names . _) ...)
(send-completion-reply (filter (cut string-prefix? prefix <>)
names)
(- cursor (string-length prefix))
cursor)))
state))
(_
(format/log "ignoring completion request~%")
#f))
state))
;; Pass the completion request to one of the proxied kernels.
(match (or (environment-from-magic first)
......@@ -357,10 +383,11 @@ Return STATE."
(format/log "forwarding completion request to \
environment ~s (PID ~s)~%"
environment (kernel-pid proxied))
(send-message proxied message #:kernel-socket kind)))
(send-message proxied message #:kernel-socket kind)
state))
(#f
(format/log "unknown target kernel for completion request~%")))))
state)
(format/log "unknown target kernel for completion request~%")
state)))))
(define (reply-inspect-request kernel kind message state)
"Handle MESSAGE, an \"inspect_request\" message, possibly by forwarding it
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment