Event Emitter provides an event mechanism like Node.js for Common Lisp objects.
Mostly ported from Node.js 'events' module.
;; Defining Event Emitter class.
(defclass person (event-emitter)
((name :initarg :name
:reader name)))
(defvar *user*
(make-instance 'person :name "Eitaro Fukamachi"))
;; Attach a event listener for an event ':say-hi'.
(on :say-hi *user*
(lambda () (format t "Hi!")))
;; *user* says 'Hi!' when an event ':say-hi' is invoked.
(emit :say-hi *user*)
;-> Hi!
(emit :say-hi *user*)
;-> Hi!
(emit :say-hi *user*)
;-> Hi!
;; Attach an one time event listener.
(once :say-hi *user*
(lambda ()
(format t "How's it going?")))
;; 'Hi!' and "How's it going?" will be printed.
(emit :say-hi *user*)
;-> Hi!
; How's it going?
;; *user* doens't say "How's it going?" anymore.
(emit :say-hi *user*)
;-> Hi!
(emit :say-hi *user*)
;-> Hi!
There's already similar library named event-glue which you may like to know.
Base standard class for 'event-emitter's.
(defclass person (event-emitter)
((name :initarg :name)))
Base strucuture class for 'event-emitter's.
(defstruct (person :include event-emitter*)
name)
Adds a listener to the end of the listeners array for the specified event.
(on :connection server
(lambda (stream) ...))
NOTE: add-listener
and on
takes 'object' and 'event' the opposite order.
Adds a one time listener for the event. This listener is invoked only the next time the event is fired, after which it is removed.
(once :connection server
(lambda (stream) ...))
Removes a listener from the listener array for the specified event.
(defun connection-cb (stream)
...)
(on :connection server #'connection-cb)
(remove-listener server :connection #'connection-cb)
Removes all listeners, or those of the specified event.
(remove-all-listeners server)
(remove-all-listeners server :connection)
Returns an array of listeners for the specified event.
Executes each of the listeners in order with the supplied arguments.
Returns T
if the event had listeners, NIL
otherwise.
Returns the number of listeners for a given event.
Copyright (c) 2014 Eitaro Fukamachi (e.arrows@gmail.com)
Licensed under the BSD 2-Clause License.