@agentshouse/mdruntime 0.1.0-cbb8d8acc9d19 → 0.1.0-cbbae1fff01c6

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 (324) hide show
  1. package/README.md +24 -11
  2. package/dist/acceptance.d.ts +3 -0
  3. package/dist/acceptance.d.ts.map +1 -0
  4. package/dist/acceptance.js +308 -0
  5. package/dist/actions.d.ts +20 -0
  6. package/dist/actions.d.ts.map +1 -0
  7. package/dist/actions.js +251 -0
  8. package/dist/adapter.d.ts +194 -90
  9. package/dist/adapter.d.ts.map +1 -1
  10. package/dist/attached-model.d.ts +7 -10
  11. package/dist/attached-model.d.ts.map +1 -1
  12. package/dist/attached-model.js +40 -41
  13. package/dist/attempt.d.ts +16 -0
  14. package/dist/attempt.d.ts.map +1 -0
  15. package/dist/attempt.js +70 -0
  16. package/dist/binary.d.ts +18 -0
  17. package/dist/binary.d.ts.map +1 -0
  18. package/dist/binary.js +8 -0
  19. package/dist/bootstrap.d.ts +37 -5
  20. package/dist/bootstrap.d.ts.map +1 -1
  21. package/dist/bootstrap.js +324 -52
  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 +12 -0
  26. package/dist/chain.d.ts.map +1 -0
  27. package/dist/chain.js +64 -0
  28. package/dist/change-set.d.ts +13 -14
  29. package/dist/change-set.d.ts.map +1 -1
  30. package/dist/change-set.js +17 -56
  31. package/dist/change.d.ts +46 -0
  32. package/dist/change.d.ts.map +1 -1
  33. package/dist/change.js +36 -0
  34. package/dist/chat-types.d.ts +5 -0
  35. package/dist/chat-types.d.ts.map +1 -0
  36. package/dist/chat-types.js +13 -0
  37. package/dist/client.d.ts +56 -18
  38. package/dist/client.d.ts.map +1 -1
  39. package/dist/client.js +164 -23
  40. package/dist/collection.d.ts +31 -14
  41. package/dist/collection.d.ts.map +1 -1
  42. package/dist/collection.js +61 -55
  43. package/dist/compile.d.ts +16 -0
  44. package/dist/compile.d.ts.map +1 -0
  45. package/dist/compile.js +711 -0
  46. package/dist/connection.d.ts +8 -104
  47. package/dist/connection.d.ts.map +1 -1
  48. package/dist/connection.js +107 -2094
  49. package/dist/context.d.ts +2 -0
  50. package/dist/context.d.ts.map +1 -0
  51. package/dist/context.js +7 -0
  52. package/dist/conversation-live.d.ts +135 -0
  53. package/dist/conversation-live.d.ts.map +1 -0
  54. package/dist/conversation-live.js +247 -0
  55. package/dist/conversation-row.d.ts +33 -0
  56. package/dist/conversation-row.d.ts.map +1 -0
  57. package/dist/conversation-row.js +53 -0
  58. package/dist/conversation.d.ts +67 -44
  59. package/dist/conversation.d.ts.map +1 -1
  60. package/dist/conversation.js +1010 -549
  61. package/dist/declaration.d.ts +10 -0
  62. package/dist/declaration.d.ts.map +1 -0
  63. package/dist/declaration.js +24 -0
  64. package/dist/digest.d.ts +7 -0
  65. package/dist/digest.d.ts.map +1 -0
  66. package/dist/digest.js +113 -0
  67. package/dist/discovery.d.ts +10 -0
  68. package/dist/discovery.d.ts.map +1 -0
  69. package/dist/discovery.js +148 -0
  70. package/dist/durability.d.ts +19 -1
  71. package/dist/durability.d.ts.map +1 -1
  72. package/dist/durability.js +5 -0
  73. package/dist/entry.d.ts +20 -0
  74. package/dist/entry.d.ts.map +1 -0
  75. package/dist/entry.js +161 -0
  76. package/dist/expectation.d.ts +23 -0
  77. package/dist/expectation.d.ts.map +1 -0
  78. package/dist/expectation.js +92 -0
  79. package/dist/failure.d.ts +14 -0
  80. package/dist/failure.d.ts.map +1 -0
  81. package/dist/failure.js +22 -0
  82. package/dist/figures.d.ts +49 -0
  83. package/dist/figures.d.ts.map +1 -0
  84. package/dist/figures.js +197 -0
  85. package/dist/file.d.ts +2 -19
  86. package/dist/file.d.ts.map +1 -1
  87. package/dist/file.js +7 -122
  88. package/dist/holdings.d.ts +29 -0
  89. package/dist/holdings.d.ts.map +1 -0
  90. package/dist/holdings.js +95 -0
  91. package/dist/host.d.ts +28 -4
  92. package/dist/host.d.ts.map +1 -1
  93. package/dist/host.js +149 -27
  94. package/dist/identity.d.ts +3 -0
  95. package/dist/identity.d.ts.map +1 -1
  96. package/dist/identity.js +13 -1
  97. package/dist/index.d.ts +40 -10
  98. package/dist/index.d.ts.map +1 -1
  99. package/dist/index.js +17 -0
  100. package/dist/install.d.ts +54 -0
  101. package/dist/install.d.ts.map +1 -0
  102. package/dist/install.js +538 -0
  103. package/dist/job.d.ts +5 -0
  104. package/dist/job.d.ts.map +1 -0
  105. package/dist/job.js +7 -0
  106. package/dist/lifecycle.d.ts +7 -0
  107. package/dist/lifecycle.d.ts.map +1 -0
  108. package/dist/lifecycle.js +272 -0
  109. package/dist/listing.d.ts +48 -0
  110. package/dist/listing.d.ts.map +1 -0
  111. package/dist/listing.js +139 -0
  112. package/dist/live.d.ts +23 -0
  113. package/dist/live.d.ts.map +1 -0
  114. package/dist/live.js +165 -0
  115. package/dist/locks.d.ts +7 -0
  116. package/dist/locks.d.ts.map +1 -0
  117. package/dist/locks.js +33 -0
  118. package/dist/log.d.ts +1 -0
  119. package/dist/log.d.ts.map +1 -1
  120. package/dist/lost.d.ts +5 -0
  121. package/dist/lost.d.ts.map +1 -0
  122. package/dist/lost.js +50 -0
  123. package/dist/media.d.ts +8 -0
  124. package/dist/media.d.ts.map +1 -0
  125. package/dist/media.js +218 -0
  126. package/dist/memory.d.ts +25 -8
  127. package/dist/memory.d.ts.map +1 -1
  128. package/dist/memory.js +295 -99
  129. package/dist/messages.d.ts +49 -0
  130. package/dist/messages.d.ts.map +1 -0
  131. package/dist/messages.js +148 -0
  132. package/dist/model.d.ts +13 -13
  133. package/dist/model.d.ts.map +1 -1
  134. package/dist/model.js +7 -15
  135. package/dist/models.d.ts +10 -0
  136. package/dist/models.d.ts.map +1 -0
  137. package/dist/models.js +156 -0
  138. package/dist/name.d.ts +4 -0
  139. package/dist/name.d.ts.map +1 -0
  140. package/dist/name.js +122 -0
  141. package/dist/observation.d.ts +22 -4
  142. package/dist/observation.d.ts.map +1 -1
  143. package/dist/observation.js +127 -26
  144. package/dist/open.d.ts +2 -0
  145. package/dist/open.d.ts.map +1 -0
  146. package/dist/open.js +1 -0
  147. package/dist/opened.d.ts +22 -0
  148. package/dist/opened.d.ts.map +1 -0
  149. package/dist/opened.js +107 -0
  150. package/dist/opening.d.ts +4 -0
  151. package/dist/opening.d.ts.map +1 -0
  152. package/dist/opening.js +59 -0
  153. package/dist/outcome.d.ts +75 -5
  154. package/dist/outcome.d.ts.map +1 -1
  155. package/dist/outcome.js +84 -1
  156. package/dist/overlay.d.ts +17 -5
  157. package/dist/overlay.d.ts.map +1 -1
  158. package/dist/overlay.js +168 -190
  159. package/dist/parts.d.ts +11 -0
  160. package/dist/parts.d.ts.map +1 -0
  161. package/dist/parts.js +21 -0
  162. package/dist/paths.d.ts +2 -0
  163. package/dist/paths.d.ts.map +1 -1
  164. package/dist/paths.js +26 -0
  165. package/dist/preparation.d.ts +20 -0
  166. package/dist/preparation.d.ts.map +1 -0
  167. package/dist/preparation.js +233 -0
  168. package/dist/presence.d.ts +52 -0
  169. package/dist/presence.d.ts.map +1 -0
  170. package/dist/presence.js +220 -0
  171. package/dist/queries.d.ts +16 -0
  172. package/dist/queries.d.ts.map +1 -0
  173. package/dist/queries.js +270 -0
  174. package/dist/query.d.ts +51 -181
  175. package/dist/query.d.ts.map +1 -1
  176. package/dist/query.js +3 -461
  177. package/dist/questions.d.ts +34 -0
  178. package/dist/questions.d.ts.map +1 -0
  179. package/dist/questions.js +122 -0
  180. package/dist/queue.d.ts +40 -0
  181. package/dist/queue.d.ts.map +1 -0
  182. package/dist/queue.js +455 -0
  183. package/dist/reach.d.ts +21 -5
  184. package/dist/reach.d.ts.map +1 -1
  185. package/dist/reach.js +305 -80
  186. package/dist/react.d.ts +43 -20
  187. package/dist/react.d.ts.map +1 -1
  188. package/dist/react.js +22 -21
  189. package/dist/read.d.ts +7 -0
  190. package/dist/read.d.ts.map +1 -0
  191. package/dist/read.js +17 -0
  192. package/dist/readiness.d.ts +49 -0
  193. package/dist/readiness.d.ts.map +1 -0
  194. package/dist/readiness.js +27 -0
  195. package/dist/reads.d.ts +37 -0
  196. package/dist/reads.d.ts.map +1 -0
  197. package/dist/reads.js +174 -0
  198. package/dist/recovery.d.ts +26 -0
  199. package/dist/recovery.d.ts.map +1 -0
  200. package/dist/recovery.js +232 -0
  201. package/dist/refresh.d.ts +20 -7
  202. package/dist/refresh.d.ts.map +1 -1
  203. package/dist/refresh.js +155 -59
  204. package/dist/refusal.d.ts +5 -0
  205. package/dist/refusal.d.ts.map +1 -0
  206. package/dist/refusal.js +57 -0
  207. package/dist/replica.d.ts +53 -33
  208. package/dist/replica.d.ts.map +1 -1
  209. package/dist/replica.js +361 -164
  210. package/dist/room-draft.d.ts +58 -12
  211. package/dist/room-draft.d.ts.map +1 -1
  212. package/dist/room-draft.js +270 -90
  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 +217 -0
  216. package/dist/rows.d.ts +32 -35
  217. package/dist/rows.d.ts.map +1 -1
  218. package/dist/rows.js +64 -124
  219. package/dist/runtime.d.ts +34 -0
  220. package/dist/runtime.d.ts.map +1 -0
  221. package/dist/runtime.js +1 -0
  222. package/dist/save.d.ts +46 -22
  223. package/dist/save.d.ts.map +1 -1
  224. package/dist/save.js +969 -171
  225. package/dist/saves.d.ts +32 -0
  226. package/dist/saves.d.ts.map +1 -0
  227. package/dist/saves.js +413 -0
  228. package/dist/search.d.ts +15 -54
  229. package/dist/search.d.ts.map +1 -1
  230. package/dist/search.js +10 -121
  231. package/dist/seed.d.ts +25 -0
  232. package/dist/seed.d.ts.map +1 -0
  233. package/dist/seed.js +145 -0
  234. package/dist/shared.d.ts +10 -0
  235. package/dist/shared.d.ts.map +1 -0
  236. package/dist/shared.js +339 -0
  237. package/dist/shown.d.ts +32 -0
  238. package/dist/shown.d.ts.map +1 -0
  239. package/dist/shown.js +290 -0
  240. package/dist/socket/admission.d.ts +7 -0
  241. package/dist/socket/admission.d.ts.map +1 -0
  242. package/dist/socket/admission.js +39 -0
  243. package/dist/socket/answers.d.ts +6 -0
  244. package/dist/socket/answers.d.ts.map +1 -0
  245. package/dist/socket/answers.js +20 -0
  246. package/dist/socket/lanes.d.ts +20 -0
  247. package/dist/socket/lanes.d.ts.map +1 -0
  248. package/dist/socket/lanes.js +77 -0
  249. package/dist/socket/reads.d.ts +10 -0
  250. package/dist/socket/reads.d.ts.map +1 -0
  251. package/dist/socket/reads.js +31 -0
  252. package/dist/socket/session.d.ts +38 -0
  253. package/dist/socket/session.d.ts.map +1 -0
  254. package/dist/socket/session.js +23 -0
  255. package/dist/socket.d.ts +67 -0
  256. package/dist/socket.d.ts.map +1 -0
  257. package/dist/socket.js +170 -0
  258. package/dist/source.d.ts +61 -0
  259. package/dist/source.d.ts.map +1 -0
  260. package/dist/source.js +82 -0
  261. package/dist/status.d.ts +7 -4
  262. package/dist/status.d.ts.map +1 -1
  263. package/dist/status.js +3 -15
  264. package/dist/store/installs.d.ts +30 -0
  265. package/dist/store/installs.d.ts.map +1 -0
  266. package/dist/store/installs.js +34 -0
  267. package/dist/store/readiness.d.ts +10 -0
  268. package/dist/store/readiness.d.ts.map +1 -0
  269. package/dist/store/readiness.js +52 -0
  270. package/dist/store/records.d.ts +210 -0
  271. package/dist/store/records.d.ts.map +1 -0
  272. package/dist/store/records.js +428 -0
  273. package/dist/store/room-held.d.ts +8 -0
  274. package/dist/store/room-held.d.ts.map +1 -0
  275. package/dist/store/room-held.js +17 -0
  276. package/dist/store/space.d.ts +11 -0
  277. package/dist/store/space.d.ts.map +1 -0
  278. package/dist/store/space.js +41 -0
  279. package/dist/store/watchers.d.ts +17 -0
  280. package/dist/store/watchers.d.ts.map +1 -0
  281. package/dist/store/watchers.js +56 -0
  282. package/dist/store/writes.d.ts +24 -0
  283. package/dist/store/writes.d.ts.map +1 -0
  284. package/dist/store/writes.js +125 -0
  285. package/dist/store.d.ts +109 -94
  286. package/dist/store.d.ts.map +1 -1
  287. package/dist/store.js +161 -384
  288. package/dist/stored.d.ts +156 -0
  289. package/dist/stored.d.ts.map +1 -0
  290. package/dist/stored.js +649 -0
  291. package/dist/surface.d.ts +88 -0
  292. package/dist/surface.d.ts.map +1 -0
  293. package/dist/surface.js +223 -0
  294. package/dist/sync.d.ts +19 -0
  295. package/dist/sync.d.ts.map +1 -0
  296. package/dist/sync.js +521 -0
  297. package/dist/tables.d.ts +412 -0
  298. package/dist/tables.d.ts.map +1 -0
  299. package/dist/tables.js +737 -0
  300. package/dist/tallies.d.ts +18 -0
  301. package/dist/tallies.d.ts.map +1 -0
  302. package/dist/tallies.js +129 -0
  303. package/dist/transfer.d.ts +0 -5
  304. package/dist/transfer.d.ts.map +1 -1
  305. package/dist/turn.d.ts +9 -0
  306. package/dist/turn.d.ts.map +1 -0
  307. package/dist/turn.js +75 -0
  308. package/dist/validation.d.ts +29 -0
  309. package/dist/validation.d.ts.map +1 -0
  310. package/dist/validation.js +104 -0
  311. package/dist/windows.d.ts +11 -0
  312. package/dist/windows.d.ts.map +1 -0
  313. package/dist/windows.js +16 -0
  314. package/dist/wire.d.ts +189 -37
  315. package/dist/wire.d.ts.map +1 -1
  316. package/dist/wire.js +77 -33
  317. package/package.json +27 -15
  318. package/AGENTS.md +0 -244
  319. package/dist/attachment.d.ts +0 -3
  320. package/dist/attachment.d.ts.map +0 -1
  321. package/dist/attachment.js +0 -8
  322. package/dist/read-side.d.ts +0 -18
  323. package/dist/read-side.d.ts.map +0 -1
  324. package/dist/read-side.js +0 -433
