@agentshouse/mdruntime 0.1.0-c6e4c19c13638 → 0.1.0-c6ecdbdce0a2c

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