-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge event system into engine. Simplify event-related interfaces
- Loading branch information
Showing
19 changed files
with
190 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
(in-package :cl-bodge.events) | ||
|
||
|
||
;; | ||
(defclass handler-registry (lockable) | ||
((handler-table :initform (make-hash-table)))) | ||
|
||
|
||
(defun invoke-handlers (reg event) | ||
(with-slots (handler-table) reg | ||
(destructuring-bind (&optional handler-lock &rest handlers) | ||
(with-instance-lock-held (reg) | ||
(gethash (class-of event) handler-table)) | ||
(when handler-lock | ||
(flet ((acquire-rest (list) | ||
(bt:with-recursive-lock-held (handler-lock) | ||
(rest list)))) | ||
(loop for handler in handlers by #'acquire-rest | ||
do (etypecase handler | ||
(function (funcall handler event)) | ||
(symbol (funcall (symbol-function handler) event))))))))) | ||
|
||
|
||
(defun register-handler (reg event-class handler) | ||
(with-slots (handler-table) reg | ||
(with-instance-lock-held (reg) | ||
(push handler (cdr (gethash event-class | ||
handler-table | ||
(list (bt:make-recursive-lock "handler-list-lock")))))))) | ||
|
||
|
||
(defun remove-handler (reg event-class handler) | ||
(with-slots (handler-table) reg | ||
(with-instance-lock-held (reg) | ||
(when-let ((handler-list (gethash event-class handler-table))) | ||
(deletef (cdr handler-list) handler))))) | ||
|
||
|
||
|
||
;; | ||
(defclass event-emitter () | ||
((handler-registry :initform (make-instance 'handler-registry)))) | ||
|
||
|
||
(defun fire-event (event emitter) | ||
(with-slots (handler-registry) emitter | ||
(invoke-handlers handler-registry event))) | ||
|
||
|
||
(defgeneric subscribe-to (event-class-name emitter handler) | ||
(:method (event-class-name (emitter event-emitter) handler) | ||
(with-slots (handler-registry) emitter | ||
(let ((event-class (find-class event-class-name))) | ||
(register-handler handler-registry event-class handler))) | ||
handler)) | ||
|
||
|
||
(defgeneric unsubscribe-from (event-class-name emitter handler) | ||
(:method (event-class-name (emitter event-emitter) handler) | ||
(with-slots (handler-registry) emitter | ||
(let ((event-class (find-class event-class-name))) | ||
(remove-handler handler-registry event-class handler))) | ||
handler)) | ||
|
||
|
||
(defmacro %with-accessor-bindings ((accessor-bindings event-var) &body body) | ||
(let ((bindings (loop for binding in accessor-bindings | ||
for (name accessor) = (if (and (listp binding) (second binding)) | ||
binding | ||
(list binding (symbolicate binding '-from))) | ||
collect `(,name (,accessor ,event-var))))) | ||
`(symbol-macrolet ,bindings | ||
,@body))) | ||
|
||
|
||
(defmacro subscribe-body-to ((event-class emitter (&rest accessor-bindings) | ||
&optional (event-var (gensym))) | ||
&body body) | ||
`(subscribe-to ',event-class ,emitter | ||
(lambda (,event-var) | ||
(declare (ignorable ,event-var)) | ||
(%with-accessor-bindings (,accessor-bindings ,event-var) | ||
,@body)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
(in-package :cl-bodge.events) | ||
|
||
;;; | ||
;;; | ||
;;; | ||
(defclass event () ()) | ||
|
||
|
||
(defmacro defevent (name (&rest superclass-names) (&rest field-names) &rest class-options) | ||
(let ((constructor-name (symbolicate 'make- name))) | ||
`(progn | ||
(defclass ,name (,@superclass-names) | ||
(,@(loop for field-name in field-names collecting | ||
`(,field-name :initarg ,(make-keyword field-name) | ||
:initform (error "~a must be provided" ',field-name) | ||
:reader ,(symbolicate field-name '-from)))) | ||
,@class-options) | ||
(declaim (inline ,constructor-name)) | ||
(defun ,constructor-name (,@field-names) | ||
(make-instance ',name ,@(loop for field-name in field-names appending | ||
`(,(make-keyword field-name) ,field-name))))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
(in-package :cl-bodge.events) | ||
|
||
|
||
(defclass event-listener () | ||
((callbacks :initform (list)))) | ||
|
||
|
||
(defun register-event-handler (event-listener event-class-name handler emitter) | ||
(with-slots (callbacks) event-listener | ||
(push (list event-class-name emitter handler) callbacks))) | ||
|
||
|
||
(defun subscribe-listener (event-listener) | ||
(with-slots (callbacks) event-listener | ||
(dolist (args callbacks) | ||
(apply #'subscribe-to args)))) | ||
|
||
|
||
(defun unsubscribe-listener (event-listener) | ||
(with-slots (callbacks) event-listener | ||
(dolist (args callbacks) | ||
(apply #'unsubscribe-from args)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.