package/dist/store.js CHANGED
@@ -1,395 +1,172 @@
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
- export const STORE_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 { createSpace } from './store/space.js';
11
+ import { createWatchers } from './store/watchers.js';
12
+ import { createWrites } from './store/writes.js';
13
+ export function createStore(options) {
14
+ const writes = createWrites();
15
+ const watchers = createWatchers(options.unpublished);
16
+ const { lost } = options;
17
+ const installs = createInstalls();
18
+ const seeds = new Map();
19
+ const landed = createRoomHeld();
20
+ const attached = createRoomHeld();
21
+ const typesHeld = new Map();
22
+ const device = createCell({
23
+ persistence: options.persistence,
24
+ storageLoss: null,
25
+ heldWhenOpened: false,
26
+ preparing: false,
27
+ sweeping: false,
28
+ unpreparedDemand: new Set(),
29
+ unpreparable: new Set(),
30
+ establishing: new Set(),
31
+ status: STARTING,
32
+ roomsShown: null,
20
33
  });
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, STORE_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'],
34
+ const space = createSpace(options.database, options.spaceFreed);
35
+ const readiness = createReadinessQueue(options.deliverReadiness, options.unpublished);
36
+ let opening;
37
+ function open() {
38
+ opening ??= options.opening(openRuntimeStore(options.database)).catch((error) => {
39
+ opening = undefined;
40
+ throw error;
96
41
  });
97
- messages.createIndex('conversation', 'conversation');
98
- database.createObjectStore(CHAT_DRAFTS, { keyPath: 'conversation' });
99
- };
100
- const database = await settled(opening);
42
+ return opening;
43
+ }
44
+ function prepareAgain(locator) {
45
+ const { unpreparable } = device.now();
46
+ device.set({
47
+ unpreparable: locator === undefined
48
+ ? new Set()
49
+ : new Set([...unpreparable].filter((held) => held !== locator)),
50
+ });
51
+ }
52
+ function typed(locator, persisted) {
53
+ if (persisted.status === 'unavailable')
54
+ return persisted;
55
+ const schema = persisted.snapshot.schema;
56
+ const held = typesHeld.get(locator);
57
+ if (held !== undefined && held.schema === schema)
58
+ return { ...persisted, types: held.types };
59
+ const types = schemaTypes(schema, options.roomTypes);
60
+ typesHeld.set(locator, { schema, types });
61
+ return { ...persisted, types };
62
+ }
63
+ function recordOf(stored) {
64
+ return { ...stored, model: typed(stored.locator, stored.model) };
65
+ }
66
+ async function rooms(locators) {
67
+ const held = await (await open()).rooms(locators);
68
+ return new Map(held
69
+ .filter(({ record }) => !lost(record.locator))
70
+ .map(({ record, indexed, documents }) => [
71
+ record.locator,
72
+ { record: recordOf(record), indexed, documents },
73
+ ]));
74
+ }
75
+ async function work(locator) {
76
+ if (lost(locator))
77
+ return { replica: undefined, queue: [], states: new Map() };
78
+ const { room, queue } = await (await open()).room(locator);
79
+ const batches = queue.map(({ batch }) => batch);
80
+ const states = new Map(queue.map(({ batch, state }) => [batch.id, state]));
81
+ return {
82
+ replica: room === null
83
+ ? undefined
84
+ : { record: recordOf(room.record), queue: batches, states, overlaid: room.overlaid },
85
+ queue: batches,
86
+ states,
87
+ };
88
+ }
89
+ async function drafts(locator, paths) {
90
+ const held = await (await open()).roomDrafts(locator, paths);
91
+ return held.filter((record) => !lost(record.locator));
92
+ }
101
93
  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
