Library
Module
Module type
Parameter
Class
Class type
Simple event library
The main loop goes like this
type top_event =
| NotForMe of Component.Event.t
| Echo of string
let echo : top_event Event.t =
On.line Unix.stdin (function
| Ok s -> Echo s
| Error _ -> Echo "error")
let handle_event = function
| NotForMe e ->
Component.handle_event e |>
List.map (Event.map (fun x -> NotForMe x))
| Echo text ->
Printf.eprintf "echo: %s\n" text;
[echo]
let rec loop evs =
let ready, evs = Todo.pop evs in
let new_evs = handle_event ready in
loop (Todo.add evs new_evs)
let main () =
loop (Todo.add Todo.empty [echo; ...])
module Event : sig ... end
The type of events and operations (setting priority, cancelling, ...)
module On : sig ... end
Events one can wait for (read data, pull from queues, ...)
val now : ?priority:int -> ?name:string -> 'a -> 'a Event.t
mix a regular computations with blocking events. E.g. to make a fold
interruptible one can do something like:
type event =
| Fold of (int -> int -> int) * int * int list
| Other
let handle_event = function
| Other -> ...
| Fold(_,acc,[]) -> Printf "done: %s\n" acc; []
| Fold(f,acc,x::xs) -> [now (Fold (f, f acc x, xs))
let main =
let rec loop todo =
match pop_opt todo with
| None -> exit 0
| Some e ->
let es = handle_event e in
loop (Todo.add todo es)
in
loop (Todo.add Todo.empty [now (Fold((+),0,[1;2;3])); ...])
module Todo : sig ... end
Set of events being waited for
wait for one event. If more are ready, return the one with higher priority.
same as pop_opt
but retuning a None
if no event is ready in stop_after_being_idle_for
seconds. Precision is about a tenth of a second.
waits until some event is ready. All ready events with the lowest priority are returned.