Rewrite callbacks: replace cons with apply-partially

This commit is contained in:
Takafumi Arakaki 2012-12-29 17:17:39 +01:00
parent 9e3be6c954
commit fdb80c9962
3 changed files with 52 additions and 51 deletions

View file

@ -112,7 +112,7 @@
"?" (format "notebook=%s" notebook-id))
:type "POST"
:parser #'ein:json-read
:success (cons #'ein:kernel--kernel-started kernel))))
:success (apply-partially #'ein:kernel--kernel-started kernel))))
(defun ein:kernel-restart (kernel)
@ -128,7 +128,7 @@
"restart")
:type "POST"
:parser #'ein:json-read
:success (cons #'ein:kernel--kernel-started kernel))))
:success (apply-partially #'ein:kernel--kernel-started kernel))))
(defun* ein:kernel--kernel-started (kernel &key data &allow-other-keys)
@ -475,9 +475,8 @@ Relevant Python code:
(ein:$kernel-kernel-url kernel)
"interrupt")
:type "POST"
:success (cons (lambda (&rest ignore)
(ein:log 'info "Sent interruption command."))
nil))))
:success (lambda (&rest ignore)
(ein:log 'info "Sent interruption command.")))))
(defun ein:kernel-kill (kernel &optional callback cbargs)
@ -488,13 +487,12 @@ Relevant Python code:
(ein:$kernel-kernel-url kernel))
:cache nil
:type "DELETE"
:success (cons (lambda (packed &rest ignore)
(ein:log 'info "Notebook kernel is killed")
(destructuring-bind (kernel callback cbargs)
packed
(setf (ein:$kernel-running kernel) nil)
(when callback (apply callback cbargs))))
(list kernel callback cbargs)))))
:success (apply-partially
(lambda (kernel callback cbargs &rest ignore)
(ein:log 'info "Notebook kernel is killed")
(setf (ein:$kernel-running kernel) nil)
(when callback (apply callback cbargs)))
kernel callback cbargs))))
;; Reply handlers.

View file

@ -339,14 +339,16 @@ See `ein:notebook-open' for more information."
url
:timeout ein:notebook-querty-timeout-open
:parser #'ein:json-read
:success (cons #'ein:notebook-request-open-callback-with-callback
(list notebook callback cbargs)))
:success (apply-partially
#'ein:notebook-request-open-callback-with-callback
notebook callback cbargs))
notebook))
(defun ein:notebook-request-open-callback-with-callback (packed &rest args)
(let ((notebook (nth 0 packed))
(callback (nth 1 packed))
(cbargs (nth 2 packed)))
(defun ein:notebook-request-open-callback-with-callback (notebook
callback
cbargs
&rest args)
(progn
(apply #'ein:notebook-request-open-callback notebook args)
(when callback
(with-current-buffer (ein:notebook-buffer notebook)
@ -598,25 +600,25 @@ of NOTEBOOK."
:headers '(("Content-Type" . "application/json"))
:cache nil
:data (json-encode data)
:error (cons #'ein:notebook-save-notebook-error notebook)
:success (cons #'ein:notebook-save-notebook-workaround
(list notebook retry callback cbarg))
:error (apply-partially #'ein:notebook-save-notebook-error notebook)
:success (apply-partially #'ein:notebook-save-notebook-workaround
notebook retry callback cbarg)
:status-code
`((204 . ,(cons (lambda (arg &rest rest)
(destructuring-bind (notebook callback cbarg)
arg
(apply #'ein:notebook-save-notebook-success
notebook rest)
(when callback
(apply callback cbarg rest))))
(list notebook callback cbarg)))))))
`((204 . ,(apply-partially
(lambda (notebook callback cbarg &rest rest)
(apply #'ein:notebook-save-notebook-success
notebook rest)
(when callback
(apply callback cbarg rest)))
notebook callback cbarg))))))
(defun ein:notebook-save-notebook-command ()
"Save the notebook."
(interactive)
(ein:notebook-save-notebook ein:%notebook% 0))
(defun* ein:notebook-save-notebook-workaround (packed &rest args
(defun* ein:notebook-save-notebook-workaround (notebook retry callback cbarg
&rest args
&key
status
response-status
@ -626,8 +628,7 @@ of NOTEBOOK."
;; choose PUT method every two times, let's check the response
;; here and fail when 204 is not returned.
(unless (eq response-status 204)
(destructuring-bind (notebook retry callback cbarg)
packed
(progn
(with-current-buffer (ein:notebook-buffer notebook)
(if (< retry ein:notebook-save-retry-max)
(progn
@ -1334,10 +1335,10 @@ the first argument and CBARGS as the rest of arguments."
:notebook-id
(ein:html-get-data-in-body-tag "data-notebook-id")))
:success
(cons (function*
(lambda (packed &key data &allow-other-keys)
(apply (car packed) data (cadr packed))))
(list callback cbargs)))))
(apply-partially (function*
(lambda (callback cbargs &key data &allow-other-keys)
(apply callback data cbargs)))
callback cbargs))))
;;; Buffer and kill hooks

View file

@ -212,19 +212,21 @@ This function is called via `ein:notebook-after-rename-hook'."
(ein:notebooklist-new-url url-or-port)
:parser (lambda ()
(ein:html-get-data-in-body-tag "data-notebook-id"))
:error (cons #'ein:notebooklist-new-notebook-error
(list url-or-port callback cbargs))
:success (cons #'ein:notebooklist-new-notebook-callback
(list url-or-port callback cbargs))))
:error (apply-partially #'ein:notebooklist-new-notebook-error
url-or-port callback cbargs)
:success (apply-partially #'ein:notebooklist-new-notebook-callback
url-or-port callback cbargs)))
(defun* ein:notebooklist-new-notebook-callback (packed &key
(defun* ein:notebooklist-new-notebook-callback (url-or-port
callback
cbargs
&key
data
&allow-other-keys
&aux
(notebook-id data)
(no-popup t))
(destructuring-bind (url-or-port callback cbargs)
packed
(progn
(ein:log 'info "Creating a new notebook... Done.")
(if notebook-id
(ein:notebook-open url-or-port notebook-id callback cbargs)
@ -289,12 +291,12 @@ This function is called via `ein:notebook-after-rename-hook'."
notebook-id)
:cache nil
:type "DELETE"
:success (cons (lambda (packed &rest ignore)
(ein:log 'info
"Deleting notebook %s... Done." (cdr packed))
(with-current-buffer (car packed)
(ein:notebooklist-reload)))
(cons (current-buffer) name))))
:success (apply-partially (lambda (buffer name &rest ignore)
(ein:log 'info
"Deleting notebook %s... Done." buffer)
(with-current-buffer name
(ein:notebooklist-reload)))
(current-buffer) name)))
(defun ein:notebooklist-render ()
"Render notebook list widget.
@ -475,8 +477,8 @@ FIMXE: document how to use `ein:notebooklist-find-file-callback'
:type "POST"
:data (concat "password=" (url-hexify-string password))
:parser #'ein:notebooklist-login--parser
:error (cons #'ein:notebooklist-login--error url-or-port)
:success (cons #'ein:notebooklist-login--success url-or-port)))
:error (apply-partially #'ein:notebooklist-login--error url-or-port)
:success (apply-partially #'ein:notebooklist-login--success url-or-port)))
(defun ein:notebooklist-login--parser ()
(goto-char (point-min))