Permalink
Browse files

next, switch_be, until

  • Loading branch information...
1 parent fdbdaa7 commit f5fad76550aba6f976cb0f39f72679f0cdd75556 Jake Donham committed Apr 22, 2010
Showing with 44 additions and 8 deletions.
  1. +25 −1 src/froc/froc.ml
  2. +19 −7 src/froc/froc.mli
View
@@ -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;
@@ -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
View
@@ -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
@@ -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. *)
@@ -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]

0 comments on commit f5fad76

Please sign in to comment.