module Mailbox:sig..end
Sending arbitrary messages to widgets
Working with messages can go a long way simplifying an intricate GUI.
Technically, Mailboxes are build upon Bogues's connections, and can be
seen as a (semantic) extension to the simple Update mechanism. Mailboxes
can be useful
WARNING: new API, not stabilized yet.
type 'a mbx
val create : ?owner:Widget.t -> unit -> 'a mbxCreate an empty mailbox which will receive messages of type
'a.
Specifying an owner widget is optional (and may disappear in future
API); it could help structuring the code: we think of it as "the
widget that handles the incoming mail".
Once a mailbox is created, anyone can send messages to it using
Mailbox.send. However the mail will be effectively handled only once
Mailbox.activate is called.
val activate : ?sync:bool -> 'a mbx -> ('a -> unit) -> unitactivate mbx handler sets the owner widget ready to receive messages,
and each received message will be handled by the handler function.
By default, "the mailman delivers at each frame": messages are handled one
by one in the Sync queue, in the order of reception (FIFO). Setting
sync=false will on the contrary execute the handler in a separate
thread. (Only one thread per mailbox.)
Note that all messages are transfered to the handler as soon as the first delivery occurs, so in principle the handler is authorized itself to send new messages to its own mailbox. Of course if all messages trigger sending a new one, this will loop forever.
val send : 'a mbx -> 'a -> unitsend mbx msg sends the message msg to the mailbox mbx.
Recall that all messages sent to a given mailbox must be of the same type. It can be handy to use polymorphic variants like
send mbx (`Start_new_level 7)
Even if the mailbox is not activated, or disabled, messages can still be sent: they accumulate in the mailbox queue, and will all be handled once the mailbox is activated or re-enabled.
val enable : 'a mbx -> unit
val disable : 'a mbx -> unit
val clear : 'a mbx -> unit