Skip to content

Commit

Permalink
next, switch_be, until
Browse files Browse the repository at this point in the history
  • Loading branch information
duckpilot committed Apr 22, 2010
1 parent fdbdaa7 commit f5fad76
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
26 changes: 25 additions & 1 deletion src/froc/froc.ml
Expand Up @@ -92,6 +92,19 @@ let hash_event t =
| Never -> 0
| Occurs o -> o.e_id

let next t =
let t', s' = make_event () in
let c = ref (ignore in
c :=
notify_result_e_cancel t
(fun r ->
cancel !c;
c := ignore;
send_result s' r;
(* XXX future deps are still added; would be better to become Never *)
Dlist.clear (occurs_of_event_sender s').e_deps);
t'

let merge ts =
let t, s = make_event () in
List.iter (fun t' -> notify_result_e t' (send_result s)) ts;
Expand Down Expand Up @@ -173,7 +186,18 @@ let notify_result_b_cancel = notify_result_cancel

let hash_behavior = hash

let switch bb = bb >>= fun b -> b
let switch_bb ?eq bb = bind ?eq bb (fun b -> b)

let switch_be ?eq b e =
let bt, bu = make_changeable ?eq () in
let c = ref (connect_cancel bu b) in
notify_result_e e
(function
| Value b -> cancel !c; c := connect_cancel bu b
| Fail e -> cancel !c; write_exn bu e);
bt

let until ?eq b e = switch_be ?eq b (next e)

let hold_result ?eq init t =
let bt, bu = make_changeable ?eq ~result:init () in
Expand Down
26 changes: 19 additions & 7 deletions src/froc/froc.mli
Expand Up @@ -218,6 +218,9 @@ type -'a event_sender
val make_event : unit -> 'a event * 'a event_sender
(** Makes a new channel for events of type ['a]. *)

val never : 'a event
(** An event which never occurs. *)

val notify_e : 'a event -> ('a -> unit) -> unit
(**
Adds a listener on the channel, which is called whenever a value
Expand Down Expand Up @@ -253,13 +256,8 @@ val send_exn : 'a event_sender -> exn -> unit
val send_result : 'a event_sender -> 'a result -> unit
(** [send_result e r] calls the listeners of the associated event with [r]. *)

val hash_event : 'a event -> int
(** A hash function for events. *)

(** {2 Derived operations} *)

val switch : 'a behavior behavior -> 'a behavior
(** [switch b] behaves as whichever behavior is currently the value of [b]. *)
val next : 'a event -> 'a event
(** [next e] fires just the next occurence of [e]. *)

val merge : 'a event list -> 'a event
(** [merge es] is an event that fires whenever any of the events in [e] fire. *)
Expand All @@ -278,6 +276,20 @@ val collect : ('b -> 'a -> 'b) -> 'b -> 'a event -> 'b event
state.
*)

val hash_event : 'a event -> int
(** A hash function for events. *)

(** {2 Derived operations} *)

val switch_bb : ?eq:('a -> 'a -> bool) -> 'a behavior behavior -> 'a behavior
(** [switch_bb b] behaves as whichever behavior is currently the value of [b]. *)

val switch_be : ?eq:('a -> 'a -> bool) -> 'a behavior -> 'a behavior event -> 'a behavior
(** [switch_be b e] behaves as [b] until [e] fires, then behaves as the last value of [e]. *)

val until : ?eq:('a -> 'a -> bool) -> 'a behavior -> 'a behavior event -> 'a behavior
(** [until b e] behaves as [b] until [e] fires [b'], then behaves as [b'] *)

val hold : ?eq:('a -> 'a -> bool) -> 'a -> 'a event -> 'a behavior
(**
[hold v e] behaves as the last value fired by [e], or [v] if [e]
Expand Down

0 comments on commit f5fad76

Please sign in to comment.