-
Notifications
You must be signed in to change notification settings - Fork 7
/
example_test.go
99 lines (80 loc) · 1.58 KB
/
example_test.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package automerge_test
import (
"fmt"
"github.com/automerge/automerge-go"
)
func ExampleAs() {
doc := automerge.New()
doc.Path("isValid").Set(true)
doc.Path("foo", "bar").Set("baz")
b, err := automerge.As[bool](doc.Path("isValid").Get())
if err != nil {
panic(err)
}
fmt.Println("isValid:", b == true)
v, err := automerge.As[string](doc.Path("foo", "bar").Get())
if err != nil {
panic(err)
}
fmt.Println("foo-bar:", v)
type S struct {
IsValid bool `automerge:"isValid"`
}
s, err := automerge.As[*S](doc.Root())
if err != nil {
panic(err)
}
fmt.Println("root valid:", s.IsValid == true)
// Output:
// isValid: true
// foo-bar: baz
// root valid: true
}
func ExampleSyncState() {
doc := automerge.New()
syncState := automerge.NewSyncState(doc)
docUpdated := make(chan bool)
recv := make(chan []byte)
send := make(chan []byte)
loop:
// generate an initial message, and then do so again
// after receiving updates from the peer or making local changes
for {
msg, valid := syncState.GenerateMessage()
if valid {
send <- msg.Bytes()
}
select {
case msg, ok := <-recv:
if !ok {
break loop
}
_, err := syncState.ReceiveMessage(msg)
if err != nil {
panic(err)
}
case _, ok := <-docUpdated:
if !ok {
break loop
}
}
}
}
/*
func ExampleDoc_SaveIncremental() {
doc1 := automerge.New()
// make initial changes
changes := make(chan []byte)
go func() {
doc2 := automerge.Load(initialState)
for ch := range changes {
err := doc2.LoadIncremental(ch)
if err != nil {
panic(err)
}
}
}()
for {
}
}
*/