diff --git a/src/util/Event.ml b/src/util/Event.ml new file mode 100644 index 00000000..4bee61c0 --- /dev/null +++ b/src/util/Event.ml @@ -0,0 +1,14 @@ +type 'a handler = 'a -> unit +type 'a t = { h: 'a handler Vec.t } [@@unboxed] + +let nop_handler_ = ignore + +module Emitter = struct + type nonrec 'a t = 'a t + + let emit (self : _ t) x : unit = Vec.iter self.h ~f:(fun h -> h x) + let create () : _ t = { h = Vec.make 3 nop_handler_ } +end + +let on self f = Vec.push self.h f +let of_emitter x = x diff --git a/src/util/Event.mli b/src/util/Event.mli new file mode 100644 index 00000000..12599ef8 --- /dev/null +++ b/src/util/Event.mli @@ -0,0 +1,12 @@ +type 'a t +(** An event emitting values of type ['a] *) + +module Emitter : sig + type 'a t + + val emit : 'a t -> 'a -> unit + val create : unit -> 'a t +end + +val on : 'a t -> ('a -> unit) -> unit +val of_emitter : 'a Emitter.t -> 'a t