@agentshouse/mdruntime 0.1.0-cae97ad2e9142 → 0.1.0-caf080a9a9c60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (315) hide show
  1. package/README.md +14 -14
  2. package/dist/acceptance.d.ts +3 -0
  3. package/dist/acceptance.d.ts.map +1 -0
  4. package/dist/acceptance.js +318 -0
  5. package/dist/act-lanes.d.ts +29 -0
  6. package/dist/act-lanes.d.ts.map +1 -0
  7. package/dist/act-lanes.js +177 -0
  8. package/dist/actions.d.ts +25 -0
  9. package/dist/actions.d.ts.map +1 -0
  10. package/dist/actions.js +261 -0
  11. package/dist/adapter.d.ts +126 -107
  12. package/dist/adapter.d.ts.map +1 -1
  13. package/dist/attached-model.d.ts +3 -10
  14. package/dist/attached-model.d.ts.map +1 -1
  15. package/dist/attached-model.js +29 -47
  16. package/dist/bootstrap.d.ts +17 -19
  17. package/dist/bootstrap.d.ts.map +1 -1
  18. package/dist/bootstrap.js +266 -135
  19. package/dist/cell.d.ts +6 -0
  20. package/dist/cell.d.ts.map +1 -0
  21. package/dist/cell.js +9 -0
  22. package/dist/chain.d.ts +2 -1
  23. package/dist/chain.d.ts.map +1 -1
  24. package/dist/change-set.d.ts +4 -0
  25. package/dist/change-set.d.ts.map +1 -1
  26. package/dist/change-set.js +6 -0
  27. package/dist/change.d.ts +1 -0
  28. package/dist/change.d.ts.map +1 -1
  29. package/dist/change.js +4 -2
  30. package/dist/chat-types.d.ts +5 -0
  31. package/dist/chat-types.d.ts.map +1 -0
  32. package/dist/chat-types.js +13 -0
  33. package/dist/client.d.ts +28 -18
  34. package/dist/client.d.ts.map +1 -1
  35. package/dist/client.js +84 -20
  36. package/dist/collection.d.ts +28 -7
  37. package/dist/collection.d.ts.map +1 -1
  38. package/dist/collection.js +35 -19
  39. package/dist/compile.d.ts +8 -6
  40. package/dist/compile.d.ts.map +1 -1
  41. package/dist/compile.js +289 -332
  42. package/dist/connection.d.ts +6 -141
  43. package/dist/connection.d.ts.map +1 -1
  44. package/dist/connection.js +111 -5012
  45. package/dist/context.d.ts +2 -0
  46. package/dist/context.d.ts.map +1 -0
  47. package/dist/context.js +7 -0
  48. package/dist/conversation-live.d.ts +138 -0
  49. package/dist/conversation-live.d.ts.map +1 -0
  50. package/dist/conversation-live.js +249 -0
  51. package/dist/conversation-row.d.ts +33 -0
  52. package/dist/conversation-row.d.ts.map +1 -0
  53. package/dist/conversation-row.js +53 -0
  54. package/dist/conversation.d.ts +59 -50
  55. package/dist/conversation.d.ts.map +1 -1
  56. package/dist/conversation.js +938 -832
  57. package/dist/declaration.d.ts +10 -0
  58. package/dist/declaration.d.ts.map +1 -0
  59. package/dist/declaration.js +24 -0
  60. package/dist/discovery.d.ts +10 -0
  61. package/dist/discovery.d.ts.map +1 -0
  62. package/dist/discovery.js +153 -0
  63. package/dist/durability.d.ts +18 -1
  64. package/dist/durability.d.ts.map +1 -1
  65. package/dist/entry.d.ts +3 -2
  66. package/dist/entry.d.ts.map +1 -1
  67. package/dist/entry.js +19 -9
  68. package/dist/expectation.d.ts +26 -13
  69. package/dist/expectation.d.ts.map +1 -1
  70. package/dist/expectation.js +72 -19
  71. package/dist/failure.d.ts +3 -0
  72. package/dist/failure.d.ts.map +1 -1
  73. package/dist/failure.js +6 -0
  74. package/dist/figures.d.ts +49 -0
  75. package/dist/figures.d.ts.map +1 -0
  76. package/dist/figures.js +197 -0
  77. package/dist/file.d.ts +2 -11
  78. package/dist/file.d.ts.map +1 -1
  79. package/dist/file.js +6 -4
  80. package/dist/holdings.d.ts +29 -0
  81. package/dist/holdings.d.ts.map +1 -0
  82. package/dist/holdings.js +95 -0
  83. package/dist/host.d.ts +20 -8
  84. package/dist/host.d.ts.map +1 -1
  85. package/dist/host.js +70 -40
  86. package/dist/identity.d.ts +0 -1
  87. package/dist/identity.d.ts.map +1 -1
  88. package/dist/identity.js +0 -3
  89. package/dist/index.d.ts +33 -12
  90. package/dist/index.d.ts.map +1 -1
  91. package/dist/index.js +14 -3
  92. package/dist/install.d.ts +54 -0
  93. package/dist/install.d.ts.map +1 -0
  94. package/dist/install.js +539 -0
  95. package/dist/lifecycle.d.ts +7 -0
  96. package/dist/lifecycle.d.ts.map +1 -0
  97. package/dist/lifecycle.js +279 -0
  98. package/dist/listing.d.ts +48 -0
  99. package/dist/listing.d.ts.map +1 -0
  100. package/dist/listing.js +139 -0
  101. package/dist/live.d.ts +5 -4
  102. package/dist/live.d.ts.map +1 -1
  103. package/dist/live.js +64 -23
  104. package/dist/locks.d.ts +7 -0
  105. package/dist/locks.d.ts.map +1 -0
  106. package/dist/locks.js +33 -0
  107. package/dist/log.d.ts +1 -0
  108. package/dist/log.d.ts.map +1 -1
  109. package/dist/lost.d.ts +5 -0
  110. package/dist/lost.d.ts.map +1 -0
  111. package/dist/lost.js +50 -0
  112. package/dist/media.d.ts +8 -0
  113. package/dist/media.d.ts.map +1 -0
  114. package/dist/media.js +218 -0
  115. package/dist/memory.d.ts +15 -6
  116. package/dist/memory.d.ts.map +1 -1
  117. package/dist/memory.js +251 -98
  118. package/dist/messages.d.ts +24 -3
  119. package/dist/messages.d.ts.map +1 -1
  120. package/dist/messages.js +92 -32
  121. package/dist/model.d.ts +8 -11
  122. package/dist/model.d.ts.map +1 -1
  123. package/dist/model.js +5 -22
  124. package/dist/models.d.ts +10 -0
  125. package/dist/models.d.ts.map +1 -0
  126. package/dist/models.js +156 -0
  127. package/dist/observation.d.ts +3 -2
  128. package/dist/observation.d.ts.map +1 -1
  129. package/dist/observation.js +15 -15
  130. package/dist/opened.d.ts +1 -1
  131. package/dist/opened.d.ts.map +1 -1
  132. package/dist/opened.js +5 -5
  133. package/dist/opening.d.ts +4 -0
  134. package/dist/opening.d.ts.map +1 -0
  135. package/dist/opening.js +59 -0
  136. package/dist/outcome.d.ts +20 -6
  137. package/dist/outcome.d.ts.map +1 -1
  138. package/dist/outcome.js +34 -2
  139. package/dist/overlay.d.ts +6 -1
  140. package/dist/overlay.d.ts.map +1 -1
  141. package/dist/overlay.js +47 -21
  142. package/dist/page-metadata.d.ts +8 -0
  143. package/dist/page-metadata.d.ts.map +1 -0
  144. package/dist/page-metadata.js +19 -0
  145. package/dist/parts.d.ts +3 -0
  146. package/dist/parts.d.ts.map +1 -0
  147. package/dist/parts.js +10 -0
  148. package/dist/paths.d.ts +2 -0
  149. package/dist/paths.d.ts.map +1 -1
  150. package/dist/paths.js +26 -0
  151. package/dist/preparation.d.ts +20 -0
  152. package/dist/preparation.d.ts.map +1 -0
  153. package/dist/preparation.js +233 -0
  154. package/dist/presence.d.ts +52 -0
  155. package/dist/presence.d.ts.map +1 -0
  156. package/dist/presence.js +220 -0
  157. package/dist/queries.d.ts +16 -0
  158. package/dist/queries.d.ts.map +1 -0
  159. package/dist/queries.js +285 -0
  160. package/dist/query.d.ts +14 -210
  161. package/dist/query.d.ts.map +1 -1
  162. package/dist/query.js +3 -505
  163. package/dist/questions.d.ts +29 -0
  164. package/dist/questions.d.ts.map +1 -0
  165. package/dist/questions.js +115 -0
  166. package/dist/queue.d.ts +40 -0
  167. package/dist/queue.d.ts.map +1 -0
  168. package/dist/queue.js +465 -0
  169. package/dist/reach.d.ts +15 -12
  170. package/dist/reach.d.ts.map +1 -1
  171. package/dist/reach.js +228 -145
  172. package/dist/react.d.ts +15 -2
  173. package/dist/react.d.ts.map +1 -1
  174. package/dist/react.js +33 -1
  175. package/dist/read.d.ts +7 -0
  176. package/dist/read.d.ts.map +1 -0
  177. package/dist/read.js +17 -0
  178. package/dist/readiness.d.ts +3 -18
  179. package/dist/readiness.d.ts.map +1 -1
  180. package/dist/readiness.js +4 -10
  181. package/dist/reads.d.ts +38 -10
  182. package/dist/reads.d.ts.map +1 -1
  183. package/dist/reads.js +163 -85
  184. package/dist/recovery.d.ts +3 -0
  185. package/dist/recovery.d.ts.map +1 -1
  186. package/dist/recovery.js +57 -1
  187. package/dist/refresh.d.ts +7 -5
  188. package/dist/refresh.d.ts.map +1 -1
  189. package/dist/refresh.js +35 -7
  190. package/dist/refusal.d.ts +5 -0
  191. package/dist/refusal.d.ts.map +1 -0
  192. package/dist/refusal.js +57 -0
  193. package/dist/replica.d.ts +13 -19
  194. package/dist/replica.d.ts.map +1 -1
  195. package/dist/replica.js +66 -35
  196. package/dist/retried.d.ts +30 -0
  197. package/dist/retried.d.ts.map +1 -0
  198. package/dist/retried.js +72 -0
  199. package/dist/room-draft.d.ts +24 -10
  200. package/dist/room-draft.d.ts.map +1 -1
  201. package/dist/room-draft.js +142 -106
  202. package/dist/room-drafts.d.ts +24 -0
  203. package/dist/room-drafts.d.ts.map +1 -0
  204. package/dist/room-drafts.js +217 -0
  205. package/dist/rows.d.ts +16 -21
  206. package/dist/rows.d.ts.map +1 -1
  207. package/dist/rows.js +46 -174
  208. package/dist/runtime.d.ts +36 -0
  209. package/dist/runtime.d.ts.map +1 -0
  210. package/dist/runtime.js +1 -0
  211. package/dist/save.d.ts +42 -84
  212. package/dist/save.d.ts.map +1 -1
  213. package/dist/save.js +349 -170
  214. package/dist/saves.d.ts +32 -0
  215. package/dist/saves.d.ts.map +1 -0
  216. package/dist/saves.js +413 -0
  217. package/dist/search.d.ts +12 -63
  218. package/dist/search.d.ts.map +1 -1
  219. package/dist/search.js +9 -52
  220. package/dist/seed.d.ts +11 -2
  221. package/dist/seed.d.ts.map +1 -1
  222. package/dist/seed.js +71 -1
  223. package/dist/shared.d.ts +1 -7
  224. package/dist/shared.d.ts.map +1 -1
  225. package/dist/shared.js +13 -105
  226. package/dist/shown.d.ts +32 -0
  227. package/dist/shown.d.ts.map +1 -0
  228. package/dist/shown.js +290 -0
  229. package/dist/socket/admission.d.ts +7 -0
  230. package/dist/socket/admission.d.ts.map +1 -0
  231. package/dist/socket/admission.js +39 -0
  232. package/dist/socket/answers.d.ts +6 -0
  233. package/dist/socket/answers.d.ts.map +1 -0
  234. package/dist/socket/answers.js +20 -0
  235. package/dist/socket/lanes.d.ts +21 -0
  236. package/dist/socket/lanes.d.ts.map +1 -0
  237. package/dist/socket/lanes.js +80 -0
  238. package/dist/socket/reads.d.ts +10 -0
  239. package/dist/socket/reads.d.ts.map +1 -0
  240. package/dist/socket/reads.js +31 -0
  241. package/dist/socket/session.d.ts +38 -0
  242. package/dist/socket/session.d.ts.map +1 -0
  243. package/dist/socket/session.js +23 -0
  244. package/dist/socket.d.ts +68 -0
  245. package/dist/socket.d.ts.map +1 -0
  246. package/dist/socket.js +171 -0
  247. package/dist/source.d.ts +54 -0
  248. package/dist/source.d.ts.map +1 -0
  249. package/dist/source.js +82 -0
  250. package/dist/status.d.ts +4 -5
  251. package/dist/status.d.ts.map +1 -1
  252. package/dist/status.js +3 -7
  253. package/dist/store/installs.d.ts +30 -0
  254. package/dist/store/installs.d.ts.map +1 -0
  255. package/dist/store/installs.js +34 -0
  256. package/dist/store/readiness.d.ts +10 -0
  257. package/dist/store/readiness.d.ts.map +1 -0
  258. package/dist/store/readiness.js +52 -0
  259. package/dist/store/records.d.ts +212 -0
  260. package/dist/store/records.d.ts.map +1 -0
  261. package/dist/store/records.js +430 -0
  262. package/dist/store/room-held.d.ts +8 -0
  263. package/dist/store/room-held.d.ts.map +1 -0
  264. package/dist/store/room-held.js +17 -0
  265. package/dist/store/space.d.ts +11 -0
  266. package/dist/store/space.d.ts.map +1 -0
  267. package/dist/store/space.js +41 -0
  268. package/dist/store/watchers.d.ts +17 -0
  269. package/dist/store/watchers.d.ts.map +1 -0
  270. package/dist/store/watchers.js +56 -0
  271. package/dist/store/writes.d.ts +24 -0
  272. package/dist/store/writes.d.ts.map +1 -0
  273. package/dist/store/writes.js +125 -0
  274. package/dist/store.d.ts +110 -158
  275. package/dist/store.d.ts.map +1 -1
  276. package/dist/store.js +167 -403
  277. package/dist/stored.d.ts +65 -59
  278. package/dist/stored.d.ts.map +1 -1
  279. package/dist/stored.js +356 -271
  280. package/dist/surface.d.ts +88 -0
  281. package/dist/surface.d.ts.map +1 -0
  282. package/dist/surface.js +223 -0
  283. package/dist/sync.d.ts +19 -0
  284. package/dist/sync.d.ts.map +1 -0
  285. package/dist/sync.js +540 -0
  286. package/dist/tables.d.ts +127 -69
  287. package/dist/tables.d.ts.map +1 -1
  288. package/dist/tables.js +218 -105
  289. package/dist/tallies.d.ts +18 -0
  290. package/dist/tallies.d.ts.map +1 -0
  291. package/dist/tallies.js +129 -0
  292. package/dist/transfer.d.ts +0 -5
  293. package/dist/transfer.d.ts.map +1 -1
  294. package/dist/validation.d.ts +3 -4
  295. package/dist/validation.d.ts.map +1 -1
  296. package/dist/validation.js +42 -100
  297. package/dist/windows.d.ts +1 -12
  298. package/dist/windows.d.ts.map +1 -1
  299. package/dist/windows.js +2 -75
  300. package/dist/wire.d.ts +63 -11
  301. package/dist/wire.d.ts.map +1 -1
  302. package/dist/wire.js +21 -5
  303. package/package.json +3 -2
  304. package/dist/attempt.d.ts +0 -16
  305. package/dist/attempt.d.ts.map +0 -1
  306. package/dist/attempt.js +0 -70
  307. package/dist/fence.d.ts +0 -6
  308. package/dist/fence.d.ts.map +0 -1
  309. package/dist/fence.js +0 -10
  310. package/dist/fulltext.d.ts +0 -27
  311. package/dist/fulltext.d.ts.map +0 -1
  312. package/dist/fulltext.js +0 -368
  313. package/dist/words.d.ts +0 -19
  314. package/dist/words.d.ts.map +0 -1
  315. package/dist/words.js +0 -150
