-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTightJsApiDemo.re
83 lines (71 loc) · 2.08 KB
/
TightJsApiDemo.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
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
81
82
83
/** A demo for provided API that closely copies the usage experience of the original JavaScript library, including mutation inside [change] functions. */;
open InfixRe;
module AM = Automerge.Js;
let d1 =
AM.(
initWithActorId("aaaaaaaa")
->change("test", root => {
root->Js.Dict.set(
"ufoo",
Js.Json.(
array([|
number(42.0),
number(20.0),
number(11.0),
number(19.0),
|])
),
);
root->Js.Dict.set("asdf", Js.Json.string("asdf"));
})
);
let d2a =
AM.(
initWithActorId("bbbbbbbb")
->merge(d1)
->change("get crazy", d => {
d->Js.Dict.set("asdf", Js.Json.string("paulus"));
switch (d->Js.Dict.get("ufoo") |?> Js.Json.decodeArray) {
| Some(ufoo) => ufoo[2] = Js.Json.number(15.0)
| None => ()
};
})
);
let d2b =
AM.(
d1->change("get dizzy", d => {
d->Js.Dict.set("asdf", Js.Json.string("omangus"));
switch (d->Js.Dict.get("ufoo") |?> Js.Json.decodeArray) {
| Some(ufoo) => ufoo[2] = Js.Json.number(9.0)
| None => ()
};
})
);
let merged = AM.merge(d2a, d2b);
let clockForRemote = d2a->AM.getClock;
let changesFromRemote = d2b->AM.getMissingChanges(clockForRemote);
let merged2 = d2a->AM.applyChanges(changesFromRemote);
switch (merged->Js.Dict.get("ufoo") |?> Js.Json.decodeArray) {
| Some(ufoo) =>
let conflicts = ufoo->AM.getArrayConflicts;
Js.log2("ufoo conflicts:", conflicts);
();
| None => ()
};
let conflicts = merged->AM.getObjectConflicts;
Js.log2("asdf conflicts:", conflicts);
let merged2b =
AM.(
merged->change("get dizzy", d => {
switch (d->Js.Dict.get("ufoo") |?> Js.Json.decodeArray) {
| Some(ufoo) => ufoo[2] = Js.Json.number(9.0)
| None => ()
};
d->Js.Dict.set("ufoo", Js.Json.string("peperoni"));
d->Js.Dict.set("asdf", Js.Json.string("omangus"));
})
);
let conflicts = merged2b->AM.getObjectConflicts;
Js.log2("asdf conflicts:", conflicts);
Js.log2("merged", merged);
Js.log2("merged2", merged2);