Skip to content

Commit

Permalink
Ensure the watcher doesn't get garbage-collected.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjbray committed Apr 18, 2018
1 parent 77510e3 commit 1ead43b
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions src/backend_osx.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
open Lwt
open Types

let watcher_ref = ref None

let make_watcher
?(debug = false)
Expand All @@ -11,25 +12,19 @@ let make_watcher

let create_flags = Fsevents.CreateFlags.detailed_interactive in
let watcher = Fsevents_lwt.create 0. create_flags (CCList.map (fun { local_dir } -> local_dir ) path_configs) in
let event_stream = Fsevents_lwt.event_stream watcher in
let stream = Fsevents_lwt.stream watcher in
let () = watcher_ref := Some watcher in (* Don't GC me! *)
let run_loop_mode = Cf.RunLoop.Mode.Default in

let cb_f = ref (fun event ->
Cf_lwt.RunLoop.run_thread (fun runloop ->
Fsevents_lwt.schedule_with_run_loop watcher runloop run_loop_mode;
if not (Fsevents_lwt.start watcher)
then prerr_endline "Failed to start FSEvents stream"
) >>= fun _rl ->

Fsevents_lwt.stream watcher
|> Lwt_stream.iter_s (fun event ->
(* TODO: handle path mapping back to server path *)
let local_path = event.Fsevents_lwt.path in
debug_log_lwt (Printf.sprintf "[livereload (osx-fsevents)] %s%!" local_path) >>= fun _ ->
change_cb local_path >>= fun _ ->
Lwt.return ())
in

Lwt.async (fun () -> Lwt_stream.iter_s !cb_f stream);

let rt_f = ref (fun runloop ->
Fsevents.schedule_with_run_loop event_stream runloop run_loop_mode;
if not (Fsevents.start event_stream)
then prerr_endline "Failed to start FSEvents stream"
)
in

Cf_lwt.RunLoop.run_thread !rt_f; >>= fun _ -> Lwt.return ();

0 comments on commit 1ead43b

Please sign in to comment.