package sel

  1. Overview
  2. Docs
Legend:
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

val pop : 'a Todo.t -> 'a * 'a Todo.t

wait for one event. If more are ready, return the one with higher priority.

  • raises Failure

    when there is nothing left to do

val pop_opt : 'a Todo.t -> 'a option * 'a Todo.t

same as pop but retuning an option

val pop_timeout : stop_after_being_idle_for:float -> 'a Todo.t -> 'a option * 'a Todo.t

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.

val wait : 'a Todo.t -> 'a list * 'a Todo.t

waits until some event is ready. All ready events with the lowest priority are returned.

val wait_timeout : stop_after_being_idle_for:float -> 'a Todo.t -> 'a list * 'a Todo.t

Same as wait but returns empty lists if no event is ready in stop_after_being_idle_for seconds. Precision is about a tenth of a second.

OCaml

Innovation. Community. Security.