mirror of
https://github.com/c-cube/sidekick.git
synced 2025-12-05 19:00:33 -05:00
add Event abstraction in Util
This commit is contained in:
parent
633a658e0c
commit
833fa8e038
2 changed files with 26 additions and 0 deletions
14
src/util/Event.ml
Normal file
14
src/util/Event.ml
Normal file
|
|
@ -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
|
||||
12
src/util/Event.mli
Normal file
12
src/util/Event.mli
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Reference in a new issue