Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
172 lines (148 loc) · 4.01 KB
/
Copy pathexample_test.go
File metadata and controls
172 lines (148 loc) · 4.01 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package ygo_test
import (
"fmt"
"github.com/Deln0r/ygo"
)
// Example is the basic shape: create a document, edit a shared type
// inside a write transaction, and read the value back.
func Example() {
doc := ygo.NewDoc()
settings := ygo.NewMap(doc, "settings")
txn := doc.WriteTxn()
settings.Set(txn, "theme", "dark")
settings.Set(txn, "fontSize", int64(14))
txn.Commit()
fmt.Println(settings.Get("theme"))
fmt.Println(settings.Get("fontSize"))
// Output:
// dark
// 14
}
// Example_array shows the shared Array: an ordered sequence you append to
// and read positionally.
func Example_array() {
doc := ygo.NewDoc()
todo := ygo.NewArray(doc, "todo")
txn := doc.WriteTxn()
todo.Push(txn, "buy milk", "write code")
txn.Commit()
fmt.Println(todo.Len())
fmt.Println(todo.Get(0))
fmt.Println(todo.Get(1))
// Output:
// 2
// buy milk
// write code
}
// Example_text shows the shared Text: a collaborative string edited by
// index. Inserts commute so concurrent edits converge.
func Example_text() {
doc := ygo.NewDoc()
note := ygo.NewText(doc, "note")
txn := doc.WriteTxn()
_ = note.Insert(txn, 0, "world")
_ = note.Insert(txn, 0, "hello ")
txn.Commit()
fmt.Println(note.String())
// Output:
// hello world
}
// Example_sync shows the core CRDT property: two documents that never
// talked to each other each apply the other's update and converge on the
// same merged state, independent of order.
func Example_sync() {
// Peer A sets a title on its copy.
a := ygo.NewDoc()
am := ygo.NewMap(a, "doc")
wa := a.WriteTxn()
am.Set(wa, "title", "Hello")
wa.Commit()
// Peer B sets a different key on its own copy.
b := ygo.NewDoc()
bm := ygo.NewMap(b, "doc")
wb := b.WriteTxn()
bm.Set(wb, "author", "Ada")
wb.Commit()
// Exchange full-state updates and apply each to the other.
if err := ygo.ApplyUpdate(a, ygo.EncodeStateAsUpdate(b)); err != nil {
panic(err)
}
if err := ygo.ApplyUpdate(b, ygo.EncodeStateAsUpdate(a)); err != nil {
panic(err)
}
// Both converge to the same merged state.
fmt.Println(am.Get("title"), am.Get("author"))
fmt.Println(bm.Get("title"), bm.Get("author"))
// Output:
// Hello Ada
// Hello Ada
}
// Example_updates shows Doc.OnUpdate: every committed transaction arrives
// as one update holding just that transaction's changes, the bytes yjs
// emits on doc.on('update'). A sync provider forwards its own and skips
// what a peer sent, which it recognises by the origin it applied it with.
func Example_updates() {
type peer struct{ name string }
remote := &peer{name: "ws-1"}
d := ygo.NewDoc()
text := ygo.NewText(d, "notes")
var outgoing [][]byte
d.OnUpdate(func(update []byte, origin any) {
if origin == remote {
return // came from that peer, do not send it back
}
outgoing = append(outgoing, update)
})
for _, s := range []string{"hello", " world"} {
txn := d.WriteTxn()
if err := text.Insert(txn, text.Length(), s); err != nil {
panic(err)
}
txn.Commit()
}
// The peer replays what was sent, then appends "!" itself.
p := ygo.NewDoc()
pt := ygo.NewText(p, "notes")
for _, u := range outgoing {
if err := ygo.ApplyUpdate(p, u); err != nil {
panic(err)
}
}
since := ygo.EncodeStateVector(p)
txn := p.WriteTxn()
if err := pt.Insert(txn, pt.Length(), "!"); err != nil {
panic(err)
}
txn.Commit()
fromPeer, err := ygo.EncodeDiff(p, since)
if err != nil {
panic(err)
}
if err := ygo.ApplyUpdateWithOrigin(d, fromPeer, remote); err != nil {
panic(err)
}
fmt.Println(len(outgoing), "updates sent")
fmt.Println(text.String())
// Output:
// 2 updates sent
// hello world!
}
// Example_undo shows the built-in UndoManager: track a shared type, edit
// it, then step backward and forward through the edit history.
func Example_undo() {
doc := ygo.NewDoc()
m := ygo.NewMap(doc, "doc")
undo := ygo.NewUndoManager(doc, m)
txn := doc.WriteTxn()
m.Set(txn, "title", "draft")
txn.Commit()
fmt.Println(m.Get("title"))
undo.Undo()
fmt.Println(m.Has("title"))
undo.Redo()
fmt.Println(m.Get("title"))
// Output:
// draft
// false
// draft
}