Add ein:events-on to set event hook

This commit is contained in:
Takafumi Arakaki 2012-05-22 20:00:27 +02:00
parent bd6a8db46d
commit b09a8a1eb0

View file

@ -61,6 +61,8 @@ See also `ein:ac-setup-maybe'."
(defclass ein:events ()
((buffer :initarg :buffer :type buffer :document "Notebook buffer")
(callbacks :initarg :callbacks :type hash-table
:initform (make-hash-table :test 'equal))
(status-notebook :initarg :status-notebook :initform nil :type symbol)
(status-kernel :initarg :status-kernel :initform nil :type symbol))
"Event handler class.")
@ -87,12 +89,15 @@ Use the variable returned by this function instead."
(status_busy "Kernel is busy...")
(status_dead "Kernel is dead. Need restart.")))
(defun ein:events-trigger (events event-type)
(defun ein:events-trigger (events event-type &optional data)
"Trigger EVENT-TYPE and let event handler EVENTS handle that event.
EVENT-TYPE is a cons like \(notebook_saved . Notebook), which is
a direct translation of \"notebook_saved.Notebook\" from the
IPython notebook client JS."
(ein:log 'debug "Event: %S" event-type)
(ein:aif (gethash event-type (oref events :callbacks))
(mapc (lambda (cb-arg) (ein:funcall-packed cb-arg data)) it))
;; FIXME! the following must be put together in the event frame work.
(case (cdr event-type)
(Kernel
(oset events :status-kernel (car event-type)))
@ -101,6 +106,24 @@ IPython notebook client JS."
(t
(ein:log 'info "Unknown event: %S" event-type))))
(defmethod ein:events-on ((events ein:events) event-type
callback &optional arg)
"Set event trigger hook.
When EVENT-TYPE is triggered on the event handler EVENTS,
CALLBACK is called. CALLBACK must take two arguments:
ARG as the first argument and DATA, which is passed via
`ein:events-trigger', as the second."
(assert (and (consp event-type)
(symbolp (car event-type))
(symbolp (cdr event-type))))
(let* ((table (oref events :callbacks))
(cbs (gethash event-type table)))
(push (cons callback arg) cbs)
(puthash event-type cbs table)))
(provide 'ein-events)
;;; ein-events.el ends here