-
Notifications
You must be signed in to change notification settings - Fork 1
/
PureImplementation.re
51 lines (42 loc) · 1.33 KB
/
PureImplementation.re
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
/** Demo that shows experimental purely functional implementation of the {{: https:/www.cl.cam.ac.uk/~arb33/papers/KleppmannBeresford-CRDT-JSON-TPDS2017.pdf} original paper}. It is much smaller than Automerge, however it lacks a lot of features and performance improvements Automerge added on top of the article. */
let logState = s => {
let reformat: string => string = [%bs.raw
s => "return JSON.stringify(JSON.parse(s), null, 3);"
];
Js.log(reformat(Json.stringify(AMPure.encodeState(s))));
};
let docA = AMPure.make("aaaaaaaa");
logState(docA);
let docA =
AMPure.(
docA
|> makeAssign(doc, EmptyObject)
|> makeAssign(doc->get("AAA"), Int(1111))
|> makeAssign(doc->get("BBB"), Int(2222))
|> makeDelete(doc->get("AAA"))
);
let ops = docA |> AMPure.send;
let docB =
AMPure.(
make("bbbbbbbbb")
|> makeAssign(doc, EmptyObject)
|> makeAssign(doc->get("CCC"), EmptyArray)
);
let docB =
AMPure.(docB |> makeInsert(doc->get("CCC")->idx(0, docB), Int(42)));
let docB =
AMPure.(
docB
|> makeAssign(doc->get("CCC")->idx(1, docB), Int(43))
|> recv(ops)
);
let ops =
AMPure.(
docA
|> recv(docB |> send)
|> makeAssign(doc->get("BBB"), Int(333))
|> send
);
let docB = AMPure.(docB |> makeAssign(doc->get("BBB"), Int(444)));
let docB = AMPure.(docB |> recv(ops));
logState(docB);