gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit72bd4f8372bd4f83notes mobile: list header on the gutter, delete confirmation as a question + two equal buttons, Delete note under the editor edgemre72bd4f83/lib/notes.hl

8.2 KB

  1. // lib/notes.hl — THE NOTES (ticket notes#1) and their SYNC (tickets notes#2, notes#4). One hl:mpackdb table,
  2. // storage/mpackdb/notes.db:
  3. // pk @id index owner { owner, text (Markdown), created, edited, gone, local, by, since, copyOf } times = ms
  4. // gone = ms a note was deleted (0 = alive; the row stays as a TOMBSTONE, /api/notes reports it)
  5. // local = the id the creating tab gave the note before it had a server id (a retried save finds it again)
  6. // by = the login tag (lib/users.hl tagOf) of the browser whose save made this version ('' = /api/notes or older)
  7. // since = the `edited` that browser had seen when it started this run of saves (its `base`; `from` is a reserved word)
  8. // copyOf = on a conflict copy: the note it was split from ('' otherwise)
  9. // The SUBJECT of a note is not stored: it is the first non-empty line of the text, without leading `#`
  10. // (CONCEPT.md "Decisions"); the page (components/notes.hl) derives it and orders the sidebar by `edited`.
  11. // Every function takes the owner's user id and only ever reaches that user's notes. Every write to the table is here.
  12. // A note's VERSION is its `edited` (ms); a browser sends the `edited` it last saw of a note (`base`).
  13. // CONTINUATION (notes#4): a browser offline queues one save after another, all with the same old `base`. The second
  14. // one would look like a conflict, so a save whose `by` + `base` are the note's `by` + `since` continues that run.
  15. // A browser that LOST a conflict keeps writing into its copy (same `copyOf`, `by`, `since`), not into a new copy each time.
  16. // Looked up in lib/notes-helpers.hl: textError (size limit), isAlive (tombstones), stampOf (version stamps), copyMark.
  17. import { MPackDB } from 'hl:mpackdb'
  18. import { now } from 'hl:time'
  19. import { storageDir, countOfList } from './util.hl'
  20. import { textError, isAlive, stampOf, copyMark } from './notes-helpers.hl'
  21. static notesTable = new MPackDB(file = storageDir + '/notes.db', primaryKey = '@id', indexes = ['owner'])
  22. // the stored record of note `n` with a new text / version / tombstone / run — every field, written out (hybriel#23)
  23. static recordOf = (n, text, edited, gone, by, since) => {
  24. return { id = n.id owner = n.owner text = text created = n.created edited = edited gone = gone local = n.local == null ? '' : n.local by = by since = since copyOf = n.copyOf == null ? '' : n.copyOf }
  25. }
  26. // one note of this owner, or null
  27. static noteOf = (userId, id) => {
  28. if (id == null || hlTypeName(id) != 'String' || id == '') { return null }
  29. n = notesTable.fetch(id)
  30. if (n == null || n.owner != userId || !isAlive(n)) { return null }
  31. return n
  32. }
  33. // THE PAGE'S LIST (components/notes.hl `notes`, the push notesChanged): every live note of the owner, with its text
  34. static notesFor = (userId) => {
  35. out = []
  36. if (userId == null) { return out }
  37. found = notesTable.find('owner', userId)
  38. let i = 0
  39. n = countOfList(found)
  40. while (i < n) {
  41. r = found[i]
  42. if (isAlive(r)) { out.push({ id = r.id text = r.text edited = r.edited local = r.local == null ? '' : r.local copyOf = r.copyOf == null ? '' : r.copyOf }) }
  43. i = i + 1
  44. }
  45. return out
  46. }
  47. // what /api/notes compares: id, edited, gone (no text) of every note of the owner, tombstones included
  48. static metaRows = (userId) => {
  49. found = notesTable.find('owner', userId)
  50. out = []
  51. let i = 0
  52. n = countOfList(found)
  53. while (i < n) {
  54. out.push({ id = found[i].id edited = found[i].edited gone = isAlive(found[i]) ? 0 : found[i].gone })
  55. i = i + 1
  56. }
  57. return out
  58. }
  59. // one note with its text, or null
  60. static noteView = (userId, id) => {
  61. n = noteOf(userId, id)
  62. return n == null ? null : { id = n.id text = n.text edited = n.edited created = n.created }
  63. }
  64. static newNote = (userId, text, local, edited, by, since, copyOf) => {
  65. t = now()
  66. id = notesTable.put({ owner = userId text = text created = t edited = stampOf(edited, 0) gone = 0 local = local by = by since = since copyOf = copyOf })
  67. return id
  68. }
  69. // the live note this browser made under its own id `local` (a save of a never-synced note, or a retry), or null
  70. static noteOfLocal = (userId, local) => {
  71. if (local == null || local == '') { return null }
  72. mine = notesTable.find('owner', userId)
  73. let i = 0
  74. let n = null
  75. c = countOfList(mine)
  76. while (i < c && n == null) {
  77. if (isAlive(mine[i]) && mine[i].local == local) { n = mine[i] }
  78. i = i + 1
  79. }
  80. return n
  81. }
  82. // the copy this browser (`by`, run `since`) already made of note `id` when it lost a conflict, or null
  83. static copyOfRun = (userId, id, by, since) => {
  84. if (by == null || by == '') { return null }
  85. mine = notesTable.find('owner', userId)
  86. let i = 0
  87. let n = null
  88. c = countOfList(mine)
  89. while (i < c && n == null) {
  90. x = mine[i]
  91. if (isAlive(x) && x.copyOf == id && x.by == by && x.since == since) { n = x }
  92. i = i + 1
  93. }
  94. return n
  95. }
  96. // does a save (`by`, `base`) continue the run of saves that made the note's current version?
  97. static continues = (n, by, base) => { return n.edited == base || (by != null && by != '' && n.by == by && n.since == base) }
  98. static answerOf = (n, copy) => {
  99. return { id = n.id edited = n.edited text = n.text copy = copy == null ? null : { id = copy.id edited = copy.edited text = copy.text } }
  100. }
  101. // A SAVE from a browser: { id (server id or ''), local (the browser's own id of a never-synced note), text,
  102. // base (the `edited` the browser last saw, 0 = never), at (when the browser made the edit), by (its login tag, '' = none) }
  103. // → { id, edited, text, copy? }: `text` is what the note is NOW; `copy` = { id, edited, text } is the version that
  104. // lost a conflict, kept as a note of its own — nothing is ever lost.
  105. static pushNote = (userId, id, local, text, base, at, by) => {
  106. bad = textError(text)
  107. if (bad != null) { return { error = bad } }
  108. let n = null
  109. if (id != '') { n = noteOf(userId, id) }
  110. if (n == null) { n = noteOfLocal(userId, local) }
  111. if (n == null) {
  112. // a new note (or one deleted elsewhere while this browser edited it: the edit becomes a note again)
  113. nid = newNote(userId, text, local, at > 0 ? at : now(), by, 0, '')
  114. if (nid == null) { return { error = 'could not store the note: ' + notesTable.lastError() } }
  115. return answerOf(notesTable.fetch(nid), null)
  116. }
  117. if (n.text == text) { return answerOf(n, null) }
  118. // this browser lost a conflict on this note before: its run goes on in its copy
  119. had = copyOfRun(userId, n.id, by, base)
  120. if (had != null) {
  121. notesTable.update(had.id, recordOf(had, text + copyMark, stampOf(at, had.edited), 0, by, base))
  122. return answerOf(n, notesTable.fetch(had.id))
  123. }
  124. if (continues(n, by, base)) {
  125. notesTable.update(n.id, recordOf(n, text, stampOf(at, n.edited), 0, by, base))
  126. after = notesTable.fetch(n.id)
  127. if (after == null || after.text != text) { return { error = 'could not store the note: ' + notesTable.lastError() } }
  128. return answerOf(after, null)
  129. }
  130. // CONFLICT: changed here AND there. The later edit wins (last write wins); the other version becomes a copy.
  131. if (at > n.edited) {
  132. cid = newNote(userId, n.text + copyMark, '', n.edited, n.by == null ? '' : n.by, n.since == null ? 0 : n.since, n.id)
  133. notesTable.update(n.id, recordOf(n, text, stampOf(at, n.edited), 0, by, base))
  134. return answerOf(notesTable.fetch(n.id), cid == null ? null : notesTable.fetch(cid))
  135. }
  136. cid = newNote(userId, text + copyMark, '', at, by, base, n.id)
  137. return answerOf(n, cid == null ? null : notesTable.fetch(cid))
  138. }
  139. // A DELETE from a browser. Deleted elsewhere already = fine. Changed elsewhere since the browser's `base` (and not by
  140. // this browser's own run) = NOT deleted (the changed note is kept; the browser gets it back). A note the browser made
  141. // and never had a server id for is found by `local`. Deleted = a tombstone (text '', gone = now).
  142. static removeNote = (userId, id, base, local, by) => {
  143. let raw = id == null || hlTypeName(id) != 'String' || id == '' ? null : notesTable.fetch(id)
  144. if (raw == null && local != null && local != '') { raw = noteOfLocal(userId, local) }
  145. if (raw == null || raw.owner != userId) { return { ok = true } }
  146. if (!isAlive(raw)) { return { ok = true } }
  147. if (!continues(raw, by, base) && !(local != null && local != '' && raw.local == local && raw.by == by)) { return { kept = true id = raw.id edited = raw.edited text = raw.text } }
  148. notesTable.update(raw.id, recordOf(raw, '', raw.edited, now(), raw.by == null ? '' : raw.by, raw.since == null ? 0 : raw.since))
  149. return { ok = true }
  150. }

