Equeueimplements generic event queues. An event system consists of an event queue, a list of event handlers, and an event source. The queue has FIFO semantics, i.e. the first event is processed next, and new events are appended to its end. When an event is delivered to an event handler, all handlers are tried until a handler accepts the event (or the event is dropped if no such handler can be found). See below how a handler can indicate whether to accept or reject an event.
When the queue is empty, the event source is called once. The source
can add events to the queue, in order to continue event processing.
If the queue remains empty, the execution of the event system
The module can be used in multi-threaded program provided no
event system is shared by several threads, or if so, access to
functions is serialized.
runwhen the event source adds new events to the queue but there are no event handlers to process them
val create :
?string_of_event:('a -> string) -> ('a t -> unit) -> 'a t
'a t -> unitis the event source. The source can call
add_eventto put new events into the queue.
string_of_event: Optionally, one can pass a printer for events. This has only an effect for debugging output.
val add_event :
'a t -> 'a -> unit
val add_handler :
'a t -> ('a t -> 'a -> unit) -> unit
An event handler is called with the event system and the event as arguments. The handler can return in various ways:
Reject: The event is rejected by the handler. The other handlers are asked to process the event.
Terminate: The event is accepted, but the handler is terminated, i.e. will never be called again.
val run :
'a t -> unit
runinvocation and all later added events are gone through. Each event is presented to the handlers until one handler accepts the event. Events rejected by all handlers are dropped silently. If there is no pending event the default event source is called once. If there are still no events the system stops and returns. If there are events to process but no handlers which can do them all events are silently dropped, and the default event source is called once.
Out_of_handlers is raised if there are events but no
handlers after the event source has been called. This is considered
as a programming error, and would cause infinite looping if not
Note that there is an implicit order among the handlers which is simply the order the handlers have been added to the system. This means that you can set a fallback handler which catches any unprocessed event by adding it last.
Note that the events are processed in the order they happen. There is no mechanism to assign priorities to events.
Handlers are allowed to raise arbitrary exceptions. Exceptions other
than Reject and Terminate are not caught, so the caller has to do this
if appropriate. It is possible to restart an event system by just
val is_running :
'a t -> bool