@agentshouse/mdruntime 0.1.0-alpha.5 → 0.1.0-alpha.50

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 (129) hide show
  1. package/AGENTS.md +210 -0
  2. package/README.md +44 -0
  3. package/dist/adapter.d.ts +253 -19
  4. package/dist/adapter.d.ts.map +1 -1
  5. package/dist/adapter.js +2 -1
  6. package/dist/adapter.js.map +1 -1
  7. package/dist/attachment.d.ts +3 -0
  8. package/dist/attachment.d.ts.map +1 -0
  9. package/dist/attachment.js +11 -0
  10. package/dist/attachment.js.map +1 -0
  11. package/dist/bootstrap.d.ts +7 -6
  12. package/dist/bootstrap.d.ts.map +1 -1
  13. package/dist/bootstrap.js +30 -10
  14. package/dist/bootstrap.js.map +1 -1
  15. package/dist/change-set.d.ts +33 -0
  16. package/dist/change-set.d.ts.map +1 -0
  17. package/dist/change-set.js +95 -0
  18. package/dist/change-set.js.map +1 -0
  19. package/dist/change.d.ts +10 -0
  20. package/dist/change.d.ts.map +1 -1
  21. package/dist/change.js.map +1 -1
  22. package/dist/client.d.ts +75 -0
  23. package/dist/client.d.ts.map +1 -0
  24. package/dist/client.js +237 -0
  25. package/dist/client.js.map +1 -0
  26. package/dist/collection.d.ts +26 -12
  27. package/dist/collection.d.ts.map +1 -1
  28. package/dist/collection.js +78 -31
  29. package/dist/collection.js.map +1 -1
  30. package/dist/connection.d.ts +85 -28
  31. package/dist/connection.d.ts.map +1 -1
  32. package/dist/connection.js +1756 -510
  33. package/dist/connection.js.map +1 -1
  34. package/dist/conversation.d.ts +79 -0
  35. package/dist/conversation.d.ts.map +1 -0
  36. package/dist/conversation.js +635 -0
  37. package/dist/conversation.js.map +1 -0
  38. package/dist/durability.d.ts +8 -3
  39. package/dist/durability.d.ts.map +1 -1
  40. package/dist/durability.js +8 -62
  41. package/dist/durability.js.map +1 -1
  42. package/dist/file.d.ts +20 -0
  43. package/dist/file.d.ts.map +1 -0
  44. package/dist/file.js +149 -0
  45. package/dist/file.js.map +1 -0
  46. package/dist/host.d.ts +14 -0
  47. package/dist/host.d.ts.map +1 -0
  48. package/dist/host.js +213 -0
  49. package/dist/host.js.map +1 -0
  50. package/dist/identity.d.ts +3 -0
  51. package/dist/identity.d.ts.map +1 -0
  52. package/dist/identity.js +23 -0
  53. package/dist/identity.js.map +1 -0
  54. package/dist/index.d.ts +15 -7
  55. package/dist/index.d.ts.map +1 -1
  56. package/dist/index.js +1 -0
  57. package/dist/index.js.map +1 -1
  58. package/dist/memory.d.ts +19 -2
  59. package/dist/memory.d.ts.map +1 -1
  60. package/dist/memory.js +283 -24
  61. package/dist/memory.js.map +1 -1
  62. package/dist/model.d.ts +55 -0
  63. package/dist/model.d.ts.map +1 -0
  64. package/dist/model.js +36 -0
  65. package/dist/model.js.map +1 -0
  66. package/dist/observation.d.ts +25 -0
  67. package/dist/observation.d.ts.map +1 -0
  68. package/dist/observation.js +92 -0
  69. package/dist/observation.js.map +1 -0
  70. package/dist/outcome.d.ts +22 -18
  71. package/dist/outcome.d.ts.map +1 -1
  72. package/dist/outcome.js +1 -11
  73. package/dist/outcome.js.map +1 -1
  74. package/dist/overlay.d.ts +9 -2
  75. package/dist/overlay.d.ts.map +1 -1
  76. package/dist/overlay.js +145 -6
  77. package/dist/overlay.js.map +1 -1
  78. package/dist/paths.d.ts +0 -1
  79. package/dist/paths.d.ts.map +1 -1
  80. package/dist/paths.js +0 -4
  81. package/dist/paths.js.map +1 -1
  82. package/dist/query.d.ts +213 -0
  83. package/dist/query.d.ts.map +1 -0
  84. package/dist/query.js +470 -0
  85. package/dist/query.js.map +1 -0
  86. package/dist/read-side.d.ts +14 -0
  87. package/dist/read-side.d.ts.map +1 -0
  88. package/dist/read-side.js +457 -0
  89. package/dist/read-side.js.map +1 -0
  90. package/dist/refresh.d.ts +4 -4
  91. package/dist/refresh.d.ts.map +1 -1
  92. package/dist/refresh.js +40 -10
  93. package/dist/refresh.js.map +1 -1
  94. package/dist/replica.d.ts +27 -4
  95. package/dist/replica.d.ts.map +1 -1
  96. package/dist/replica.js +255 -13
  97. package/dist/replica.js.map +1 -1
  98. package/dist/room-draft.d.ts +124 -0
  99. package/dist/room-draft.d.ts.map +1 -0
  100. package/dist/room-draft.js +406 -0
  101. package/dist/room-draft.js.map +1 -0
  102. package/dist/rows.d.ts +41 -0
  103. package/dist/rows.d.ts.map +1 -0
  104. package/dist/rows.js +102 -0
  105. package/dist/rows.js.map +1 -0
  106. package/dist/save.d.ts +11 -2
  107. package/dist/save.d.ts.map +1 -1
  108. package/dist/save.js +116 -128
  109. package/dist/save.js.map +1 -1
  110. package/dist/search.d.ts +61 -0
  111. package/dist/search.d.ts.map +1 -0
  112. package/dist/search.js +155 -0
  113. package/dist/search.js.map +1 -0
  114. package/dist/status.d.ts +9 -2
  115. package/dist/status.d.ts.map +1 -1
  116. package/dist/status.js +5 -4
  117. package/dist/status.js.map +1 -1
  118. package/dist/store.d.ts +90 -18
  119. package/dist/store.d.ts.map +1 -1
  120. package/dist/store.js +492 -67
  121. package/dist/store.js.map +1 -1
  122. package/dist/transfer.d.ts +5 -0
  123. package/dist/transfer.d.ts.map +1 -1
  124. package/dist/transfer.js.map +1 -1
  125. package/dist/wire.d.ts +64 -0
  126. package/dist/wire.d.ts.map +1 -0
  127. package/dist/wire.js +145 -0
  128. package/dist/wire.js.map +1 -0
  129. package/package.json +15 -13
