Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

it's OK to sample behaviors directly

  • Loading branch information...
commit 70ebf62b915dc603d0f2ed1ab550480940758193 1 parent ee7ccd0
Jake Donham authored
Showing with 23 additions and 36 deletions.
  1. +9 −9 src/froc-dom/froc_dom.ml
  2. +3 −21 src/froc/froc.ml
  3. +11 −6 src/froc/froc.mli
View
18 src/froc-dom/froc_dom.ml
@@ -62,15 +62,15 @@ let delay_eb t msb =
let e, s = make_event () in
let rec de = { l_val = Fail Exit; l_next = de } in
let de_next = ref de in
- let t_msb = sample (fun e msb -> e, msb) t msb in
- notify_result_e t_msb begin function
- | Fail _ as r ->
- de_next := { l_val = r; l_next = !de_next};
- send_delayed_event s !de_next
- | Value (v, ms) ->
- let de = { l_val = Value v; l_next = !de_next } in
- de_next := de;
- ignore (Dom.window#setTimeout (fun () -> send_delayed_event s de) ms)
+ notify_result_e t begin fun r ->
+ match sample_result msb with
+ | Fail _ as r ->
+ de_next := { l_val = r; l_next = !de_next};
+ send_delayed_event s !de_next
+ | Value ms ->
+ let de = { l_val = r; l_next = !de_next } in
+ de_next := de;
+ ignore (Dom.window#setTimeout (fun () -> send_delayed_event s de) ms)
end;
e
View
24 src/froc/froc.ml
@@ -179,6 +179,9 @@ let join_e ee =
type 'a behavior = 'a t
+let sample = read
+let sample_result = read_result
+
let notify_b = notify
let notify_b_cancel = notify_cancel
let notify_result_b = notify_result
@@ -212,27 +215,6 @@ let changes b =
notify_result_b ~current:false b (write_temp_result u);
t
-let sample f e b =
- if is_never e then never else
- if is_constant b then
- match read_result b with
- | Fail ex -> map (fun _ -> raise ex) e
- | Value bv -> map (fun ev -> f ev bv) e
- else
- let t, u = make_event () in
- let notify = ref false in
- add_reader2 e b begin fun () ->
- if not !notify then notify := true
- else
- match read_result e, read_result b with
- | Fail Unset, _ -> ()
- | Fail e, _
- | _, Fail e -> write_temp_result u (Fail e)
- | Value ve, Value vb ->
- try write_temp_result u (Value (f ve vb)) with e -> write_temp_result u (Fail e)
- end;
- t
-
let when_true b =
map (fun b -> ()) (filter (fun b -> b) (changes b))
View
17 src/froc/froc.mli
@@ -102,6 +102,17 @@ val lift : ?eq:('b -> 'b -> bool) -> ('a -> 'b) -> 'a behavior -> 'b behavior
binding it to a behavior.
*)
+val sample : 'b behavior -> 'b
+ (**
+ [sample b] returns the current value of [b], or raises [b]'s
+ exception if it is failed.
+ *)
+
+val sample_result : 'b behavior -> 'b result
+ (**
+ Same as [sample] but returns a result.
+ *)
+
val catch : ?eq:('a -> 'a -> bool) -> (unit -> 'a behavior) -> (exn -> 'a behavior) -> 'a behavior
(**
[catch b f] behaves the same as [b()] if [b()] succeeds. If [b()]
@@ -301,12 +312,6 @@ val hold : ?eq:('a -> 'a -> bool) -> 'a -> 'a event -> 'a behavior
val changes : 'a behavior -> 'a event
(** [changes b] fires the value of [b] whenever it changes. *)
-val sample : ('a -> 'b -> 'c) -> 'a event -> 'b behavior -> 'c event
- (**
- [sample f e b] fires [f ev bv] (where [ev] and [bv] are the values of
- [e] and [b]) when [e] fires.
- *)
-
val when_true : bool behavior -> unit event
(** [when_true b] fires whenever [b] becomes true. *)
Please sign in to comment.
Something went wrong with that request. Please try again.