@agentshouse/mdruntime 0.1.0-c3903a68dcbbf → 0.1.0-c396ea417807b

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 (362) hide show
  1. package/README.md +30 -15
  2. package/dist/acceptance.d.ts +3 -0
  3. package/dist/acceptance.d.ts.map +1 -0
  4. package/dist/acceptance.js +328 -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 +341 -0
  11. package/dist/adapter.d.ts +360 -102
  12. package/dist/adapter.d.ts.map +1 -1
  13. package/dist/adapter.js +1 -9
  14. package/dist/attached-model.d.ts +7 -0
  15. package/dist/attached-model.d.ts.map +1 -0
  16. package/dist/attached-model.js +41 -0
  17. package/dist/attachment.d.ts +21 -2
  18. package/dist/attachment.d.ts.map +1 -1
  19. package/dist/attachment.js +14 -7
  20. package/dist/authored.d.ts +88 -0
  21. package/dist/authored.d.ts.map +1 -0
  22. package/dist/authored.js +347 -0
  23. package/dist/bootstrap.d.ts +40 -6
  24. package/dist/bootstrap.d.ts.map +1 -1
  25. package/dist/bootstrap.js +324 -52
  26. package/dist/cell.d.ts +6 -0
  27. package/dist/cell.d.ts.map +1 -0
  28. package/dist/cell.js +9 -0
  29. package/dist/chain.d.ts +12 -0
  30. package/dist/chain.d.ts.map +1 -0
  31. package/dist/chain.js +67 -0
  32. package/dist/change-set.d.ts +13 -14
  33. package/dist/change-set.d.ts.map +1 -1
  34. package/dist/change-set.js +17 -60
  35. package/dist/change.d.ts +50 -0
  36. package/dist/change.d.ts.map +1 -1
  37. package/dist/change.js +39 -0
  38. package/dist/chat-types.d.ts +3 -0
  39. package/dist/chat-types.d.ts.map +1 -0
  40. package/dist/chat-types.js +11 -0
  41. package/dist/client.d.ts +66 -34
  42. package/dist/client.d.ts.map +1 -1
  43. package/dist/client.js +162 -49
  44. package/dist/collection.d.ts +36 -16
  45. package/dist/collection.d.ts.map +1 -1
  46. package/dist/collection.js +74 -64
  47. package/dist/column.d.ts +5 -0
  48. package/dist/column.d.ts.map +1 -0
  49. package/dist/column.js +9 -0
  50. package/dist/compile.d.ts +19 -0
  51. package/dist/compile.d.ts.map +1 -0
  52. package/dist/compile.js +755 -0
  53. package/dist/connection.d.ts +8 -121
  54. package/dist/connection.d.ts.map +1 -1
  55. package/dist/connection.js +119 -2207
  56. package/dist/context.d.ts +2 -0
  57. package/dist/context.d.ts.map +1 -0
  58. package/dist/context.js +7 -0
  59. package/dist/conversation-live.d.ts +149 -0
  60. package/dist/conversation-live.d.ts.map +1 -0
  61. package/dist/conversation-live.js +259 -0
  62. package/dist/conversation-row.d.ts +40 -0
  63. package/dist/conversation-row.d.ts.map +1 -0
  64. package/dist/conversation-row.js +66 -0
  65. package/dist/conversation.d.ts +115 -48
  66. package/dist/conversation.d.ts.map +1 -1
  67. package/dist/conversation.js +1171 -436
  68. package/dist/declaration.d.ts +10 -0
  69. package/dist/declaration.d.ts.map +1 -0
  70. package/dist/declaration.js +24 -0
  71. package/dist/digest.d.ts +7 -0
  72. package/dist/digest.d.ts.map +1 -0
  73. package/dist/digest.js +113 -0
  74. package/dist/discovery.d.ts +12 -0
  75. package/dist/discovery.d.ts.map +1 -0
  76. package/dist/discovery.js +175 -0
  77. package/dist/download.d.ts +6 -0
  78. package/dist/download.d.ts.map +1 -0
  79. package/dist/download.js +42 -0
  80. package/dist/durability.d.ts +24 -1
  81. package/dist/durability.d.ts.map +1 -1
  82. package/dist/durability.js +12 -0
  83. package/dist/entry.d.ts +44 -0
  84. package/dist/entry.d.ts.map +1 -0
  85. package/dist/entry.js +238 -0
  86. package/dist/expectation.d.ts +34 -0
  87. package/dist/expectation.d.ts.map +1 -0
  88. package/dist/expectation.js +123 -0
  89. package/dist/failure.d.ts +16 -0
  90. package/dist/failure.d.ts.map +1 -0
  91. package/dist/failure.js +27 -0
  92. package/dist/figures.d.ts +49 -0
  93. package/dist/figures.d.ts.map +1 -0
  94. package/dist/figures.js +199 -0
  95. package/dist/file.d.ts +2 -19
  96. package/dist/file.d.ts.map +1 -1
  97. package/dist/file.js +8 -122
  98. package/dist/holdings.d.ts +29 -0
  99. package/dist/holdings.d.ts.map +1 -0
  100. package/dist/holdings.js +95 -0
  101. package/dist/host.d.ts +39 -4
  102. package/dist/host.d.ts.map +1 -1
  103. package/dist/host.js +250 -83
  104. package/dist/house-query.d.ts +24 -0
  105. package/dist/house-query.d.ts.map +1 -0
  106. package/dist/house-query.js +102 -0
  107. package/dist/identity.d.ts +3 -0
  108. package/dist/identity.d.ts.map +1 -1
  109. package/dist/identity.js +13 -1
  110. package/dist/index.d.ts +50 -11
  111. package/dist/index.d.ts.map +1 -1
  112. package/dist/index.js +25 -1
  113. package/dist/install.d.ts +60 -0
  114. package/dist/install.d.ts.map +1 -0
  115. package/dist/install.js +537 -0
  116. package/dist/lifecycle.d.ts +7 -0
  117. package/dist/lifecycle.d.ts.map +1 -0
  118. package/dist/lifecycle.js +292 -0
  119. package/dist/listing.d.ts +60 -0
  120. package/dist/listing.d.ts.map +1 -0
  121. package/dist/listing.js +168 -0
  122. package/dist/live.d.ts +39 -0
  123. package/dist/live.d.ts.map +1 -0
  124. package/dist/live.js +223 -0
  125. package/dist/loaded.d.ts +7 -0
  126. package/dist/loaded.d.ts.map +1 -0
  127. package/dist/loaded.js +46 -0
  128. package/dist/locks.d.ts +7 -0
  129. package/dist/locks.d.ts.map +1 -0
  130. package/dist/locks.js +34 -0
  131. package/dist/lost.d.ts +5 -0
  132. package/dist/lost.d.ts.map +1 -0
  133. package/dist/lost.js +49 -0
  134. package/dist/media.d.ts +24 -0
  135. package/dist/media.d.ts.map +1 -0
  136. package/dist/media.js +365 -0
  137. package/dist/memory.d.ts +37 -10
  138. package/dist/memory.d.ts.map +1 -1
  139. package/dist/memory.js +405 -93
  140. package/dist/messages.d.ts +63 -0
  141. package/dist/messages.d.ts.map +1 -0
  142. package/dist/messages.js +169 -0
  143. package/dist/modal.d.ts +2 -0
  144. package/dist/modal.d.ts.map +1 -0
  145. package/dist/modal.js +11 -0
  146. package/dist/model.d.ts +23 -20
  147. package/dist/model.d.ts.map +1 -1
  148. package/dist/model.js +14 -13
  149. package/dist/models.d.ts +10 -0
  150. package/dist/models.d.ts.map +1 -0
  151. package/dist/models.js +153 -0
  152. package/dist/name.d.ts +3 -0
  153. package/dist/name.d.ts.map +1 -0
  154. package/dist/name.js +118 -0
  155. package/dist/observation.d.ts +44 -7
  156. package/dist/observation.d.ts.map +1 -1
  157. package/dist/observation.js +279 -31
  158. package/dist/online.d.ts +7 -0
  159. package/dist/online.d.ts.map +1 -0
  160. package/dist/online.js +29 -0
  161. package/dist/open.d.ts +2 -0
  162. package/dist/open.d.ts.map +1 -0
  163. package/dist/open.js +1 -0
  164. package/dist/opened.d.ts +22 -0
  165. package/dist/opened.d.ts.map +1 -0
  166. package/dist/opened.js +107 -0
  167. package/dist/opening.d.ts +4 -0
  168. package/dist/opening.d.ts.map +1 -0
  169. package/dist/opening.js +69 -0
  170. package/dist/outcome.d.ts +94 -20
  171. package/dist/outcome.d.ts.map +1 -1
  172. package/dist/outcome.js +114 -1
  173. package/dist/overlay.d.ts +32 -6
  174. package/dist/overlay.d.ts.map +1 -1
  175. package/dist/overlay.js +240 -189
  176. package/dist/page-metadata.d.ts +8 -0
  177. package/dist/page-metadata.d.ts.map +1 -0
  178. package/dist/page-metadata.js +19 -0
  179. package/dist/parts.d.ts +6 -0
  180. package/dist/parts.d.ts.map +1 -0
  181. package/dist/parts.js +36 -0
  182. package/dist/paths.d.ts +2 -1
  183. package/dist/paths.d.ts.map +1 -1
  184. package/dist/paths.js +26 -16
  185. package/dist/preparation.d.ts +46 -0
  186. package/dist/preparation.d.ts.map +1 -0
  187. package/dist/preparation.js +344 -0
  188. package/dist/presence.d.ts +58 -0
  189. package/dist/presence.d.ts.map +1 -0
  190. package/dist/presence.js +231 -0
  191. package/dist/queries.d.ts +16 -0
  192. package/dist/queries.d.ts.map +1 -0
  193. package/dist/queries.js +330 -0
  194. package/dist/query.d.ts +55 -180
  195. package/dist/query.d.ts.map +1 -1
  196. package/dist/query.js +3 -422
  197. package/dist/questions.d.ts +30 -0
  198. package/dist/questions.d.ts.map +1 -0
  199. package/dist/questions.js +117 -0
  200. package/dist/queue.d.ts +40 -0
  201. package/dist/queue.d.ts.map +1 -0
  202. package/dist/queue.js +480 -0
  203. package/dist/reach.d.ts +65 -0
  204. package/dist/reach.d.ts.map +1 -0
  205. package/dist/reach.js +638 -0
  206. package/dist/react.d.ts +61 -0
  207. package/dist/react.d.ts.map +1 -0
  208. package/dist/react.js +86 -0
  209. package/dist/read.d.ts +7 -0
  210. package/dist/read.d.ts.map +1 -0
  211. package/dist/read.js +17 -0
  212. package/dist/readiness.d.ts +68 -0
  213. package/dist/readiness.d.ts.map +1 -0
  214. package/dist/readiness.js +35 -0
  215. package/dist/reads.d.ts +41 -0
  216. package/dist/reads.d.ts.map +1 -0
  217. package/dist/reads.js +201 -0
  218. package/dist/recovery.d.ts +29 -0
  219. package/dist/recovery.d.ts.map +1 -0
  220. package/dist/recovery.js +255 -0
  221. package/dist/reference-forms.d.ts +4 -0
  222. package/dist/reference-forms.d.ts.map +1 -0
  223. package/dist/reference-forms.js +74 -0
  224. package/dist/refresh.d.ts +21 -7
  225. package/dist/refresh.d.ts.map +1 -1
  226. package/dist/refresh.js +155 -55
  227. package/dist/refusal.d.ts +5 -0
  228. package/dist/refusal.d.ts.map +1 -0
  229. package/dist/refusal.js +55 -0
  230. package/dist/replica.d.ts +48 -33
  231. package/dist/replica.d.ts.map +1 -1
  232. package/dist/replica.js +362 -170
  233. package/dist/retried.d.ts +30 -0
  234. package/dist/retried.d.ts.map +1 -0
  235. package/dist/retried.js +72 -0
  236. package/dist/room-draft.d.ts +88 -14
  237. package/dist/room-draft.d.ts.map +1 -1
  238. package/dist/room-draft.js +506 -98
  239. package/dist/room-drafts.d.ts +24 -0
  240. package/dist/room-drafts.d.ts.map +1 -0
  241. package/dist/room-drafts.js +222 -0
  242. package/dist/room-list.d.ts +10 -0
  243. package/dist/room-list.d.ts.map +1 -0
  244. package/dist/room-list.js +45 -0
  245. package/dist/rows.d.ts +41 -34
  246. package/dist/rows.d.ts.map +1 -1
  247. package/dist/rows.js +80 -89
  248. package/dist/running.d.ts +5 -0
  249. package/dist/running.d.ts.map +1 -0
  250. package/dist/running.js +10 -0
  251. package/dist/runtime.d.ts +37 -0
  252. package/dist/runtime.d.ts.map +1 -0
  253. package/dist/runtime.js +1 -0
  254. package/dist/save.d.ts +48 -23
  255. package/dist/save.d.ts.map +1 -1
  256. package/dist/save.js +1031 -211
  257. package/dist/saves.d.ts +34 -0
  258. package/dist/saves.d.ts.map +1 -0
  259. package/dist/saves.js +463 -0
  260. package/dist/search.d.ts +15 -54
  261. package/dist/search.d.ts.map +1 -1
  262. package/dist/search.js +10 -121
  263. package/dist/seed.d.ts +25 -0
  264. package/dist/seed.d.ts.map +1 -0
  265. package/dist/seed.js +145 -0
  266. package/dist/selector.d.ts +3 -0
  267. package/dist/selector.d.ts.map +1 -0
  268. package/dist/selector.js +22 -0
  269. package/dist/shared.d.ts +10 -0
  270. package/dist/shared.d.ts.map +1 -0
  271. package/dist/shared.js +339 -0
  272. package/dist/shown.d.ts +35 -0
  273. package/dist/shown.d.ts.map +1 -0
  274. package/dist/shown.js +326 -0
  275. package/dist/socket/admission.d.ts +7 -0
  276. package/dist/socket/admission.d.ts.map +1 -0
  277. package/dist/socket/admission.js +39 -0
  278. package/dist/socket/answers.d.ts +6 -0
  279. package/dist/socket/answers.d.ts.map +1 -0
  280. package/dist/socket/answers.js +20 -0
  281. package/dist/socket/lanes.d.ts +28 -0
  282. package/dist/socket/lanes.d.ts.map +1 -0
  283. package/dist/socket/lanes.js +103 -0
  284. package/dist/socket/reads.d.ts +10 -0
  285. package/dist/socket/reads.d.ts.map +1 -0
  286. package/dist/socket/reads.js +31 -0
  287. package/dist/socket/session.d.ts +41 -0
  288. package/dist/socket/session.d.ts.map +1 -0
  289. package/dist/socket/session.js +26 -0
  290. package/dist/socket.d.ts +68 -0
  291. package/dist/socket.d.ts.map +1 -0
  292. package/dist/socket.js +165 -0
  293. package/dist/source.d.ts +61 -0
  294. package/dist/source.d.ts.map +1 -0
  295. package/dist/source.js +95 -0
  296. package/dist/status.d.ts +7 -4
  297. package/dist/status.d.ts.map +1 -1
  298. package/dist/status.js +3 -15
  299. package/dist/store/installs.d.ts +30 -0
  300. package/dist/store/installs.d.ts.map +1 -0
  301. package/dist/store/installs.js +40 -0
  302. package/dist/store/readiness.d.ts +11 -0
  303. package/dist/store/readiness.d.ts.map +1 -0
  304. package/dist/store/readiness.js +68 -0
  305. package/dist/store/records.d.ts +234 -0
  306. package/dist/store/records.d.ts.map +1 -0
  307. package/dist/store/records.js +474 -0
  308. package/dist/store/room-held.d.ts +8 -0
  309. package/dist/store/room-held.d.ts.map +1 -0
  310. package/dist/store/room-held.js +17 -0
  311. package/dist/store/rooms-shown.d.ts +8 -0
  312. package/dist/store/rooms-shown.d.ts.map +1 -0
  313. package/dist/store/rooms-shown.js +13 -0
  314. package/dist/store/space.d.ts +11 -0
  315. package/dist/store/space.d.ts.map +1 -0
  316. package/dist/store/space.js +41 -0
  317. package/dist/store/watchers.d.ts +17 -0
  318. package/dist/store/watchers.d.ts.map +1 -0
  319. package/dist/store/watchers.js +56 -0
  320. package/dist/store/writes.d.ts +24 -0
  321. package/dist/store/writes.d.ts.map +1 -0
  322. package/dist/store/writes.js +125 -0
  323. package/dist/store.d.ts +115 -102
  324. package/dist/store.d.ts.map +1 -1
  325. package/dist/store.js +173 -406
  326. package/dist/stored.d.ts +181 -0
  327. package/dist/stored.d.ts.map +1 -0
  328. package/dist/stored.js +807 -0
  329. package/dist/surface.d.ts +94 -0
  330. package/dist/surface.d.ts.map +1 -0
  331. package/dist/surface.js +255 -0
  332. package/dist/sync.d.ts +21 -0
  333. package/dist/sync.d.ts.map +1 -0
  334. package/dist/sync.js +557 -0
  335. package/dist/tables.d.ts +388 -0
  336. package/dist/tables.d.ts.map +1 -0
  337. package/dist/tables.js +506 -0
  338. package/dist/tallies.d.ts +18 -0
  339. package/dist/tallies.d.ts.map +1 -0
  340. package/dist/tallies.js +128 -0
  341. package/dist/transfer.d.ts +0 -5
  342. package/dist/transfer.d.ts.map +1 -1
  343. package/dist/turn.d.ts +9 -0
  344. package/dist/turn.d.ts.map +1 -0
  345. package/dist/turn.js +75 -0
  346. package/dist/validation.d.ts +31 -0
  347. package/dist/validation.d.ts.map +1 -0
  348. package/dist/validation.js +144 -0
  349. package/dist/windows.d.ts +11 -0
  350. package/dist/windows.d.ts.map +1 -0
  351. package/dist/windows.js +16 -0
  352. package/dist/wire.d.ts +268 -22
  353. package/dist/wire.d.ts.map +1 -1
  354. package/dist/wire.js +114 -69
  355. package/package.json +35 -11
  356. package/AGENTS.md +0 -210
  357. package/dist/log.d.ts +0 -5
  358. package/dist/log.d.ts.map +0 -1
  359. package/dist/log.js +0 -14
  360. package/dist/read-side.d.ts +0 -14
  361. package/dist/read-side.d.ts.map +0 -1
  362. package/dist/read-side.js +0 -371
