aboutsummaryrefslogtreecommitdiffstats
path: root/examples/documents.aup
blob: 46714641266aa3e29dad74531908d84d18cb8e62 (plain)
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
# Revision-checked byte edits of keyed documents. A document is an item of a
# keyed list; an edit names the revision it was made against and commits only
# if that is still the document's (0 for one not there yet), and a failed
# event leaves everything as it was.
record Doc {
  id: text
  body: bytes
  revision: i64
}
state docs: list of Doc by id

input show = message {
  id: text
}
input edit = message {
  id: text
  base: i64
  start: u32
  remove: u32
  insert: bytes
}

on show {
  reply docs[show.id].body
}

on edit {
  if docs.has(edit.id) {
    let d = docs[edit.id]
    require d.revision == edit.base
    require edit.start + edit.remove <= d.body.bytes
    docs[edit.id].body = d.body[0 .. edit.start] + edit.insert + d.body[edit.start + edit.remove .. d.body.bytes]
    docs[edit.id].revision = d.revision + 1
  } else {
    require edit.base == 0 && edit.start == 0 && edit.remove == 0
    docs.add(Doc { id: edit.id, body: edit.insert, revision: 1 })
  }
  reply edit.insert
}