-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_processor_client.ml
More file actions
80 lines (67 loc) · 2.47 KB
/
message_processor_client.ml
File metadata and controls
80 lines (67 loc) · 2.47 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
module M_p = Message_processor
module type Processor_core = sig
include M_p.Processor_core
val render: ?title: string -> client_state -> config: config -> Reactjs.Low_level_bindings.react_element Js.t
end
module type Processor = sig
include M_p.Processor
module Client: sig
type t
val print_state: t -> unit
val create: ?name: string -> ?title: string -> config -> int -> t
val get_name: t -> string
val get_title: t -> string option
end
val set_client_state: Client.t -> Yojson.Safe.json -> int -> (Client.t, string) result
val update_client_state: Client.t -> Yojson.Safe.json -> int -> (Client.t, string) result
val get_react_class: Client.t -> Client.t Bus.t -> Reactjs.Low_level_bindings.react_class Js.t
end
module Make_processor (Core: Processor_core) = struct
include M_p.Make_processor(Core)
module Client = struct
type t = {
c: config;
name: string;
order: int;
title: string option;
state: Core.client_state;
version: int
}
let print_state t =
print_string (Yojson.Safe.to_string (Core.client_state_to_yojson t.state));
flush_all ()
let create ?name ?title config order = {
c = config;
name = (match name with
| Some str -> str
| None -> Core.default_name);
order;
title = title;
state = Core.client_state_empty config;
version = 0
}
let get_name { name } = name
let get_title { title } = title
end
let set_client_state t json version =
match Core.client_state_of_yojson json with
| Ok state -> Ok {
t with
Client.state; version
}
| Error _ as err -> err
let update_client_state t json version =
if t.Client.version <> version - 1 then Error "Version mismatch"
else match Core.update_of_yojson json with
| Ok update -> Ok {
t with
Client.state = Core.update_client_state t.Client.state update;
version
}
| Error _ as err -> err
let get_react_class { Client.title; Client.c } bus =
let render_func state =
Core.render state.Client.state ?title ~config: c in
let component_class, _ = Util_react.component_bus ~bus render_func in
component_class
end