package/dist/store.js CHANGED
@@ -1,417 +1,184 @@
1
- import { sameChange } from './overlay.js';
2
- const DATABASE = 'mdruntime';
3
- const ROOMS = 'rooms';
4
- const DOCUMENTS = 'documents';
5
- const BINARY_TARGETS = 'binaryTargets';
6
- const ATTACHMENTS = 'attachments';
7
- const PENDING_CHANGES = 'pendingChanges';
8
- const OUTCOMES = 'outcomes';
9
- const DECLARATIONS = 'declarations';
10
- const ROOM_DRAFTS = 'roomDrafts';
11
- const PRESENTATION = 'presentation';
12
- const CONVERSATIONS = 'conversations';
13
- const MESSAGES = 'messages';
14
- const CHAT_DRAFTS = 'chatDrafts';
15
- const CURRENT = 'current';
16
- const SCHEMA = 19;
17
- function settled(request) {
18
- return new Promise((resolve, reject) => {
19
- request.onsuccess = () => resolve(request.result);
20
- request.onerror = () => reject(request.error);
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
+ completedWhenOpened: false,
28
+ preparing: false,
29
+ sweeping: false,
30
+ unpreparedDemand: new Set(),
31
+ unpreparable: new Set(),
32
+ establishing: new Set(),
33
+ status: STARTING,
21
34
  });
22
- }
23
- function completed(transaction) {
24
- return new Promise((resolve, reject) => {
25
- transaction.oncomplete = () => resolve();
26
- transaction.onerror = () => reject(transaction.error);
27
- transaction.onabort = () => reject(transaction.error);
28
- });
29
- }
30
- const EMPTY_DECLARATIONS = {
31
- lastCompleted: null,
32
- generation: null,
33
- providerLimits: [],
34
- capabilities: [],
35
- slots: [],
36
- };
37
- function declaration(database) {
38
- const transaction = database.transaction(DECLARATIONS, 'readonly');
39
- return settled(transaction.objectStore(DECLARATIONS).get(CURRENT)).then((record) => record ?? EMPTY_DECLARATIONS);
40
- }
41
- function updateDeclaration(database, update) {
42
- const transaction = database.transaction(DECLARATIONS, 'readwrite');
43
- const store = transaction.objectStore(DECLARATIONS);
44
- const prior = store.get(CURRENT);
45
- prior.onsuccess = () => {
46
- store.put(update(prior.result ?? EMPTY_DECLARATIONS), CURRENT);
47
- };
48
- return completed(transaction);
49
- }
50
- function amendPending(database, locator, id, amendment) {
51
- const transaction = database.transaction(PENDING_CHANGES, 'readwrite');
52
- const pending = transaction.objectStore(PENDING_CHANGES);
53
- const request = pending.get([locator, id]);
54
- request.onsuccess = () => {
55
- const record = request.result;
56
- if (record !== undefined)
57
- pending.put({ ...record, ...amendment });
58
- };
59
- return completed(transaction);
60
- }
61
- function roomRecord(record, queue) {
62
- const { documents, binaryTargets, revisions, changes, written, attachments, ...room } = record;
63
- return { ...room, order: queue.map((batch) => batch.id) };
64
- }
65
- function removeRoomEntries(store, locator) {
66
- const keys = store.getAllKeys();
67
- keys.onsuccess = () => {
68
- for (const key of keys.result) {
69
- if (Array.isArray(key) && key[0] === locator)
70
- store.delete(key);
71
- }
72
- };
73
- }
74
- export function removeRuntimeStore() {
75
- return new Promise((resolve, reject) => {
76
- const removal = indexedDB.deleteDatabase(DATABASE);
77
- removal.onsuccess = () => resolve();
78
- removal.onerror = () => reject(removal.error);
79
- });
80
- }
81
- export async function openRuntimeStore() {
82
- const opening = indexedDB.open(DATABASE, SCHEMA);
83
- opening.onupgradeneeded = () => {
84
- const database = opening.result;
85
- for (const name of [...database.objectStoreNames])
86
- database.deleteObjectStore(name);
87
- database.createObjectStore(ROOMS, { keyPath: 'locator' });
88
- database.createObjectStore(DOCUMENTS, { keyPath: ['locator', 'path'] });
89
- database.createObjectStore(BINARY_TARGETS, { keyPath: ['locator', 'path'] });
90
- database.createObjectStore(ATTACHMENTS, { keyPath: ['locator', 'reference'] });
91
- database.createObjectStore(PENDING_CHANGES, { keyPath: ['locator', 'id'] });
92
- const outcomes = database.createObjectStore(OUTCOMES, { keyPath: 'id' });
93
- outcomes.createIndex('locator', 'locator');
94
- database.createObjectStore(DECLARATIONS);
95
- database.createObjectStore(ROOM_DRAFTS, { keyPath: ['locator', 'path'] });
96
- database.createObjectStore(PRESENTATION, { keyPath: 'app' });
97
- database.createObjectStore(CONVERSATIONS, { keyPath: 'conversation' });
98
- const messages = database.createObjectStore(MESSAGES, {
99
- keyPath: ['conversation', 'identity'],
35
+ const space = createSpace(options.database, options.spaceFreed);
36
+ const readiness = createReadinessQueue(options.deliverReadiness, options.unpublished);
37
+ let opening;
38
+ let needed = false;
39
+ function open() {
40
+ needed = true;
41
+ opening ??= options.opening(openRuntimeStore(options.database)).then((opened) => {
42
+ options.retries.succeeded('database', OPENING);
43
+ return opened;
44
+ }, (error) => {
45
+ opening = undefined;
46
+ options.retries.failed('database', OPENING, error);
47
+ throw error;
100
48
  });
101
- messages.createIndex('conversation', 'conversation');
102
- database.createObjectStore(CHAT_DRAFTS, { keyPath: 'conversation' });
103
- };
104
- const database = await settled(opening);
49
+ return opening;
50
+ }
51
+ function prepareAgain(locator) {
52
+ const { unpreparable } = device.now();
53
+ device.set({
54
+ unpreparable: locator === undefined
55
+ ? new Set()
56
+ : new Set([...unpreparable].filter((held) => held !== locator)),
57
+ });
58
+ }
59
+ function typed(locator, persisted) {
60
+ if (persisted.status === 'unavailable')
61
+ return persisted;
62
+ const schema = persisted.snapshot.schema;
63
+ const held = typesHeld.get(locator);
64
+ if (held !== undefined && held.schema === schema)
65
+ return { ...persisted, types: held.types };
66
+ const types = schemaTypes(schema, options.roomTypes);
67
+ typesHeld.set(locator, { schema, types });
68
+ return { ...persisted, types };
69
+ }
70
+ function recordOf(stored) {
71
+ return { ...stored, model: typed(stored.locator, stored.model) };
72
+ }
73
+ async function rooms(locators) {
74
+ const held = await (await open()).rooms(locators);
75
+ return new Map(held
76
+ .filter(({ record }) => !lost(record.locator))
77
+ .map(({ record, indexed, documents }) => [
78
+ record.locator,
79
+ { record: recordOf(record), indexed, documents },
80
+ ]));
81
+ }
82
+ async function work(locator) {
83
+ if (lost(locator))
84
+ return { replica: undefined, queue: [], states: new Map() };
85
+ const { room, queue } = await (await open()).room(locator);
86
+ const batches = queue.map(({ batch }) => batch);
87
+ const states = new Map(queue.map(({ batch, state }) => [batch.id, state]));
88
+ return {
89
+ replica: room === null
90
+ ? undefined
91
+ : { record: recordOf(room.record), queue: batches, states, overlaid: room.overlaid },
92
+ queue: batches,
93
+ states,
94
+ };
95
+ }
96
+ async function drafts(locator, paths) {
97
+ const held = await (await open()).roomDrafts(locator, paths);
98
+ return held.filter((record) => !lost(record.locator));
99
+ }
105
100
  return {
106
- async all() {
107
- const transaction = database.transaction([ROOMS, DOCUMENTS, BINARY_TARGETS, ATTACHMENTS], 'readonly');
108
- const [rooms, documents, targets, attachments] = await Promise.all([
109
- settled(transaction.objectStore(ROOMS).getAll()),
110
- settled(transaction.objectStore(DOCUMENTS).getAll()),
111
- settled(transaction.objectStore(BINARY_TARGETS).getAll()),
112
- settled(transaction.objectStore(ATTACHMENTS).getAll()),
113
- ]);
114
- const byRoom = new Map();
115
- for (const { order, ...room } of rooms) {
116
- byRoom.set(room.locator, {
117
- ...room,
118
- documents: {},
119
- revisions: {},
120
- changes: {},
121
- written: {},
122
- binaryTargets: [],
123
- attachments: [],
124
- });
125
- }
126
- const head = (room, held) => {
127
- if (held.revision !== null) {
128
- room.revisions[held.path] = held.revision;
129
- }
130
- if (held.change !== null) {
131
- room.changes[held.path] = held.change;
132
- }
101
+ device,
102
+ roomsShown: createRoomsShown(),
103
+ space,
104
+ readiness,
105
+ ask: options.database.ask,
106
+ changed: options.changed,
107
+ open,
108
+ needed: () => needed,
109
+ durably: (work) => writes.exclusive(async () => {
110
+ const local = await open();
111
+ return work(local);
112
+ }),
113
+ exclusive: writes.exclusive,
114
+ gathered: writes.gathered,
115
+ written: writes.written,
116
+ startingWrite: writes.startingWrite,
117
+ finishingWrite: writes.finishingWrite,
118
+ ahead: writes.ahead,
119
+ rederive: writes.rederive,
120
+ refreshPage: writes.refreshPage,
121
+ stageBytes: writes.stageBytes,
122
+ deriveBytes: writes.deriveBytes,
123
+ rooms,
124
+ record: async (locator) => (await rooms([locator])).get(locator)?.record,
125
+ work,
126
+ replica: async (locator) => (await work(locator)).replica,
127
+ async covering() {
128
+ const held = await (await open()).covering();
129
+ if (!options.losing())
130
+ return held;
131
+ return {
132
+ rooms: new Map([...held.rooms].filter(([locator]) => !lost(locator))),
133
+ locking: new Set([...held.locking].filter((locator) => !lost(locator))),
134
+ queues: new Map([...held.queues].filter(([locator]) => !lost(locator))),
133
135
  };
134
- for (const document of documents) {
135
- const room = byRoom.get(document.locator);
136
- if (room === undefined)
137
- continue;
138
- room.documents[document.path] = document.content;
139
- if (document.written !== null) {
140
- room.written[document.path] = document.written;
141
- }
142
- head(room, document);
143
- }
144
- for (const target of targets) {
145
- const room = byRoom.get(target.locator);
146
- if (room === undefined)
147
- continue;
148
- room.binaryTargets.push(target.path);
149
- head(room, target);
150
- }
151
- for (const { locator, ...attachment } of attachments) {
152
- const room = byRoom.get(locator);
153
- if (room === undefined)
154
- continue;
155
- room.attachments.push(attachment);
156
- }
157
- return [...byRoom.values()];
158
- },
159
- async write({ next, previous, queue, previousQueue, saved, outcomes, roomDrafts, consumedRoomDrafts, }) {
160
- const transaction = database.transaction([ROOMS, DOCUMENTS, BINARY_TARGETS, ATTACHMENTS, PENDING_CHANGES, OUTCOMES, ROOM_DRAFTS], 'readwrite');
161
- transaction.objectStore(ROOMS).put(roomRecord(next, queue));
162
- const documents = transaction.objectStore(DOCUMENTS);
163
- for (const path of Object.keys(previous?.documents ?? {})) {
164
- if (next.documents[path] === undefined)
165
- documents.delete([next.locator, path]);
166
- }
167
- for (const [path, content] of Object.entries(next.documents)) {
168
- if (previous?.documents[path] === content &&
169
- previous.revisions[path] === next.revisions[path] &&
170
- previous.written[path] === next.written[path] &&
171
- sameChange(previous.changes[path], next.changes[path]))
172
- continue;
173
- documents.put({
174
- locator: next.locator,
175
- path,
176
- content,
177
- revision: next.revisions[path] ?? null,
178
- change: next.changes[path] ?? null,
179
- written: next.written[path] ?? null,
180
- });
181
- }
182
- const targets = transaction.objectStore(BINARY_TARGETS);
183
- const beforeTargets = new Set(previous?.binaryTargets ?? []);
184
- const afterTargets = new Set(next.binaryTargets);
185
- for (const path of beforeTargets) {
186
- if (!afterTargets.has(path))
187
- targets.delete([next.locator, path]);
188
- }
189
- for (const path of afterTargets) {
190
- if (!beforeTargets.has(path) ||
191
- previous?.revisions[path] !== next.revisions[path] ||
192
- !sameChange(previous?.changes[path], next.changes[path])) {
193
- targets.put({
194
- locator: next.locator,
195
- path,
196
- revision: next.revisions[path] ?? null,
197
- change: next.changes[path] ?? null,
198
- });
199
- }
200
- }
201
- const attachments = transaction.objectStore(ATTACHMENTS);
202
- const held = new Map((previous?.attachments ?? []).map((attachment) => [attachment.reference, attachment]));
203
- const declared = new Set(next.attachments.map((attachment) => attachment.reference));
204
- for (const reference of held.keys()) {
205
- if (!declared.has(reference))
206
- attachments.delete([next.locator, reference]);
207
- }
208
- for (const attachment of next.attachments) {
209
- if (held.get(attachment.reference) === attachment)
210
- continue;
211
- attachments.put({ ...attachment, locator: next.locator });
212
- }
213
- if (previousQueue !== undefined) {
214
- const pending = transaction.objectStore(PENDING_CHANGES);
215
- const before = new Set(previousQueue.map((batch) => batch.id));
216
- const after = new Set(queue.map((batch) => batch.id));
217
- for (const id of before) {
218
- if (!after.has(id))
219
- pending.delete([next.locator, id]);
220
- }
221
- for (const batch of queue) {
222
- if (!before.has(batch.id)) {
223
- pending.put({
224
- locator: next.locator,
225
- id: batch.id,
226
- batch,
227
- drafts: saved ?? [],
228
- submission: { state: 'waiting' },
229
- lost: false,
230
- });
231
- }
232
- }
233
- }
234
- const outcomeStore = transaction.objectStore(OUTCOMES);
235
- for (const entry of outcomes ?? []) {
236
- if (entry.outcome === null)
237
- outcomeStore.delete(entry.id);
238
- else
239
- outcomeStore.put({ locator: next.locator, id: entry.id, outcome: entry.outcome });
240
- }
241
- const draftStore = transaction.objectStore(ROOM_DRAFTS);
242
- for (const path of consumedRoomDrafts ?? [])
243
- draftStore.delete([next.locator, path]);
244
- for (const record of roomDrafts ?? [])
245
- draftStore.put(record);
246
- await completed(transaction);
247
- },
248
- async revokeModels(records) {
249
- const transaction = database.transaction(ROOMS, 'readwrite');
250
- const rooms = transaction.objectStore(ROOMS);
251
- const revoked = { status: 'unavailable', reason: 'revoked' };
252
- for (const { record, queue } of records) {
253
- rooms.put(roomRecord({ ...record, model: revoked, selectionModel: revoked }, queue));
254
- }
255
- await completed(transaction);
256
- },
257
- async lastCompleted() {
258
- return (await declaration(database)).lastCompleted;
259
- },
260
- complete(at) {
261
- return updateDeclaration(database, (record) => ({ ...record, lastCompleted: at }));
262
- },
263
- async providerLimits() {
264
- return (await declaration(database)).providerLimits;
265
136
  },
266
- async capabilities() {
267
- return (await declaration(database)).capabilities;
268
- },
269
- async slots() {
270
- return (await declaration(database)).slots;
271
- },
272
- declare(limits, capabilities, slots) {
273
- return updateDeclaration(database, (record) => ({
274
- ...record,
275
- providerLimits: limits,
276
- capabilities,
277
- slots,
278
- }));
279
- },
280
- async queues() {
281
- const transaction = database.transaction([ROOMS, PENDING_CHANGES], 'readonly');
282
- const [rooms, pending] = await Promise.all([
283
- settled(transaction.objectStore(ROOMS).getAll()),
284
- settled(transaction.objectStore(PENDING_CHANGES).getAll()),
285
- ]);
286
- const byId = new Map(pending.map((record) => [`${record.locator}\0${record.id}`, record]));
287
- return rooms.map((room) => ({
288
- locator: room.locator,
289
- pending: room.order.map((id) => {
290
- const record = byId.get(`${room.locator}\0${id}`);
291
- if (record === undefined)
292
- throw new Error(`Missing Pending Change ${room.locator}/${id}`);
293
- const { batch, drafts, submission, lost, upload } = record;
294
- return { batch, state: { drafts, submission, lost, upload } };
295
- }),
296
- }));
297
- },
298
- async outcomes() {
299
- const transaction = database.transaction(OUTCOMES, 'readonly');
300
- return settled(transaction.objectStore(OUTCOMES).getAll());
301
- },
302
- submit(locator, id, submission, lost) {
303
- return amendPending(database, locator, id, { submission, lost });
304
- },
305
- keepUpload(locator, id, upload) {
306
- return amendPending(database, locator, id, { upload });
307
- },
308
- async roomDrafts() {
309
- const transaction = database.transaction(ROOM_DRAFTS, 'readonly');
310
- return settled(transaction.objectStore(ROOM_DRAFTS).getAll());
311
- },
312
- async presentation() {
313
- const transaction = database.transaction(PRESENTATION, 'readonly');
314
- return settled(transaction.objectStore(PRESENTATION).getAll());
315
- },
316
- async present(record) {
317
- const transaction = database.transaction(PRESENTATION, 'readwrite');
318
- transaction.objectStore(PRESENTATION).put(record);
319
- await completed(transaction);
320
- },
321
- async conversations() {
322
- const transaction = database.transaction([CONVERSATIONS, MESSAGES], 'readonly');
323
- const [reads, messages] = await Promise.all([
324
- settled(transaction.objectStore(CONVERSATIONS).getAll()),
325
- settled(transaction.objectStore(MESSAGES).getAll()),
326
- ]);
327
- const held = new Map();
328
- for (const { conversation, read } of reads) {
329
- held.set(conversation, { conversation, read, messages: [] });
330
- }
331
- for (const { conversation, ...message } of messages) {
332
- const entry = held.get(conversation) ?? { conversation, read: undefined, messages: [] };
333
- entry.messages.push(message);
334
- held.set(conversation, entry);
335
- }
336
- return [...held.values()];
337
- },
338
- async keepMessages({ conversation, messages, read, sealed }) {
339
- const transaction = database.transaction([CONVERSATIONS, MESSAGES, CHAT_DRAFTS], 'readwrite');
340
- const stored = transaction.objectStore(MESSAGES);
341
- for (const message of messages) {
342
- stored.put({ ...message, conversation });
343
- }
344
- if (read !== undefined) {
345
- transaction
346
- .objectStore(CONVERSATIONS)
347
- .put({ conversation, read });
348
- }
349
- if (sealed === true)
350
- transaction.objectStore(CHAT_DRAFTS).delete(conversation);
351
- await completed(transaction);
352
- },
353
- async dropConversations(conversations) {
354
- const transaction = database.transaction([CONVERSATIONS, MESSAGES, CHAT_DRAFTS], 'readwrite');
355
- const messages = transaction.objectStore(MESSAGES);
356
- for (const conversation of conversations) {
357
- transaction.objectStore(CONVERSATIONS).delete(conversation);
358
- transaction.objectStore(CHAT_DRAFTS).delete(conversation);
359
- const keys = messages.index('conversation').getAllKeys(conversation);
360
- keys.onsuccess = () => {
361
- for (const key of keys.result)
362
- messages.delete(key);
363
- };
364
- }
365
- await completed(transaction);
366
- },
367
- async chatDrafts() {
368
- const transaction = database.transaction(CHAT_DRAFTS, 'readonly');
369
- return settled(transaction.objectStore(CHAT_DRAFTS).getAll());
370
- },
371
- async keepChatDraft(record) {
372
- const transaction = database.transaction(CHAT_DRAFTS, 'readwrite');
373
- transaction.objectStore(CHAT_DRAFTS).put(record);
374
- await completed(transaction);
375
- },
376
- async dropChatDraft(conversation) {
377
- const transaction = database.transaction(CHAT_DRAFTS, 'readwrite');
378
- transaction.objectStore(CHAT_DRAFTS).delete(conversation);
379
- await completed(transaction);
380
- },
381
- async keepRoomDraft(record) {
382
- const transaction = database.transaction(ROOM_DRAFTS, 'readwrite');
383
- transaction.objectStore(ROOM_DRAFTS).put(record);
384
- await completed(transaction);
385
- },
386
- async discardRoomDraft(locator, path) {
387
- const transaction = database.transaction(ROOM_DRAFTS, 'readwrite');
388
- transaction.objectStore(ROOM_DRAFTS).delete([locator, path]);
389
- await completed(transaction);
390
- },
391
- async dropRooms(locators) {
392
- const transaction = database.transaction([ROOMS, DOCUMENTS, BINARY_TARGETS, ATTACHMENTS, PENDING_CHANGES, OUTCOMES, ROOM_DRAFTS], 'readwrite');
393
- for (const locator of locators) {
394
- transaction.objectStore(ROOMS).delete(locator);
395
- for (const name of [DOCUMENTS, BINARY_TARGETS, ATTACHMENTS, PENDING_CHANGES, ROOM_DRAFTS]) {
396
- removeRoomEntries(transaction.objectStore(name), locator);
397
- }
398
- const outcomes = transaction.objectStore(OUTCOMES);
399
- const ids = outcomes.index('locator').getAllKeys(locator);
400
- ids.onsuccess = () => {
401
- for (const id of ids.result)
402
- outcomes.delete(id);
403
- };
404
- }
405
- await completed(transaction);
406
- },
407
- async generation() {
408
- return (await declaration(database)).generation;
137
+ async status() {
138
+ const held = await (await open()).status();
139
+ return {
140
+ queued: held.queued.filter((locator) => !lost(locator)),
141
+ lastCompleted: held.lastCompleted,
142
+ unsent: held.unsent.filter(({ room }) => !lost(room)),
143
+ resets: held.resets.filter(({ locator }) => !lost(locator)),
144
+ returned: held.returned.filter(({ locator }) => !lost(locator)),
145
+ };
409
146
  },
410
- rememberGeneration(value) {
411
- return updateDeclaration(database, (record) => ({ ...record, generation: value }));
147
+ queued: async () => (await (await open()).queued()).filter((locator) => !lost(locator)),
148
+ drafts,
149
+ draft: async (locator, path) => (await drafts(locator, [path]))[0],
150
+ composed: ({ model, declaration }) => compositionOf(model, declaration.typedSelection, options.builtIn, options.typedPaths),
151
+ publication: watchers.publication,
152
+ owing: watchers.owing,
153
+ watch: watchers.watch,
154
+ watching: watchers.watching,
155
+ tell: watchers.tell,
156
+ installs,
157
+ seeds: {
158
+ of: (key) => seeds.get(key),
159
+ keep(key, seed) {
160
+ seeds.set(key, seed);
161
+ },
162
+ drop(key) {
163
+ seeds.delete(key);
164
+ },
165
+ },
166
+ conflicted,
167
+ attached,
168
+ opened: openedFiles(options.database, lost),
169
+ prepareAgain,
170
+ part: (file, part) => options.database.ask({ kind: 'file.part', key: file, part }),
171
+ forget(locator) {
172
+ typesHeld.delete(locator);
173
+ conflicted.forget(locator);
174
+ attached.forget(locator);
175
+ installs.change(locator, ({ installing }) => ({ ...NO_INSTALL, installing }));
176
+ prepareAgain(locator);
412
177
  },
413
178
  close() {
414
- database.close();
179
+ space.stop();
180
+ readiness.close();
181
+ watchers.close();
415
182
  },
416
183
  };
417
184
  }