Source file Plugin_state.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

(** {1 Manage state} *)

type t = {
  actions: Plugin.action_callback;
}

let cmd_reload st =
  let open Lwt.Infix in
  Command.make_simple
    ~descr:"reload state from disk"
    ~prio:10
    ~cmd:"reload"
    (fun _ _ ->
       Signal.Send_ref.send st.actions Plugin.Require_reload >|= fun () ->
       Some (Talk.select Talk.Ack)
    )

let cmd_save st =
  let open Lwt.Infix in
  Command.make_simple
    ~descr:"save state to disk"
    ~prio:10
    ~cmd:"save"
    (fun _ _ ->
       Signal.Send_ref.send st.actions Plugin.Require_save >|= fun () ->
       Some (Talk.select Talk.Ack)
    )

let plugin =
  Plugin.stateful
    ~name:"state"
    ~of_json:(fun actions _ -> Lwt_err.return {actions})
    ~to_json:(fun _ -> None)
    ~stop:(fun _ -> Lwt.return_unit)
    ~commands:(fun st -> [ cmd_reload st; cmd_save st; ])
    ()