@@ -14,52 +14,73 @@ type ('a, 'r) id = Yes : ('a, 'a) id | No : ('a, 'r) id
1414
1515let rec request_1_as :
1616 type a r . (_ -> r ) Computation. t -> (a -> r ) -> (a , r ) id -> a t -> _ =
17- fun computation to_result id -> function
18- | Request { request } -> request computation to_result
19- | Choose ts -> request_n_as computation to_result id ts
17+ fun target to_result id -> function
18+ | Request { request } -> request target to_result
19+ | Choose ts -> request_n_as target to_result id ts
2020 | Wrap { event; fn } ->
2121 let to_result =
2222 match id with No -> fun x -> to_result (fn x) | Yes -> fn
2323 in
24- request_1_as computation to_result No event
24+ request_1_as target to_result No event
2525
2626and request_n_as :
2727 type a r . (_ -> r ) Computation. t -> (a -> r ) -> (a , r ) id -> a t list -> _ =
28- fun computation to_result id -> function
28+ fun target to_result id -> function
2929 | [] -> ()
3030 | t :: ts ->
31- request_1_as computation to_result id t;
32- request_n_as computation to_result id ts
31+ request_1_as target to_result id t;
32+ request_n_as target to_result id ts
3333
3434type ('a, _) tycon = Id : ('a , 'a t ) tycon | List : ('a , 'a t list ) tycon
3535
3636let sync_as : type a n. n -> (a, n) tycon -> a =
3737 fun t n ->
38- let computation = Computation. create ~mode: `LIFO () in
38+ let target = Computation. create ~mode: `LIFO () in
3939 match
4040 match n with
41- | Id -> request_1_as computation Fun. id Yes t
42- | List -> request_n_as computation Fun. id Yes t
41+ | Id -> request_1_as target Fun. id Yes t
42+ | List -> request_n_as target Fun. id Yes t
4343 with
4444 | () ->
45- if Computation. is_running computation then begin
45+ if Computation. is_running target then begin
4646 let t = Trigger. create () in
47- if Computation. try_attach computation t then
47+ if Computation. try_attach target t then
4848 match Trigger. await t with
4949 | None -> ()
5050 | Some exn_bt ->
51- if Computation. try_cancel computation exn_bt then
52- Exn_bt. raise exn_bt
51+ if Computation. try_cancel target exn_bt then Exn_bt. raise exn_bt
5352 end ;
54- Computation. await computation ()
53+ Computation. await target ()
5554 | exception exn ->
5655 let exn_bt = Exn_bt. get exn in
57- Computation. cancel computation exn_bt;
56+ Computation. cancel target exn_bt;
5857 Exn_bt. raise exn_bt
5958
6059let guard create_event =
61- let request computation to_result =
62- request_1_as computation to_result No (create_event () )
60+ let request target to_result =
61+ request_1_as target to_result No (create_event () )
62+ in
63+ Request { request }
64+
65+ let [@ alert " -handler" ] from_computation source =
66+ let request target to_result =
67+ let result () = to_result (Computation. await source) in
68+ if Computation. is_running source then begin
69+ let propagator =
70+ Trigger. from_action result target @@ fun _ result target ->
71+ Computation. return target result
72+ in
73+ if Computation. try_attach source propagator then begin
74+ let detacher =
75+ Trigger. from_action propagator source @@ fun _ propagator source ->
76+ Computation. detach source propagator
77+ in
78+ if not (Computation. try_attach target detacher) then
79+ Computation. detach source propagator
80+ end
81+ else Computation. return target result
82+ end
83+ else Computation. return target result
6384 in
6485 Request { request }
6586
0 commit comments