- }
94
+ device,
95
+ space,
96
+ readiness,
97
+ ask: options.database.ask,
98
+ changed: options.changed,
99
+ open,
100
+ durably: (work) => writes.exclusive(async () => {
101
+ const local = await open();
102
+ return work(local);
103
+ }),
104
+ exclusive: writes.exclusive,
105
+ gathered: writes.gathered,
106
+ written: writes.written,
107
+ startingWrite: writes.startingWrite,
108
+ finishingWrite: writes.finishingWrite,
109
+ ahead: writes.ahead,
110
+ rederive: writes.rederive,
111
+ refreshPage: writes.refreshPage,
112
+ stageBytes: writes.stageBytes,
113
+ deriveBytes: writes.deriveBytes,
114
+ rooms,
115
+ record: async (locator) => (await rooms([locator])).get(locator)?.record,
116
+ work,
117
+ replica: async (locator) => (await work(locator)).replica,
118
+ async covering() {
119
+ const held = await (await open()).covering();
120
+ return {
121
+ rooms: new Map([...held.rooms].filter(([locator]) => !lost(locator))),
122
+ queues: new Map([...held.queues].filter(([locator]) => !lost(locator))),
129
123
  };
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
124
  },
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;
125
+ async status() {
126
+ const held = await (await open()).status();
127
+ return {
128
+ queued: held.queued.filter((locator) => !lost(locator)),
129
+ lastCompleted: held.lastCompleted,
130
+ unsent: held.unsent.filter(({ room }) => !lost(room)),
131
+ resets: held.resets.filter(({ locator }) => !lost(locator)),
132
+ returned: held.returned.filter(({ locator }) => !lost(locator)),
133
+ };
387
134
  },
