anchor.diff:            :documentation "The hinge instance the event emitter functions within.")
   
      (oneshots :accessor oneshots)
  -   (listeners :accessor listeners)
  -
  -   (events :accessor events
  -           :initform (make-instance 'queue :size 20)
  -           :documentation "A queue of emitted events. Drained with an interval
  -to avoid blocking the event loop delivering events.")
  -   (runner :accessor event-runner
  -           :initform nil
  -           :documentation "A watcher that runs through the event queue
  -delivering events to listeners.")))
  +   (listeners :accessor listeners)))
   
   (defgeneric reset (emitter)
     (:documentation "Reset the emitter to its empty initial state.")
  
positive.code: ;; Generics
  (defgeneric emit (emitter event &rest args)
    (:documentation "Emit `event' on the `emitter', invoking callbacks, if any, with `args'"))
  
  (defgeneric add-listener (emitter event callback)
    (:documentation "Add a listener to `emitter' so when `event'
  
negative.code:   (:documentation "remove either all listeners on `emitter' or just those bound to `event' if given."))
  
  ;; Methods
  (defmethod deliver ((emitter emitter) event args)
    (let ((registered (gethash event (listeners emitter))))
      (flet ((queue-cb (cb)
               (when (remhash cb (oneshots emitter))
                 (remove-listener emitter event cb))
               (enqueue (deliver-queue (owner emitter)) (lambda () (apply cb args)))))
        (mapc #'queue-cb registered))))
  
  (defmethod deliver :after ((emitter emitter) event args)
    (declare (ignore event args))
    (flet ((deliver-cb (l w e)
             (declare (ignore l e))
             (format t "Deliver queue: ~S~%" (deliver-queue (owner emitter)))
             (if-let (thunk (dequeue (deliver-queue (owner emitter))))
               (funcall thunk)
               (ev:stop-watcher (owner emitter) w :keep-callback t))))
  
      (unless (deliver-runner (owner emitter))
        (setf (deliver-runner (owner emitter))
              (let ((watcher (make-instance 'ev:ev-idle)))
                (ev:set-idle (owner emitter) watcher #'deliver-cb)
                (incf (ev:watcher-slot watcher :priority))
                watcher)))
  
      (ev:start-watcher (owner emitter) (deliver-runner (owner emitter)))))
  
  (defmethod emit ((emitter emitter) (event string) &rest args)
    "Enqueue the delivery of an `event' with `args'. The event will
  be delivered at some point in the future, but very soon, in the order
  that it was emitted relative to other events."
    (flet ((emit-thunk ()
             (deliver emitter event args)))
      (enqueue (emit-queue (owner emitter)) #'emit-thunk)))
  
  (defmethod emit :after ((emitter emitter) (event string) &rest args)
    (declare (ignore args))
    (flet ((emitter-cb (l w e)
             (declare (ignore l e))
             (format t "Emit queue: ~S~%" (emit-queue (owner emitter)))
             (if-let (thunk (dequeue (emit-queue (owner emitter))))
               (funcall thunk)
               (ev:stop-watcher (owner emitter) w :keep-callback t))))
  
      (unless (emit-runner (owner emitter))
        (setf (emit-runner (owner emitter))
              (let ((watcher (make-instance 'ev:ev-idle)))
                (ev:set-idle (owner emitter) watcher #'emitter-cb)
                watcher)))
  
      (ev:start-watcher (owner emitter) (emit-runner (owner emitter)))))
  
  (defmethod add-listener ((emitter emitter) (event string) (callback symbol))
    (add-listener emitter event (symbol-function callback)))
  