Branches

Latest commits

  • 72bd4f83notes mobile: list header on the gutter, delete confirmation as a question + two equal buttons, Delete note under the editor edgemre
  • ba1e73f8notes: a sent edit is done once the server's version is not older, so a second writer's text shows in the first tab; gate: two writers both waysmre
  • c69ef9ebsync components.hybriel 134b4e3: MdEditor builds an editor put into the page at once, redraws a surface put in anew (F5, hybriel#211)mre
  • 2eb783f5sync components 9a: shared Icon (icons/icons.hl), md-editor toolbar icons + single focus ring (components f4-icons ba96735)mre
  • 4dd46dadsync-components: copy icons/icons.hl + README (F4); testsmre
  • c92e4a0ftests/e2e-outside.mjs: real-Chrome E2E, a note pushed from another device while open, then typingmre
  • 0516b4ccmd-editor.hl: verbatim copy of components f3-mdvalue (an older text handed back is kept)mre
  • 38cc11ecmd-editor.hl: verbatim copy of components f3-mdvalue (an outside value change is shown by the editor)mre
  • 3e7e79f5reports/H1-hydrate.mdmre
  • d98775e8tests/browser.mjs: wait for hydration before the clicks after a goto('/') (tables new note, phone sidebar)mre
  • 1f3198b6tests/cdp.mjs: HL_CPU_THROTTLE=<rate> optional CPU throttling for gate runsmre
  • 39cf9fdfre-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
  • db07aa84tests: short-id-switch ports from env (NOTES_SWITCH_IDENT_PORT/_PORT), defaults unchangedmre
  • ef9f9feere-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
  • 5485467cdocker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • 86ea4a85deploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • edeab20fnotes mission 015: mission, report, LOG, STATUSmre
  • 542a0383notes mission 015: e2e-chrome E2E_HOST (own cookie jar in the shared Chrome)mre
  • 8b30f510notes mission 015: main-menu from layouts 0bcc629, current = true kept (route marks only /), server-HTML marker checksmre
  • 84b574c4notes mission 015: re-vendor plugins from hybriel f9614007 (incl. web, #117)mre