package luv

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file FS_event.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(* This file is part of Luv, released under the MIT license. See LICENSE.md for
   details, or visit https://github.com/aantron/luv/blob/master/LICENSE.md. *)



type t = [ `FS_event ] Handle.t

module Event =
struct
  type t = [
    | `RENAME
    | `CHANGE
  ]

  let to_c = let open C.Types.FS_event.Event in function
    | `RENAME -> rename
    | `CHANGE -> change

  let all = [
    `RENAME;
    `CHANGE;
  ]
end

let init ?loop () =
  let event = Handle.allocate C.Types.FS_event.t in
  C.Functions.FS_event.init (Loop.or_default loop) event
  |> Error.to_result event

let trampoline =
  C.Functions.FS_event.get_trampoline ()

let start
    ?(watch_entry = false)
    ?(stat = false)
    ?(recursive = false)
    event
    path
    callback =

  let flags =
    let accumulate = Helpers.Bit_field.accumulate in
    0
    |> accumulate C.Types.FS_event.Flag.watch_entry watch_entry
    |> accumulate C.Types.FS_event.Flag.stat stat
    |> accumulate C.Types.FS_event.Flag.recursive recursive
  in
  Handle.set_reference event begin fun filename events result ->
    let result =
      Error.to_result_lazy (fun () ->
        filename, Helpers.Bit_field.c_to_list Event.to_c Event.all events)
        result
    in
    Error.catch_exceptions callback result
  end;
  let immediate_result =
    C.Functions.FS_event.start
      event trampoline (Ctypes.ocaml_string_start path) flags
  in
  if immediate_result < 0 then
    callback (Error.result_from_c immediate_result)

let stop event =
  C.Functions.FS_event.stop event
  |> Error.to_result ()
OCaml

Innovation. Community. Security.