package/dist/store.js CHANGED
@@ -1,413 +1,177 @@
1
- import { binaryLocalId } from './identity.js';
2
- const FILE = '\u0000file';
3
- function batchFiles(batch) {
4
- return batch.changes.flatMap((change) => (change.kind === 'binary' ? [change.localId] : []));
5
- }
6
- function draftFiles(drafts) {
7
- return drafts.flatMap(({ parts }) => (parts.file === undefined ? [] : [parts.file.localId]));
8
- }
9
- function roomDraftRow(record) {
10
- return { path: record.path, record: JSON.stringify(record), files: draftFiles([record]) };
11
- }
12
- function authoredRows({ locator, queue, founded, saved, roomDrafts, consumedRoomDrafts, accepted }, held) {
13
- return {
14
- locator,
15
- ...(queue === undefined
16
- ? {}
17
- : {
18
- order: queue.map(({ id }) => id),
19
- added: queue
20
- .filter(({ id }) => held?.has(id) !== true)
21
- .map((batch) => {
22
- const drafts = saved?.get(batch.id) ?? [];
23
- return {
24
- id: batch.id,
25
- batch: JSON.stringify(batch),
26
- state: JSON.stringify({
27
- drafts,
28
- submission: { state: 'waiting' },
29
- lost: false,
30
- }),
31
- files: [...batchFiles(batch), ...draftFiles(drafts)],
32
- };
33
- }),
34
- }),
35
- ...(founded === undefined
36
- ? {}
37
- : {
38
- founded: founded.map((batch) => ({
39
- id: batch.id,
40
- batch: JSON.stringify(batch),
41
- files: batchFiles(batch),
42
- })),
43
- }),
44
- ...(roomDrafts === undefined ? {} : { roomDrafts: roomDrafts.map(roomDraftRow) }),
45
- ...(consumedRoomDrafts === undefined ? {} : { consumedRoomDrafts }),
46
- ...(accepted === undefined
47
- ? {}
48
- : {
49
- amendments: accepted.map(({ id, roomVersion }) => ({
50
- id,
51
- amendment: JSON.stringify({ accepted: roomVersion }),
52
- })),
53
- }),
54
- };
55
- }
56
- function marked(value) {
57
- return typeof value === 'object' && value !== null && FILE in value;
58
- }
59
- function keyed(keys, value) {
60
- const stored = [];
61
- const files = [];
62
- const record = JSON.stringify(value, (_name, held) => {
63
- if (!(held instanceof Blob))
64
- return held;
65
- let key = keys.get(held);
66
- if (key === undefined) {
67
- key = binaryLocalId();
68
- keys.set(held, key);
69
- }
70
- stored.push({ key, blob: held });
71
- files.push(key);
72
- return { [FILE]: key, type: held.type };
1
+ import { schemaTypes } from './attached-model.js';
2
+ import { createCell } from './cell.js';
3
+ import { openedFiles } from './opened.js';
4
+ import { compositionOf } from './replica.js';
5
+ import { STARTING } from './status.js';
6
+ import { createInstalls, NO_INSTALL } from './store/installs.js';
7
+ import { openRuntimeStore, } from './store/records.js';
8
+ import { createReadinessQueue } from './store/readiness.js';
9
+ import { createRoomHeld } from './store/room-held.js';
10
+ import { createSpace } from './store/space.js';
11
+ import { createWatchers } from './store/watchers.js';
12
+ import { createWrites } from './store/writes.js';
13
+ const OPENING = 'store';
14
+ export function createStore(options) {
15
+ const writes = createWrites();
16
+ const watchers = createWatchers(options.unpublished);
17
+ const { lost } = options;
18
+ const installs = createInstalls();
19
+ const seeds = new Map();
20
+ const landed = createRoomHeld();
21
+ const attached = createRoomHeld();
22
+ const typesHeld = new Map();
23
+ const device = createCell({
24
+ persistence: options.persistence,
25
+ storageLoss: null,
26
+ heldWhenOpened: false,
27
+ preparing: false,
28
+ sweeping: false,
29
+ unpreparedDemand: new Set(),
30
+ unpreparable: new Set(),
31
+ establishing: new Set(),
32
+ status: STARTING,
33
+ roomsShown: null,
73
34
  });
74
- return { record, files, stored };
75
- }
76
- async function filed(value, read) {
77
- if (Array.isArray(value))
78
- return Promise.all(value.map((item) => filed(item, read)));
79
- if (marked(value))
80
- return read(value[FILE], value.type);
81
- if (typeof value !== 'object' || value === null)
82
- return value;
83
- const restored = {};
84
- for (const [name, item] of Object.entries(value))
85
- restored[name] = await filed(item, read);
86
- return restored;
87
- }
88
- function json(value, fallback) {
89
- return value === undefined ? fallback : JSON.parse(value);
90
- }
91
- export function openRuntimeStore(fenced, authored) {
92
- const keys = new WeakMap();
93
- const read = async (key, type) => {
94
- const blob = await authored.ask({ kind: 'authored.file', key, type });
95
- keys.set(blob, key);
96
- return blob;
97
- };
98
- const restored = async (records) => Promise.all(records.map(async (record) => (await filed(JSON.parse(record), read))));
99
- const amend = async (locator, id, amendment) => {
100
- if (fenced(locator))
101
- return;
102
- await authored.ask({
103
- kind: 'authored.amend',
104
- locator,
105
- id,
106
- amendment: JSON.stringify(amendment),
35
+ const space = createSpace(options.database, options.spaceFreed);
36
+ const readiness = createReadinessQueue(options.deliverReadiness, options.unpublished);
37
+ let opening;
38
+ function open() {
39
+ opening ??= options.opening(openRuntimeStore(options.database)).then((opened) => {
40
+ options.retries.succeeded('database', OPENING);
41
+ return opened;
42
+ }, (error) => {
43
+ opening = undefined;
44
+ options.retries.failed('database', OPENING, error);
45
+ throw error;
107
46
  });
108
- };
109
- const fact = (name) => authored.ask({ kind: 'authored.fact', name });
110
- const queued = new Map();
111
- const changed = new Map();
112
- const change = (key) => changed.set(key, (changed.get(key) ?? 0) + 1);
113
- const rows = (writes) => writes.map((write) => authoredRows(write, queued.get(write.locator)));
114
- const noted = (writes) => {
115
- for (const { locator, queue } of writes) {
116
- if (queue !== undefined)
117
- queued.set(locator, new Set(queue.map(({ id }) => id)));
118
- }
119
- };
47
+ return opening;
48
+ }
49
+ function prepareAgain(locator) {
50
+ const { unpreparable } = device.now();
51
+ device.set({
52
+ unpreparable: locator === undefined
53
+ ? new Set()
54
+ : new Set([...unpreparable].filter((held) => held !== locator)),
55
+ });
56
+ }
57
+ function typed(locator, persisted) {
58
+ if (persisted.status === 'unavailable')
59
+ return persisted;
60
+ const schema = persisted.snapshot.schema;
61
+ const held = typesHeld.get(locator);
62
+ if (held !== undefined && held.schema === schema)
63
+ return { ...persisted, types: held.types };
64
+ const types = schemaTypes(schema, options.roomTypes);
65
+ typesHeld.set(locator, { schema, types });
66
+ return { ...persisted, types };
67
+ }
68
+ function recordOf(stored) {
69
+ return { ...stored, model: typed(stored.locator, stored.model) };
70
+ }
71
+ async function rooms(locators) {
72
+ const held = await (await open()).rooms(locators);
73
+ return new Map(held
74
+ .filter(({ record }) => !lost(record.locator))
75
+ .map(({ record, indexed, documents }) => [
76
+ record.locator,
77
+ { record: recordOf(record), indexed, documents },
78
+ ]));
79
+ }
80
+ async function work(locator) {
81
+ if (lost(locator))
82
+ return { replica: undefined, queue: [], states: new Map() };
83
+ const { room, queue } = await (await open()).room(locator);
84
+ const batches = queue.map(({ batch }) => batch);
85
+ const states = new Map(queue.map(({ batch, state }) => [batch.id, state]));
86
+ return {
87
+ replica: room === null
88
+ ? undefined
89
+ : { record: recordOf(room.record), queue: batches, states, overlaid: room.overlaid },
90
+ queue: batches,
91
+ states,
92
+ };
93
+ }
94
+ async function drafts(locator, paths) {
95
+ const held = await (await open()).roomDrafts(locator, paths);
96
+ return held.filter((record) => !lost(record.locator));
97
+ }
120
98
  return {
121
- async opened() {
122
- const held = await authored.ask({ kind: 'replica.open' });
123
- const unindexed = new Set(held.keys.flatMap(({ key, rederiving }) => (rederiving === null ? [] : [key])));
124
- const overlaid = new Map();
125
- for (const { key, path } of held.overlaid) {
126
- const paths = overlaid.get(key) ?? new Set();
127
- paths.add(path);
128
- overlaid.set(key, paths);
129
- }
99
+ device,
100
+ space,
101
+ readiness,
102
+ ask: options.database.ask,
103
+ changed: options.changed,
104
+ open,
105
+ durably: (work) => writes.exclusive(async () => {
106
+ const local = await open();
107
+ return work(local);
108
+ }),
109
+ exclusive: writes.exclusive,
110
+ gathered: writes.gathered,
111
+ written: writes.written,
112
+ startingWrite: writes.startingWrite,
113
+ finishingWrite: writes.finishingWrite,
114
+ ahead: writes.ahead,
115
+ rederive: writes.rederive,
116
+ refreshPage: writes.refreshPage,
117
+ stageBytes: writes.stageBytes,
118
+ deriveBytes: writes.deriveBytes,
119
+ rooms,
120
+ record: async (locator) => (await rooms([locator])).get(locator)?.record,
121
+ work,
122
+ replica: async (locator) => (await work(locator)).replica,
123
+ async covering() {
124
+ const held = await (await open()).covering();
130
125
  return {
131
- rooms: new Map(held.rooms
132
- .filter(({ locator }) => !fenced(locator))
133
- .map(({ locator, key, record }) => [
134
- locator,
135
- { ...JSON.parse(record), key },
136
- ])),
137
- transfers: new Map(held.transfers
138
- .filter(({ locator }) => !fenced(locator))
139
- .map(({ locator, key, record }) => [
140
- locator,
141
- { ...JSON.parse(record), key },
142
- ])),
143
- kept: held.kept
144
- .filter(({ locator }) => !fenced(locator))
145
- .reduce((parts, { locator, token, bytes }) => parts.set(locator, new Map(parts.get(locator)).set(token, bytes)), new Map()),
146
- unindexed,
147
- overlaid,
148
- erasing: held.erasing.length > 0,
149
- orphans: held.orphans,
150
- lastCompleted: json(held.facts.lastCompleted, null),
151
- providerLimits: json(held.facts.providerLimits, []),
152
- capabilities: json(held.facts.capabilities, []),
153
- member: json(held.facts.member, null),
154
- targets: json(held.facts.targets, {}),
155
- presentation: held.presentation.map(({ app, value }) => ({
156
- app,
157
- value: JSON.parse(value),
158
- })),
126
+ rooms: new Map([...held.rooms].filter(([locator]) => !lost(locator))),
127
+ queues: new Map([...held.queues].filter(([locator]) => !lost(locator))),
159
128
  };
160
129
  },
161
- counts(rooms) {
162
- return authored.ask({ kind: 'replica.counts', rooms });
163
- },
164
- query(plan, rooms, budget) {
165
- return authored.ask({ kind: 'replica.query', plan, rooms, budget });
166
- },
167
- changes(keys) {
168
- return keys.reduce((total, key) => total + (changed.get(key) ?? 0), 0);
169
- },
170
- entries(key, view, paths) {
171
- return authored.ask({ kind: 'replica.entries', key, view, paths });
172
- },
173
- typed(key, view, paths) {
174
- return authored.ask({ kind: 'replica.typed', key, view, paths });
175
- },
176
- referrers(key, view, targets, kinds) {
177
- return authored.ask({ kind: 'replica.referrers', key, view, targets, kinds });
178
- },
179
- walk(key, view, after, bytes) {
180
- return authored.ask({ kind: 'replica.walk', key, view, after, bytes });
181
- },
182
- files(key, view, after, limit) {
183
- return authored.ask({ kind: 'replica.files', key, view, after, limit });
184
- },
185
- typedWithin(key, view, roots, within) {
186
- return authored.ask({ kind: 'replica.typedWithin', key, view, roots, within });
187
- },
188
- search(ask) {
189
- return authored.ask({ kind: 'replica.search', ...ask });
190
- },
191
- async stage(record, composition, page, written) {
192
- if (fenced(record.locator))
193
- return null;
194
- const { key, ...kept } = record;
195
- return authored.ask({
196
- kind: 'replica.stage',
197
- locator: record.locator,
198
- transfer: record.transfer,
199
- record: JSON.stringify(kept),
200
- key,
201
- composition,
202
- entries: page.entries,
203
- folders: page.folders,
204
- ...(written === undefined ? {} : { written }),
205
- });
206
- },
207
- async transferPart(locator, token) {
208
- if (fenced(locator))
209
- return null;
210
- return authored.ask({ kind: 'replica.transferPart', locator, token });
211
- },
212
- async keepPart(locator, token, part) {
213
- if (fenced(locator))
214
- return;
215
- await authored.ask({ kind: 'replica.keepPart', locator, token, part });
216
- },
217
- async refresh(key, page) {
218
- await authored.ask({
219
- kind: 'replica.refresh',
220
- key,
221
- entries: page.entries,
222
- removals: page.removals,
223
- folders: page.folders,
224
- folderRemovals: page.folderRemovals,
225
- });
226
- },
227
- async unrefresh(key) {
228
- await authored.ask({ kind: 'replica.unrefresh', key });
229
- },
230
- async commit({ rooms, writes = [], files = [], whole = false, discards = [] }) {
231
- const kept = whole ? writes : writes.filter(({ locator }) => !fenced(locator));
232
- const refused = whole ? kept.find(({ locator }) => fenced(locator)) : undefined;
233
- if (refused !== undefined) {
234
- return { fenced: refused.locator, counts: {}, changed: {}, indexed: {} };
235
- }
236
- for (const room of rooms)
237
- change(room.key);
238
- const committed = await authored.ask({
239
- kind: 'replica.commit',
240
- ...(kept.length === 0 && files.length === 0 && discards.length === 0
241
- ? {}
242
- : { authored: { writes: rows(kept), files, whole, discards } }),
243
- rooms: rooms.filter(({ locator }) => !fenced(locator)),
244
- });
245
- if (committed.fenced === null || !whole)
246
- noted(kept);
247
- for (const locator of discards)
248
- queued.delete(locator);
249
- return committed;
250
- },
251
- async fence(locators) {
252
- await authored.ask({ kind: 'replica.fence', locators });
253
- for (const locator of locators)
254
- queued.delete(locator);
255
- },
256
- async erase(locator, limit) {
257
- return (await authored.ask({ kind: 'replica.erase', locator, limit })).remaining;
258
- },
259
- async abandon(locator, kept = false) {
260
- if (fenced(locator))
261
- return null;
262
- return authored.ask({ kind: 'replica.abandon', locator, kept });
263
- },
264
- async release(key) {
265
- await authored.ask({ kind: 'replica.release', key });
266
- },
267
- async rederive(key, limit) {
268
- change(key);
269
- return (await authored.ask({ kind: 'replica.rederive', key, limit })).done;
270
- },
271
- async fact(name, value) {
272
- await authored.ask({ kind: 'replica.fact', name, value: JSON.stringify(value) });
273
- },
274
- async queues() {
275
- const { queues, pending } = await authored.ask({ kind: 'authored.queues' });
276
- queued.clear();
277
- for (const queue of queues)
278
- queued.set(queue.locator, new Set(queue.ids));
279
- const byId = new Map(pending.map((record) => [`${record.locator}\0${record.id}`, record]));
280
- return queues
281
- .filter((queue) => !fenced(queue.locator))
282
- .map((queue) => ({
283
- locator: queue.locator,
284
- pending: queue.ids.map((id) => {
285
- const record = byId.get(`${queue.locator}\0${id}`);
286
- if (record === undefined)
287
- throw new Error(`Missing Pending Change ${queue.locator}/${id}`);
288
- return {
289
- batch: JSON.parse(record.batch),
290
- state: JSON.parse(record.state),
291
- };
292
- }),
293
- }));
294
- },
295
- submit(locator, id, submission, lost) {
296
- return amend(locator, id, { submission, lost });
297
- },
298
- keepUpload(locator, id, upload) {
299
- return amend(locator, id, { upload });
300
- },
301
- async roomDrafts() {
302
- const held = await authored.ask({ kind: 'authored.roomDrafts' });
303
- return held
304
- .map((record) => JSON.parse(record))
305
- .filter((record) => !fenced(record.locator));
306
- },
307
- async present(record) {
308
- await authored.ask({
309
- kind: 'replica.present',
310
- app: record.app,
311
- value: JSON.stringify(record.value ?? null),
312
- });
313
- },
314
- conversations() {
315
- return authored.ask({ kind: 'conversation.held' });
316
- },
317
- async keepMessages(write) {
318
- await authored.ask({ kind: 'conversation.keep', ...write });
319
- },
320
- messageWindow(conversation, limit, before) {
321
- return authored.ask({ kind: 'conversation.window', conversation, limit, before });
322
- },
323
- messageCount(conversation) {
324
- return authored.ask({ kind: 'conversation.count', conversation });
325
- },
326
- turnArrived(conversation, turn) {
327
- return authored.ask({ kind: 'conversation.turn', conversation, turn });
328
- },
329
- async dropConversations(conversations) {
330
- await authored.ask({ kind: 'conversation.drop', conversations });
331
- },
332
- async chatDrafts() {
333
- const records = await authored.ask({ kind: 'authored.chatDrafts' });
334
- return records.map((record) => JSON.parse(record));
335
- },
336
- async keepChatDrafts(records) {
337
- await authored.ask({
338
- kind: 'authored.chatDraft',
339
- kept: records.map((record) => ({
340
- conversation: record.conversation,
341
- record: JSON.stringify(record),
342
- })),
343
- dropped: [],
344
- });
345
- },
346
- async dropChatDraft(conversation) {
347
- await authored.ask({ kind: 'authored.chatDraft', kept: [], dropped: [conversation] });
348
- },
349
- async calls() {
350
- return restored(await authored.ask({ kind: 'authored.calls' }));
351
- },
352
- async keepCall(record) {
353
- const { stored, ...kept } = keyed(keys, record);
354
- await authored.ask({
355
- kind: 'authored.call',
356
- operation: record.operation,
357
- kept,
358
- files: stored,
359
- });
360
- },
361
- async dropCall(operation) {
362
- await authored.ask({ kind: 'authored.call', operation, kept: null, files: [] });
363
- },
364
- async keepRoomDraft(record, files) {
365
- if (fenced(record.locator))
366
- return;
367
- const { path, ...kept } = roomDraftRow(record);
368
- await authored.ask({
369
- kind: 'authored.roomDraft',
370
- locator: record.locator,
371
- path,
372
- kept,
373
- files,
374
- });
375
- },
376
- async discardRoomDraft(locator, path) {
377
- if (fenced(locator))
378
- return;
379
- await authored.ask({ kind: 'authored.roomDraft', locator, path, kept: null, files: [] });
380
- },
381
- fences() {
382
- return authored.ask({ kind: 'authored.fences' });
383
- },
384
- receivedHeads() {
385
- return authored.ask({ kind: 'replica.received' });
386
- },
387
- receivedParts(key, count) {
388
- return authored.ask({ kind: 'replica.receivedParts', key, count });
389
- },
390
- async keepReceived(key, head, part) {
391
- await authored.ask({
392
- kind: 'replica.receive',
393
- key,
394
- head: JSON.stringify(head),
395
- ...(part === undefined
396
- ? {}
397
- : { part: { index: part.index, value: JSON.stringify(part.value) } }),
398
- });
399
- },
400
- async dropReceived(keys) {
401
- await authored.ask({ kind: 'replica.unreceive', keys });
402
- },
403
- generation() {
404
- return fact('generation');
405
- },
406
- async rememberGeneration(value) {
407
- await authored.ask({ kind: 'authored.remember', name: 'generation', value });
130
+ async status() {
131
+ const held = await (await open()).status();
132
+ return {
133
+ queued: held.queued.filter((locator) => !lost(locator)),
134
+ lastCompleted: held.lastCompleted,
135
+ unsent: held.unsent.filter(({ room }) => !lost(room)),
136
+ resets: held.resets.filter(({ locator }) => !lost(locator)),
137
+ returned: held.returned.filter(({ locator }) => !lost(locator)),
138
+ };
408
139
  },
409
- file(key, type) {
410
- return authored.ask({ kind: 'authored.file', key, type });
140
+ queued: async () => (await (await open()).queued()).filter((locator) => !lost(locator)),
141
+ drafts,
142
+ draft: async (locator, path) => (await drafts(locator, [path]))[0],
143
+ composed: ({ model, declaration }) => compositionOf(model, declaration.typedSelection, options.builtIn, options.typedPaths),
144
+ publication: watchers.publication,
145
+ owing: watchers.owing,
146
+ watch: watchers.watch,
147
+ watching: watchers.watching,
148
+ tell: watchers.tell,
149
+ installs,
150
+ seeds: {
151
+ of: (key) => seeds.get(key),
152
+ keep(key, seed) {
153
+ seeds.set(key, seed);
154
+ },
155
+ drop(key) {
156
+ seeds.delete(key);
157
+ },
158
+ },
159
+ landed,
160
+ attached,
161
+ opened: openedFiles(options.database, lost),
162
+ prepareAgain,
163
+ part: (file, part) => options.database.ask({ kind: 'file.part', key: file, part }),
164
+ forget(locator) {
165
+ typesHeld.delete(locator);
166
+ landed.forget(locator);
167
+ attached.forget(locator);
168
+ installs.change(locator, ({ installing }) => ({ ...NO_INSTALL, installing }));
169
+ prepareAgain(locator);
170
+ },
171
+ close() {
172
+ space.stop();
173
+ readiness.close();
174
+ watchers.close();
411
175
  },
412
176
  };
413
177
  }