You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
21 lines
573 B
21 lines
573 B
3 years ago
|
(defpackage #:observable
|
||
|
(:use #:cl)
|
||
|
(:export :observable :on :off :notify))
|
||
|
|
||
|
(in-package :observable)
|
||
|
|
||
|
(defclass observable ()
|
||
|
((observers :accessor observers
|
||
|
:initform (make-hash-table))))
|
||
|
|
||
|
(defmethod on ((self observable) event f)
|
||
|
(push f (gethash event (observers self))))
|
||
|
|
||
|
(defmethod off ((self observable) event f)
|
||
|
(setf (gethash event (observers self))
|
||
|
(remove f (gethash event (observers self)))))
|
||
|
|
||
|
(defmethod notify ((self observable) event &optional value)
|
||
|
(loop for observer in (gethash event (observers self))
|
||
|
do (funcall observer value)))
|