diff --git a/lib_eio_js/browser/eio_browser.ml b/lib_eio_js/browser/eio_browser.ml index 9ebb0ee89..d76a6412e 100644 --- a/lib_eio_js/browser/eio_browser.ml +++ b/lib_eio_js/browser/eio_browser.ml @@ -85,10 +85,11 @@ module Scheduler = struct let stop t = Brr.Ev.unlisten t.listener - let wakeup = + (* A new message must be created for every call. *) + let wakeup t = let open Brr_io in let args = [| Ev.create Message.Ev.message |> Ev.to_jv |] in - fun t -> Jv.call (El.to_jv t.scheduler) "dispatchEvent" args |> ignore + Jv.call (El.to_jv t.scheduler) "dispatchEvent" args |> ignore let enqueue_thread t k v = Run_queue.push t.run_q (fun () -> Suspended.continue k v); @@ -124,7 +125,7 @@ let await fut = enter_unchecked @@ fun st k -> (* There is no way to cancel a Javascript promise (which Fut wraps) so we have to leak this memory unfortunately. *) - let cancelled = ref true in + let cancelled = ref false in Fiber_context.set_cancel_fn k.fiber (fun exn -> cancelled := true; Scheduler.enqueue_failed_thread st k exn); Fut.await fut (fun v -> if not !cancelled then begin diff --git a/lib_eio_js/browser/test/test.ml b/lib_eio_js/browser/test/test.ml index 3c0f8ca2c..a7ee9f7e9 100644 --- a/lib_eio_js/browser/test/test.ml +++ b/lib_eio_js/browser/test/test.ml @@ -56,6 +56,12 @@ module Browser_tests = struct in Alcotest.(check string) "timeout cancelled" "B" v + let test_fut_await () = + let p, r = Fut.create () in + Fiber.both + (fun () -> Eio_browser.await p) + (fun () -> r ()) + let test_fut_cancel () = let p, _ = Fut.create () in let v = @@ -76,6 +82,7 @@ module Browser_tests = struct let tests = [ Alcotest.test_case "timeout cancelled" `Quick test_timeout_cancel; + Alcotest.test_case "fut await" `Quick test_fut_await; Alcotest.test_case "fut cancelled" `Quick test_fut_cancel; Alcotest.test_case "test timeout" `Quick test_timeout; Alcotest.test_case "test multiple timeouts" `Quick test_multiple_timeouts