package/dist/store.js CHANGED
@@ -1,14 +1,19 @@
1
- import { requestPersistentStorage } from './durability.js';
1
+ import { sameChange } from './overlay.js';
2
2
  const DATABASE = 'mdruntime';
3
- const REPLICAS = 'replicas';
4
- const SYNCHRONIZATION = 'synchronization';
5
- const DISCOVERY = 'discovery';
6
- const QUEUES = 'queues';
7
- const DRAFTS = 'drafts';
8
- const LAST_COMPLETED = 'lastCompleted';
9
- const GENERATION = 'generation';
10
- const PROVIDER_LIMITS = 'providerLimits';
11
- const SCHEMA = 4;
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;
12
17
  function settled(request) {
13
18
  return new Promise((resolve, reject)=>{
14
19
  request.onsuccess = ()=>resolve(request.result);
@@ -22,108 +27,528 @@ function completed(transaction) {
22
27
  transaction.onabort = ()=>reject(transaction.error);
23
28
  });
24
29
  }
25
- export async function openReplicaStore() {
26
- await requestPersistentStorage();
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([
54
+ locator,
55
+ id
56
+ ]);
57
+ request.onsuccess = ()=>{
58
+ const record = request.result;
59
+ if (record !== undefined) pending.put({
60
+ ...record,
61
+ ...amendment
62
+ });
63
+ };
64
+ return completed(transaction);
65
+ }
66
+ function roomRecord(record, queue) {
67
+ const { documents, binaryTargets, revisions, changes, written, attachments, ...room } = record;
68
+ return {
69
+ ...room,
70
+ order: queue.map((batch)=>batch.id)
71
+ };
72
+ }
73
+ function removeRoomEntries(store, locator) {
74
+ const keys = store.getAllKeys();
75
+ keys.onsuccess = ()=>{
76
+ for (const key of keys.result){
77
+ if (Array.isArray(key) && key[0] === locator) store.delete(key);
78
+ }
79
+ };
80
+ }
81
+ export function removeRuntimeStore() {
82
+ return new Promise((resolve, reject)=>{
83
+ const removal = indexedDB.deleteDatabase(DATABASE);
84
+ removal.onsuccess = ()=>resolve();
85
+ removal.onerror = ()=>reject(removal.error);
86
+ });
87
+ }
88
+ export async function openRuntimeStore() {
27
89
  const opening = indexedDB.open(DATABASE, SCHEMA);
28
90
  opening.onupgradeneeded = ()=>{
29
91
  const database = opening.result;
30
92
  for (const name of [
31
93
  ...database.objectStoreNames
32
94
  ])database.deleteObjectStore(name);
33
- database.createObjectStore(REPLICAS, {
95
+ database.createObjectStore(ROOMS, {
34
96
  keyPath: 'locator'
35
97
  });
36
- database.createObjectStore(SYNCHRONIZATION);
37
- database.createObjectStore(DISCOVERY);
38
- database.createObjectStore(QUEUES, {
39
- keyPath: 'locator'
98
+ database.createObjectStore(DOCUMENTS, {
99
+ keyPath: [
100
+ 'locator',
101
+ 'path'
102
+ ]
40
103
  });
41
- database.createObjectStore(DRAFTS, {
104
+ database.createObjectStore(BINARY_TARGETS, {
42
105
  keyPath: [
43
106
  'locator',
44
107
  'path'
45
108
  ]
46
109
  });
110
+ database.createObjectStore(ATTACHMENTS, {
111
+ keyPath: [
112
+ 'locator',
113
+ 'reference'
114
+ ]
115
+ });
116
+ database.createObjectStore(PENDING_CHANGES, {
117
+ keyPath: [
118
+ 'locator',
119
+ 'id'
120
+ ]
121
+ });
122
+ const outcomes = database.createObjectStore(OUTCOMES, {
123
+ keyPath: 'id'
124
+ });
125
+ outcomes.createIndex('locator', 'locator');
126
+ database.createObjectStore(DECLARATIONS);
127
+ database.createObjectStore(ROOM_DRAFTS, {
128
+ keyPath: [
129
+ 'locator',
130
+ 'path'
131
+ ]
132
+ });
133
+ database.createObjectStore(PRESENTATION, {
134
+ keyPath: 'app'
135
+ });
136
+ database.createObjectStore(CONVERSATIONS, {
137
+ keyPath: 'conversation'
138
+ });
139
+ const messages = database.createObjectStore(MESSAGES, {
140
+ keyPath: [
141
+ 'conversation',
142
+ 'identity'
143
+ ]
144
+ });
145
+ messages.createIndex('conversation', 'conversation');
146
+ database.createObjectStore(CHAT_DRAFTS, {
147
+ keyPath: 'conversation'
148
+ });
47
149
  };
48
150
  const database = await settled(opening);
49
151
  return {
50
152
  async all () {
51
- const transaction = database.transaction(REPLICAS, 'readonly');
52
- return settled(transaction.objectStore(REPLICAS).getAll());
153
+ const transaction = database.transaction([
154
+ ROOMS,
155
+ DOCUMENTS,
156
+ BINARY_TARGETS,
157
+ ATTACHMENTS
158
+ ], 'readonly');
159
+ const [rooms, documents, targets, attachments] = await Promise.all([
160
+ settled(transaction.objectStore(ROOMS).getAll()),
161
+ settled(transaction.objectStore(DOCUMENTS).getAll()),
162
+ settled(transaction.objectStore(BINARY_TARGETS).getAll()),
163
+ settled(transaction.objectStore(ATTACHMENTS).getAll())
164
+ ]);
165
+ const byRoom = new Map();
166
+ for (const { order, ...room } of rooms){
167
+ byRoom.set(room.locator, {
168
+ ...room,
169
+ documents: {},
170
+ revisions: {},
171
+ changes: {},
172
+ written: {},
173
+ binaryTargets: [],
174
+ attachments: []
175
+ });
176
+ }
177
+ const head = (room, held)=>{
178
+ if (held.revision !== null) {
179
+ room.revisions[held.path] = held.revision;
180
+ }
181
+ if (held.change !== null) {
182
+ room.changes[held.path] = held.change;
183
+ }
184
+ };
185
+ for (const document of documents){
186
+ const room = byRoom.get(document.locator);
187
+ if (room === undefined) continue;
188
+ room.documents[document.path] = document.content;
189
+ if (document.written !== null) {
190
+ room.written[document.path] = document.written;
191
+ }
192
+ head(room, document);
193
+ }
194
+ for (const target of targets){
195
+ const room = byRoom.get(target.locator);
196
+ if (room === undefined) continue;
197
+ room.binaryTargets.push(target.path);
198
+ head(room, target);
199
+ }
200
+ for (const { locator, ...attachment } of attachments){
201
+ const room = byRoom.get(locator);
202
+ if (room === undefined) continue;
203
+ room.attachments.push(attachment);
204
+ }
205
+ return [
206
+ ...byRoom.values()
207
+ ];
208
+ },
209
+ async write ({ next, previous, queue, previousQueue, saved, outcomes, roomDrafts, consumedRoomDrafts }) {
210
+ const transaction = database.transaction([
211
+ ROOMS,
212
+ DOCUMENTS,
213
+ BINARY_TARGETS,
214
+ ATTACHMENTS,
215
+ PENDING_CHANGES,
216
+ OUTCOMES,
217
+ ROOM_DRAFTS
218
+ ], 'readwrite');
219
+ transaction.objectStore(ROOMS).put(roomRecord(next, queue));
220
+ const documents = transaction.objectStore(DOCUMENTS);
221
+ for (const path of Object.keys(previous?.documents ?? {})){
222
+ if (next.documents[path] === undefined) documents.delete([
223
+ next.locator,
224
+ path
225
+ ]);
226
+ }
227
+ for (const [path, content] of Object.entries(next.documents)){
228
+ if (previous?.documents[path] === content && previous.revisions[path] === next.revisions[path] && previous.written[path] === next.written[path] && sameChange(previous.changes[path], next.changes[path])) continue;
229
+ documents.put({
230
+ locator: next.locator,
231
+ path,
232
+ content,
233
+ revision: next.revisions[path] ?? null,
234
+ change: next.changes[path] ?? null,
235
+ written: next.written[path] ?? null
236
+ });
237
+ }
238
+ const targets = transaction.objectStore(BINARY_TARGETS);
239
+ const beforeTargets = new Set(previous?.binaryTargets ?? []);
240
+ const afterTargets = new Set(next.binaryTargets);
241
+ for (const path of beforeTargets){
242
+ if (!afterTargets.has(path)) targets.delete([
243
+ next.locator,
244
+ path
245
+ ]);
246
+ }
247
+ for (const path of afterTargets){
248
+ if (!beforeTargets.has(path) || previous?.revisions[path] !== next.revisions[path] || !sameChange(previous?.changes[path], next.changes[path])) {
249
+ targets.put({
250
+ locator: next.locator,
251
+ path,
252
+ revision: next.revisions[path] ?? null,
253
+ change: next.changes[path] ?? null
254
+ });
255
+ }
256
+ }
257
+ const attachments = transaction.objectStore(ATTACHMENTS);
258
+ const held = new Map((previous?.attachments ?? []).map((attachment)=>[
259
+ attachment.reference,
260
+ attachment
261
+ ]));
262
+ const declared = new Set(next.attachments.map((attachment)=>attachment.reference));
263
+ for (const reference of held.keys()){
264
+ if (!declared.has(reference)) attachments.delete([
265
+ next.locator,
266
+ reference
267
+ ]);
268
+ }
269
+ for (const attachment of next.attachments){
270
+ if (held.get(attachment.reference) === attachment) continue;
271
+ attachments.put({
272
+ ...attachment,
273
+ locator: next.locator
274
+ });
275
+ }
276
+ if (previousQueue !== undefined) {
277
+ const pending = transaction.objectStore(PENDING_CHANGES);
278
+ const before = new Set(previousQueue.map((batch)=>batch.id));
279
+ const after = new Set(queue.map((batch)=>batch.id));
280
+ for (const id of before){
281
+ if (!after.has(id)) pending.delete([
282
+ next.locator,
283
+ id
284
+ ]);
285
+ }
286
+ for (const batch of queue){
287
+ if (!before.has(batch.id)) {
288
+ pending.put({
289
+ locator: next.locator,
290
+ id: batch.id,
291
+ batch,
292
+ drafts: saved ?? [],
293
+ submission: {
294
+ state: 'waiting'
295
+ },
296
+ lost: false
297
+ });
298
+ }
299
+ }
300
+ }
301
+ const outcomeStore = transaction.objectStore(OUTCOMES);
302
+ for (const entry of outcomes ?? []){
303
+ if (entry.outcome === null) outcomeStore.delete(entry.id);
304
+ else outcomeStore.put({
305
+ locator: next.locator,
306
+ id: entry.id,
307
+ outcome: entry.outcome
308
+ });
309
+ }
310
+ const draftStore = transaction.objectStore(ROOM_DRAFTS);
311
+ for (const path of consumedRoomDrafts ?? [])draftStore.delete([
312
+ next.locator,
313
+ path
314
+ ]);
315
+ for (const record of roomDrafts ?? [])draftStore.put(record);
316
+ await completed(transaction);
53
317
  },
54
- async publish (record) {
55
- const transaction = database.transaction(REPLICAS, 'readwrite');
56
- transaction.objectStore(REPLICAS).put(record);
318
+ async revokeModels (records) {
319
+ const transaction = database.transaction(ROOMS, 'readwrite');
320
+ const rooms = transaction.objectStore(ROOMS);
321
+ const revoked = {
322
+ status: 'unavailable',
323
+ reason: 'revoked'
324
+ };
325
+ for (const { record, queue } of records){
326
+ rooms.put(roomRecord({
327
+ ...record,
328
+ model: revoked,
329
+ selectionModel: revoked
330
+ }, queue));
331
+ }
57
332
  await completed(transaction);
58
333
  },
59
334
  async lastCompleted () {
60
- const transaction = database.transaction(SYNCHRONIZATION, 'readonly');
61
- const value = await settled(transaction.objectStore(SYNCHRONIZATION).get(LAST_COMPLETED));
62
- return value ?? null;
335
+ return (await declaration(database)).lastCompleted;
63
336
  },
64
- async complete (at) {
65
- const transaction = database.transaction(SYNCHRONIZATION, 'readwrite');
66
- transaction.objectStore(SYNCHRONIZATION).put(at, LAST_COMPLETED);
67
- await completed(transaction);
337
+ complete (at) {
338
+ return updateDeclaration(database, (record)=>({
339
+ ...record,
340
+ lastCompleted: at
341
+ }));
68
342
  },
69
343
  async providerLimits () {
70
- const transaction = database.transaction(DISCOVERY, 'readonly');
71
- const value = await settled(transaction.objectStore(DISCOVERY).get(PROVIDER_LIMITS));
72
- return value ?? [];
344
+ return (await declaration(database)).providerLimits;
73
345
  },
74
- async declare (limits) {
75
- const transaction = database.transaction(DISCOVERY, 'readwrite');
76
- transaction.objectStore(DISCOVERY).put(limits, PROVIDER_LIMITS);
77
- await completed(transaction);
346
+ async capabilities () {
347
+ return (await declaration(database)).capabilities;
348
+ },
349
+ async slots () {
350
+ return (await declaration(database)).slots;
351
+ },
352
+ declare (limits, capabilities, slots) {
353
+ return updateDeclaration(database, (record)=>({
354
+ ...record,
355
+ providerLimits: limits,
356
+ capabilities,
357
+ slots
358
+ }));
78
359
  },
79
360
  async queues () {
80
- const transaction = database.transaction(QUEUES, 'readonly');
81
- return settled(transaction.objectStore(QUEUES).getAll());
361
+ const transaction = database.transaction([
362
+ ROOMS,
363
+ PENDING_CHANGES
364
+ ], 'readonly');
365
+ const [rooms, pending] = await Promise.all([
366
+ settled(transaction.objectStore(ROOMS).getAll()),
367
+ settled(transaction.objectStore(PENDING_CHANGES).getAll())
368
+ ]);
369
+ const byId = new Map(pending.map((record)=>[
370
+ `${record.locator}\0${record.id}`,
371
+ record
372
+ ]));
373
+ return rooms.map((room)=>({
374
+ locator: room.locator,
375
+ pending: room.order.map((id)=>{
376
+ const record = byId.get(`${room.locator}\0${id}`);
377
+ if (record === undefined) throw new Error(`Missing Pending Change ${room.locator}/${id}`);
378
+ const { batch, drafts, submission, lost, upload } = record;
379
+ return {
380
+ batch,
381
+ state: {
382
+ drafts,
383
+ submission,
384
+ lost,
385
+ upload
386
+ }
387
+ };
388
+ })
389
+ }));
390
+ },
391
+ async outcomes () {
392
+ const transaction = database.transaction(OUTCOMES, 'readonly');
393
+ return settled(transaction.objectStore(OUTCOMES).getAll());
394
+ },
395
+ submit (locator, id, submission, lost) {
396
+ return amendPending(database, locator, id, {
397
+ submission,
398
+ lost
399
+ });
400
+ },
401
+ keepUpload (locator, id, upload) {
402
+ return amendPending(database, locator, id, {
403
+ upload
404
+ });
405
+ },
406
+ async roomDrafts () {
407
+ const transaction = database.transaction(ROOM_DRAFTS, 'readonly');
408
+ return settled(transaction.objectStore(ROOM_DRAFTS).getAll());
82
409
  },
83
- async drafts () {
84
- const transaction = database.transaction(DRAFTS, 'readonly');
85
- return settled(transaction.objectStore(DRAFTS).getAll());
410
+ async presentation () {
411
+ const transaction = database.transaction(PRESENTATION, 'readonly');
412
+ return settled(transaction.objectStore(PRESENTATION).getAll());
86
413
  },
87
- async keep (draft) {
88
- const transaction = database.transaction(DRAFTS, 'readwrite');
89
- transaction.objectStore(DRAFTS).put(draft);
414
+ async present (record) {
415
+ const transaction = database.transaction(PRESENTATION, 'readwrite');
416
+ transaction.objectStore(PRESENTATION).put(record);
90
417
  await completed(transaction);
91
418
  },
92
- async discard (locator, path) {
93
- const transaction = database.transaction(DRAFTS, 'readwrite');
94
- transaction.objectStore(DRAFTS).delete([
95
- locator,
96
- path
419
+ async conversations () {
420
+ const transaction = database.transaction([
421
+ CONVERSATIONS,
422
+ MESSAGES
423
+ ], 'readonly');
424
+ const [reads, messages] = await Promise.all([
425
+ settled(transaction.objectStore(CONVERSATIONS).getAll()),
426
+ settled(transaction.objectStore(MESSAGES).getAll())
97
427
  ]);
428
+ const held = new Map();
429
+ for (const { conversation, read } of reads){
430
+ held.set(conversation, {
431
+ conversation,
432
+ read,
433
+ messages: []
434
+ });
435
+ }
436
+ for (const { conversation, ...message } of messages){
437
+ const entry = held.get(conversation) ?? {
438
+ conversation,
439
+ read: undefined,
440
+ messages: []
441
+ };
442
+ entry.messages.push(message);
443
+ held.set(conversation, entry);
444
+ }
445
+ return [
446
+ ...held.values()
447
+ ];
448
+ },
449
+ async keepMessages ({ conversation, messages, read, sealed }) {
450
+ const transaction = database.transaction([
451
+ CONVERSATIONS,
452
+ MESSAGES,
453
+ CHAT_DRAFTS
454
+ ], 'readwrite');
455
+ const stored = transaction.objectStore(MESSAGES);
456
+ for (const message of messages){
457
+ stored.put({
458
+ ...message,
459
+ conversation
460
+ });
461
+ }
462
+ if (read !== undefined) {
463
+ transaction.objectStore(CONVERSATIONS).put({
464
+ conversation,
465
+ read
466
+ });
467
+ }
468
+ if (sealed === true) transaction.objectStore(CHAT_DRAFTS).delete(conversation);
98
469
  await completed(transaction);
99
470
  },
100
- async append (queue, consumed) {
471
+ async dropConversations (conversations) {
101
472
  const transaction = database.transaction([
102
- QUEUES,
103
- DRAFTS
473
+ CONVERSATIONS,
474
+ MESSAGES,
475
+ CHAT_DRAFTS
104
476
  ], 'readwrite');
105
- transaction.objectStore(QUEUES).put(queue);
106
- const drafts = transaction.objectStore(DRAFTS);
107
- for (const path of consumed)drafts.delete([
108
- queue.locator,
477
+ const messages = transaction.objectStore(MESSAGES);
478
+ for (const conversation of conversations){
479
+ transaction.objectStore(CONVERSATIONS).delete(conversation);
480
+ transaction.objectStore(CHAT_DRAFTS).delete(conversation);
481
+ const keys = messages.index('conversation').getAllKeys(conversation);
482
+ keys.onsuccess = ()=>{
483
+ for (const key of keys.result)messages.delete(key);
484
+ };
485
+ }
486
+ await completed(transaction);
487
+ },
488
+ async chatDrafts () {
489
+ const transaction = database.transaction(CHAT_DRAFTS, 'readonly');
490
+ return settled(transaction.objectStore(CHAT_DRAFTS).getAll());
491
+ },
492
+ async keepChatDraft (record) {
493
+ const transaction = database.transaction(CHAT_DRAFTS, 'readwrite');
494
+ transaction.objectStore(CHAT_DRAFTS).put(record);
495
+ await completed(transaction);
496
+ },
497
+ async dropChatDraft (conversation) {
498
+ const transaction = database.transaction(CHAT_DRAFTS, 'readwrite');
499
+ transaction.objectStore(CHAT_DRAFTS).delete(conversation);
500
+ await completed(transaction);
501
+ },
502
+ async keepRoomDraft (record) {
503
+ const transaction = database.transaction(ROOM_DRAFTS, 'readwrite');
504
+ transaction.objectStore(ROOM_DRAFTS).put(record);
505
+ await completed(transaction);
506
+ },
507
+ async discardRoomDraft (locator, path) {
508
+ const transaction = database.transaction(ROOM_DRAFTS, 'readwrite');
509
+ transaction.objectStore(ROOM_DRAFTS).delete([
510
+ locator,
109
511
  path
110
512
  ]);
111
513
  await completed(transaction);
112
514
  },
113
- async writeQueue (queue) {
114
- const transaction = database.transaction(QUEUES, 'readwrite');
115
- transaction.objectStore(QUEUES).put(queue);
515
+ async dropRooms (locators) {
516
+ const transaction = database.transaction([
517
+ ROOMS,
518
+ DOCUMENTS,
519
+ BINARY_TARGETS,
520
+ ATTACHMENTS,
521
+ PENDING_CHANGES,
522
+ OUTCOMES,
523
+ ROOM_DRAFTS
524
+ ], 'readwrite');
525
+ for (const locator of locators){
526
+ transaction.objectStore(ROOMS).delete(locator);
527
+ for (const name of [
528
+ DOCUMENTS,
529
+ BINARY_TARGETS,
530
+ ATTACHMENTS,
531
+ PENDING_CHANGES,
532
+ ROOM_DRAFTS
533
+ ]){
534
+ removeRoomEntries(transaction.objectStore(name), locator);
535
+ }
536
+ const outcomes = transaction.objectStore(OUTCOMES);
537
+ const ids = outcomes.index('locator').getAllKeys(locator);
538
+ ids.onsuccess = ()=>{
539
+ for (const id of ids.result)outcomes.delete(id);
540
+ };
541
+ }
116
542
  await completed(transaction);
117
543
  },
118
544
  async generation () {
119
- const transaction = database.transaction(SYNCHRONIZATION, 'readonly');
120
- const value = await settled(transaction.objectStore(SYNCHRONIZATION).get(GENERATION));
121
- return value ?? null;
545
+ return (await declaration(database)).generation;
122
546
  },
123
- async rememberGeneration (value) {
124
- const transaction = database.transaction(SYNCHRONIZATION, 'readwrite');
125
- transaction.objectStore(SYNCHRONIZATION).put(value, GENERATION);
126
- await completed(transaction);
547
+ rememberGeneration (value) {
548
+ return updateDeclaration(database, (record)=>({
549
+ ...record,
550
+ generation: value
551
+ }));
127
552
  },
128
553
  close () {
129
554
  database.close();