388
- rememberGeneration(value) {
389
- return updateDeclaration(database, (record) => ({ ...record, generation: value }));
135
+ queued: async () => (await (await open()).queued()).filter((locator) => !lost(locator)),
136
+ drafts,
137
+ draft: async (locator, path) => (await drafts(locator, [path]))[0],
138
+ composed: ({ model, declaration }) => compositionOf(model, declaration.typedSelection, options.builtIn, options.typedPaths),
139
+ publication: watchers.publication,
140
+ owing: watchers.owing,
141
+ watch: watchers.watch,
142
+ watching: watchers.watching,
143
+ tell: watchers.tell,
144
+ installs,
145
+ seeds: {
146
+ of: (key) => seeds.get(key),
147
+ keep(key, seed) {
148
+ seeds.set(key, seed);
149
+ },
150
+ drop(key) {
151
+ seeds.delete(key);
152
+ },
153
+ },
154
+ landed,
155
+ attached,
156
+ opened: openedFiles(options.database, lost),
157
+ prepareAgain,
158
+ part: (file, part) => options.database.ask({ kind: 'file.part', key: file, part }),
159
+ forget(locator) {
160
+ typesHeld.delete(locator);
161
+ landed.forget(locator);
162
+ attached.forget(locator);
163
+ installs.change(locator, ({ installing }) => ({ ...NO_INSTALL, installing }));
164
+ prepareAgain(locator);
390
165
  },
391
166
  close() {
392
- database.close();
167
+ space.stop();
168
+ readiness.close();
169
+ watchers.close();
393
170
  },
394
171
  };
395
172
  }