@agentshouse/mdruntime 0.1.0-c5fe3b2cddda7 → 0.1.0-c6197367d5fad

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