@agentshouse/mdruntime 0.1.0-c5b50992a4193 → 0.1.0-c698c0ebd65bd

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 (169) hide show
  1. package/AGENTS.md +151 -96
  2. package/README.md +13 -3
  3. package/dist/adapter.d.ts +47 -14
  4. package/dist/adapter.d.ts.map +1 -1
  5. package/dist/attempt.d.ts +16 -0
  6. package/dist/attempt.d.ts.map +1 -0
  7. package/dist/attempt.js +70 -0
  8. package/dist/binary.d.ts +23 -0
  9. package/dist/binary.d.ts.map +1 -0
  10. package/dist/binary.js +8 -0
  11. package/dist/bootstrap.d.ts +17 -5
  12. package/dist/bootstrap.d.ts.map +1 -1
  13. package/dist/bootstrap.js +92 -38
  14. package/dist/change-set.d.ts.map +1 -1
  15. package/dist/change-set.js +19 -12
  16. package/dist/change.d.ts +15 -0
  17. package/dist/change.d.ts.map +1 -1
  18. package/dist/change.js +26 -1
  19. package/dist/chat/acts.d.ts +33 -0
  20. package/dist/chat/acts.d.ts.map +1 -0
  21. package/dist/chat/acts.js +36 -0
  22. package/dist/chat/context.d.ts +3 -0
  23. package/dist/chat/context.d.ts.map +1 -0
  24. package/dist/chat/context.js +10 -0
  25. package/dist/chat/input.d.ts +9 -0
  26. package/dist/chat/input.d.ts.map +1 -0
  27. package/dist/chat/input.js +99 -0
  28. package/dist/chat/kit.d.ts +60 -0
  29. package/dist/chat/kit.d.ts.map +1 -0
  30. package/dist/chat/kit.js +123 -0
  31. package/dist/chat/lasting.d.ts +6 -0
  32. package/dist/chat/lasting.d.ts.map +1 -0
  33. package/dist/chat/lasting.js +19 -0
  34. package/dist/chat/list.d.ts +26 -0
  35. package/dist/chat/list.d.ts.map +1 -0
  36. package/dist/chat/list.js +62 -0
  37. package/dist/chat/markdown.d.ts +20 -0
  38. package/dist/chat/markdown.d.ts.map +1 -0
  39. package/dist/chat/markdown.js +188 -0
  40. package/dist/chat/message.d.ts +6 -0
  41. package/dist/chat/message.d.ts.map +1 -0
  42. package/dist/chat/message.js +192 -0
  43. package/dist/chat/messages.d.ts +21 -0
  44. package/dist/chat/messages.d.ts.map +1 -0
  45. package/dist/chat/messages.js +151 -0
  46. package/dist/chat/name.d.ts +6 -0
  47. package/dist/chat/name.d.ts.map +1 -0
  48. package/dist/chat/name.js +37 -0
  49. package/dist/chat/opened.d.ts +11 -0
  50. package/dist/chat/opened.d.ts.map +1 -0
  51. package/dist/chat/opened.js +17 -0
  52. package/dist/chat/overlay.d.ts +31 -0
  53. package/dist/chat/overlay.d.ts.map +1 -0
  54. package/dist/chat/overlay.js +97 -0
  55. package/dist/chat/parts.d.ts +57 -0
  56. package/dist/chat/parts.d.ts.map +1 -0
  57. package/dist/chat/parts.js +119 -0
  58. package/dist/chat/process.d.ts +20 -0
  59. package/dist/chat/process.d.ts.map +1 -0
  60. package/dist/chat/process.js +32 -0
  61. package/dist/chat/provider.d.ts +63 -0
  62. package/dist/chat/provider.d.ts.map +1 -0
  63. package/dist/chat/provider.js +620 -0
  64. package/dist/chat/question.d.ts +9 -0
  65. package/dist/chat/question.d.ts.map +1 -0
  66. package/dist/chat/question.js +80 -0
  67. package/dist/chat/questions.d.ts +31 -0
  68. package/dist/chat/questions.d.ts.map +1 -0
  69. package/dist/chat/questions.js +111 -0
  70. package/dist/chat/read.d.ts +5 -0
  71. package/dist/chat/read.d.ts.map +1 -0
  72. package/dist/chat/read.js +13 -0
  73. package/dist/chat/ready.d.ts +33 -0
  74. package/dist/chat/ready.d.ts.map +1 -0
  75. package/dist/chat/ready.js +91 -0
  76. package/dist/chat/style.d.ts +4 -0
  77. package/dist/chat/style.d.ts.map +1 -0
  78. package/dist/chat/style.js +302 -0
  79. package/dist/chat.d.ts +14 -0
  80. package/dist/chat.d.ts.map +1 -0
  81. package/dist/chat.js +8 -0
  82. package/dist/client.d.ts +15 -6
  83. package/dist/client.d.ts.map +1 -1
  84. package/dist/client.js +66 -16
  85. package/dist/collection.d.ts +6 -11
  86. package/dist/collection.d.ts.map +1 -1
  87. package/dist/collection.js +56 -19
  88. package/dist/connection.d.ts +22 -9
  89. package/dist/connection.d.ts.map +1 -1
  90. package/dist/connection.js +566 -175
  91. package/dist/conversation.d.ts +15 -5
  92. package/dist/conversation.d.ts.map +1 -1
  93. package/dist/conversation.js +189 -64
  94. package/dist/entry.d.ts +17 -0
  95. package/dist/entry.d.ts.map +1 -0
  96. package/dist/entry.js +156 -0
  97. package/dist/expectation.d.ts +21 -0
  98. package/dist/expectation.d.ts.map +1 -0
  99. package/dist/expectation.js +70 -0
  100. package/dist/failure.d.ts +13 -0
  101. package/dist/failure.d.ts.map +1 -0
  102. package/dist/failure.js +19 -0
  103. package/dist/fence.d.ts +6 -0
  104. package/dist/fence.d.ts.map +1 -0
  105. package/dist/fence.js +10 -0
  106. package/dist/host.d.ts +3 -1
  107. package/dist/host.d.ts.map +1 -1
  108. package/dist/host.js +16 -12
  109. package/dist/identity.d.ts +3 -0
  110. package/dist/identity.d.ts.map +1 -1
  111. package/dist/identity.js +13 -1
  112. package/dist/index.d.ts +9 -4
  113. package/dist/index.d.ts.map +1 -1
  114. package/dist/index.js +3 -0
  115. package/dist/memory.d.ts +5 -3
  116. package/dist/memory.d.ts.map +1 -1
  117. package/dist/memory.js +62 -20
  118. package/dist/model.d.ts +3 -2
  119. package/dist/model.d.ts.map +1 -1
  120. package/dist/open.d.ts +2 -0
  121. package/dist/open.d.ts.map +1 -0
  122. package/dist/open.js +1 -0
  123. package/dist/outcome.d.ts +32 -3
  124. package/dist/outcome.d.ts.map +1 -1
  125. package/dist/outcome.js +42 -1
  126. package/dist/overlay.d.ts +14 -3
  127. package/dist/overlay.d.ts.map +1 -1
  128. package/dist/overlay.js +75 -37
  129. package/dist/query.d.ts +4 -0
  130. package/dist/query.d.ts.map +1 -1
  131. package/dist/query.js +3 -0
  132. package/dist/reach.d.ts +2 -0
  133. package/dist/reach.d.ts.map +1 -1
  134. package/dist/reach.js +44 -20
  135. package/dist/react.d.ts +25 -16
  136. package/dist/react.d.ts.map +1 -1
  137. package/dist/react.js +8 -6
  138. package/dist/read-side.d.ts.map +1 -1
  139. package/dist/read-side.js +9 -2
  140. package/dist/refresh.d.ts +10 -0
  141. package/dist/refresh.d.ts.map +1 -1
  142. package/dist/refresh.js +42 -38
  143. package/dist/replica.d.ts +7 -4
  144. package/dist/replica.d.ts.map +1 -1
  145. package/dist/replica.js +74 -54
  146. package/dist/room-draft.d.ts +26 -9
  147. package/dist/room-draft.d.ts.map +1 -1
  148. package/dist/room-draft.js +132 -23
  149. package/dist/rows.d.ts +5 -1
  150. package/dist/rows.d.ts.map +1 -1
  151. package/dist/rows.js +10 -2
  152. package/dist/save.d.ts +26 -1
  153. package/dist/save.d.ts.map +1 -1
  154. package/dist/save.js +345 -37
  155. package/dist/search.d.ts +3 -2
  156. package/dist/search.d.ts.map +1 -1
  157. package/dist/status.d.ts +2 -2
  158. package/dist/status.d.ts.map +1 -1
  159. package/dist/status.js +0 -8
  160. package/dist/store.d.ts +48 -14
  161. package/dist/store.d.ts.map +1 -1
  162. package/dist/store.js +366 -170
  163. package/dist/wire.d.ts +7 -3
  164. package/dist/wire.d.ts.map +1 -1
  165. package/dist/wire.js +15 -2
  166. package/package.json +26 -14
  167. package/dist/attachment.d.ts +0 -3
  168. package/dist/attachment.d.ts.map +0 -1
  169. package/dist/attachment.js +0 -8
package/dist/store.js CHANGED
@@ -1,9 +1,11 @@
1
- import { sameChange } from './overlay.js';
1
+ import { sameBinary, sameChange } from './overlay.js';
2
2
  const DATABASE = 'mdruntime';
3
+ const AUTHORED = 'mdruntime-authored';
4
+ const FENCES = 'fences';
3
5
  const ROOMS = 'rooms';
4
6
  const DOCUMENTS = 'documents';
5
- const BINARY_TARGETS = 'binaryTargets';
6
- const ATTACHMENTS = 'attachments';
7
+ const BINARY_FILES = 'binaryFiles';
8
+ const TRANSFERS = 'transfers';
7
9
  const PENDING_CHANGES = 'pendingChanges';
8
10
  const DECLARATIONS = 'declarations';
9
11
  const ROOM_DRAFTS = 'roomDrafts';
@@ -11,8 +13,24 @@ const PRESENTATION = 'presentation';
11
13
  const CONVERSATIONS = 'conversations';
12
14
  const MESSAGES = 'messages';
13
15
  const CHAT_DRAFTS = 'chatDrafts';
16
+ const QUEUES = 'queues';
17
+ const FACTS = 'facts';
14
18
  const CURRENT = 'current';
15
- const SCHEMA = 20;
19
+ const MEMBER = 'member';
20
+ const GENERATION = 'generation';
21
+ const HOLDER = 'holder';
22
+ const BINDING = 'binding';
23
+ export const STORE_SCHEMA = 23;
24
+ export const AUTHORED_SCHEMA = 2;
25
+ const AUTHORED_STORES = [
26
+ [QUEUES, { keyPath: 'locator' }],
27
+ [PENDING_CHANGES, { keyPath: ['locator', 'id'] }],
28
+ [ROOM_DRAFTS, { keyPath: ['locator', 'path'] }],
29
+ [CHAT_DRAFTS, { keyPath: 'conversation' }],
30
+ [FACTS, undefined],
31
+ [FENCES, { keyPath: 'locator' }],
32
+ ];
33
+ const READ_BATCH = 256;
16
34
  function settled(request) {
17
35
  return new Promise((resolve, reject) => {
18
36
  request.onsuccess = () => resolve(request.result);
@@ -28,7 +46,6 @@ function completed(transaction) {
28
46
  }
29
47
  const EMPTY_DECLARATIONS = {
30
48
  lastCompleted: null,
31
- generation: null,
32
49
  providerLimits: [],
33
50
  capabilities: [],
34
51
  };
@@ -56,9 +73,102 @@ function amendPending(database, locator, id, amendment) {
56
73
  };
57
74
  return completed(transaction);
58
75
  }
59
- function roomRecord(record, queue) {
60
- const { documents, binaryTargets, revisions, changes, written, attachments, ...room } = record;
61
- return { ...room, order: queue.map((batch) => batch.id) };
76
+ function roomRecord(record) {
77
+ const { documents, binaries, revisions, changes, written, ...room } = record;
78
+ return room;
79
+ }
80
+ function chatDraftRecord(record) {
81
+ return { ...record, revision: record.revision ?? '' };
82
+ }
83
+ function underTransfer(locator, transfer, after) {
84
+ return after === undefined
85
+ ? IDBKeyRange.bound([locator, transfer], [locator, transfer, []])
86
+ : IDBKeyRange.bound([locator, transfer, after], [locator, transfer, []], true);
87
+ }
88
+ function underRoom(locator) {
89
+ return IDBKeyRange.bound([locator], [locator, []]);
90
+ }
91
+ async function readTransfer(store, locator, transfer, receive) {
92
+ let range = underTransfer(locator, transfer);
93
+ for (;;) {
94
+ const batch = await settled(store.getAll(range, READ_BATCH));
95
+ for (const record of batch)
96
+ receive(record);
97
+ if (batch.length < READ_BATCH)
98
+ return;
99
+ range = underTransfer(locator, transfer, batch[batch.length - 1].path);
100
+ }
101
+ }
102
+ function emptyLayer() {
103
+ return { documents: {}, binaries: {}, revisions: {}, changes: {}, written: {} };
104
+ }
105
+ async function readLayer(transaction, locator, transfer) {
106
+ const layer = emptyLayer();
107
+ const head = (held) => {
108
+ if (held.revision !== null)
109
+ layer.revisions[held.path] = held.revision;
110
+ if (held.change !== null)
111
+ layer.changes[held.path] = held.change;
112
+ };
113
+ await readTransfer(transaction.objectStore(DOCUMENTS), locator, transfer, (document) => {
114
+ layer.documents[document.path] = document.content;
115
+ if (document.written !== null)
116
+ layer.written[document.path] = document.written;
117
+ head(document);
118
+ });
119
+ await readTransfer(transaction.objectStore(BINARY_FILES), locator, transfer, (file) => {
120
+ layer.binaries[file.path] = file.binary;
121
+ head(file);
122
+ });
123
+ return layer;
124
+ }
125
+ function documentRecord(locator, transfer, layer, path) {
126
+ return {
127
+ locator,
128
+ transfer,
129
+ path,
130
+ content: layer.documents[path],
131
+ revision: layer.revisions[path] ?? null,
132
+ change: layer.changes[path] ?? null,
133
+ written: layer.written[path] ?? null,
134
+ };
135
+ }
136
+ function binaryFileRecord(locator, transfer, layer, path) {
137
+ return {
138
+ locator,
139
+ transfer,
140
+ path,
141
+ revision: layer.revisions[path] ?? null,
142
+ change: layer.changes[path] ?? null,
143
+ binary: layer.binaries[path],
144
+ };
145
+ }
146
+ function writeLayer(transaction, locator, transfer, next, base) {
147
+ const documents = transaction.objectStore(DOCUMENTS);
148
+ for (const path of Object.keys(base?.documents ?? {})) {
149
+ if (next.documents[path] === undefined)
150
+ documents.delete([locator, transfer, path]);
151
+ }
152
+ for (const [path, content] of Object.entries(next.documents)) {
153
+ if (base?.documents[path] === content &&
154
+ base.revisions[path] === next.revisions[path] &&
155
+ base.written[path] === next.written[path] &&
156
+ sameChange(base.changes[path], next.changes[path]))
157
+ continue;
158
+ documents.put(documentRecord(locator, transfer, next, path));
159
+ }
160
+ const files = transaction.objectStore(BINARY_FILES);
161
+ for (const path of Object.keys(base?.binaries ?? {})) {
162
+ if (next.binaries[path] === undefined)
163
+ files.delete([locator, transfer, path]);
164
+ }
165
+ for (const path of Object.keys(next.binaries)) {
166
+ if (!sameBinary(base?.binaries[path], next.binaries[path]) ||
167
+ base?.revisions[path] !== next.revisions[path] ||
168
+ !sameChange(base?.changes[path], next.changes[path])) {
169
+ files.put(binaryFileRecord(locator, transfer, next, path));
170
+ }
171
+ }
62
172
  }
63
173
  function removeRoomEntries(store, locator) {
64
174
  const keys = store.getAllKeys();
@@ -69,177 +179,184 @@ function removeRoomEntries(store, locator) {
69
179
  }
70
180
  };
71
181
  }
72
- export function removeRuntimeStore() {
182
+ function removeDatabase(name) {
73
183
  return new Promise((resolve, reject) => {
74
- const removal = indexedDB.deleteDatabase(DATABASE);
184
+ const removal = indexedDB.deleteDatabase(name);
75
185
  removal.onsuccess = () => resolve();
76
186
  removal.onerror = () => reject(removal.error);
77
187
  });
78
188
  }
79
- export async function openRuntimeStore() {
80
- const opening = indexedDB.open(DATABASE, SCHEMA);
189
+ export async function authoredHolder() {
190
+ const authored = await openAuthored();
191
+ try {
192
+ const transaction = authored.transaction(FACTS, 'readonly');
193
+ const held = transaction.objectStore(FACTS).get(HOLDER);
194
+ await completed(transaction);
195
+ return typeof held.result === 'string' ? held.result : null;
196
+ }
197
+ finally {
198
+ authored.close();
199
+ }
200
+ }
201
+ export async function authoredBy(holder) {
202
+ const authored = await openAuthored();
203
+ try {
204
+ const transaction = authored.transaction(FACTS, 'readwrite');
205
+ const facts = transaction.objectStore(FACTS);
206
+ const held = facts.get(HOLDER);
207
+ let owned = true;
208
+ held.onsuccess = () => {
209
+ if (held.result === undefined)
210
+ facts.put(holder, HOLDER);
211
+ else
212
+ owned = held.result === holder;
213
+ };
214
+ await completed(transaction);
215
+ return owned;
216
+ }
217
+ finally {
218
+ authored.close();
219
+ }
220
+ }
221
+ export async function authoredBinding() {
222
+ const authored = await openAuthored();
223
+ try {
224
+ const transaction = authored.transaction(FACTS, 'readonly');
225
+ const held = transaction.objectStore(FACTS).get(BINDING);
226
+ await completed(transaction);
227
+ return typeof held.result === 'string' ? held.result : null;
228
+ }
229
+ finally {
230
+ authored.close();
231
+ }
232
+ }
233
+ export async function bindAuthored(binding) {
234
+ const authored = await openAuthored();
235
+ try {
236
+ const transaction = authored.transaction(FACTS, 'readwrite');
237
+ transaction.objectStore(FACTS).put(binding, BINDING);
238
+ await completed(transaction);
239
+ }
240
+ finally {
241
+ authored.close();
242
+ }
243
+ }
244
+ export async function removeRuntimeStore(holder) {
245
+ await removeDatabase(DATABASE);
246
+ if (holder !== undefined && (await authoredBy(holder)))
247
+ return true;
248
+ await removeDatabase(AUTHORED);
249
+ return false;
250
+ }
251
+ function openReplica() {
252
+ const opening = indexedDB.open(DATABASE, STORE_SCHEMA);
81
253
  opening.onupgradeneeded = () => {
82
254
  const database = opening.result;
83
255
  for (const name of [...database.objectStoreNames])
84
256
  database.deleteObjectStore(name);
85
257
  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'] });
258
+ database.createObjectStore(DOCUMENTS, { keyPath: ['locator', 'transfer', 'path'] });
259
+ database.createObjectStore(BINARY_FILES, { keyPath: ['locator', 'transfer', 'path'] });
260
+ database.createObjectStore(TRANSFERS, { keyPath: 'locator' });
90
261
  database.createObjectStore(DECLARATIONS);
91
- database.createObjectStore(ROOM_DRAFTS, { keyPath: ['locator', 'path'] });
92
262
  database.createObjectStore(PRESENTATION, { keyPath: 'app' });
93
263
  database.createObjectStore(CONVERSATIONS, { keyPath: 'conversation' });
94
264
  const messages = database.createObjectStore(MESSAGES, {
95
265
  keyPath: ['conversation', 'identity'],
96
266
  });
97
267
  messages.createIndex('conversation', 'conversation');
98
- database.createObjectStore(CHAT_DRAFTS, { keyPath: 'conversation' });
99
268
  };
100
- const database = await settled(opening);
269
+ return settled(opening);
270
+ }
271
+ function openAuthored() {
272
+ const opening = indexedDB.open(AUTHORED, AUTHORED_SCHEMA);
273
+ opening.onupgradeneeded = () => {
274
+ const database = opening.result;
275
+ for (const [name, parameters] of AUTHORED_STORES) {
276
+ if (!database.objectStoreNames.contains(name))
277
+ database.createObjectStore(name, parameters);
278
+ }
279
+ };
280
+ return settled(opening);
281
+ }
282
+ export async function openRuntimeStore(fenced) {
283
+ const authored = await openAuthored();
284
+ const database = await openReplica().catch((error) => {
285
+ authored.close();
286
+ throw error;
287
+ });
101
288
  return {
102
289
  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
- }
129
- };
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);
290
+ const transaction = database.transaction([ROOMS, DOCUMENTS, BINARY_FILES], 'readonly');
291
+ const rooms = await settled(transaction.objectStore(ROOMS).getAll());
292
+ const records = [];
293
+ for (const room of rooms.filter((held) => !fenced(held.locator))) {
294
+ records.push({ ...room, ...(await readLayer(transaction, room.locator, room.transfer)) });
139
295
  }
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));
296
+ return records;
297
+ },
298
+ async keepAuthored({ locator, queue, saved, roomDrafts, consumedRoomDrafts }) {
299
+ if (fenced(locator))
300
+ return;
301
+ const transaction = authored.transaction([QUEUES, PENDING_CHANGES, ROOM_DRAFTS], 'readwrite');
302
+ const order = transaction.objectStore(QUEUES);
303
+ const pending = transaction.objectStore(PENDING_CHANGES);
304
+ const held = order.get(locator);
305
+ held.onsuccess = () => {
306
+ const before = new Set(held.result?.order ?? []);
212
307
  const after = new Set(queue.map((batch) => batch.id));
308
+ if (queue.length === 0)
309
+ order.delete(locator);
310
+ else
311
+ order.put({ locator, order: [...after] });
213
312
  for (const id of before) {
214
313
  if (!after.has(id))
215
- pending.delete([next.locator, id]);
314
+ pending.delete([locator, id]);
216
315
  }
217
316
  for (const batch of queue) {
218
317
  if (!before.has(batch.id)) {
219
318
  pending.put({
220
- locator: next.locator,
319
+ locator,
221
320
  id: batch.id,
222
321
  batch,
223
- drafts: saved ?? [],
322
+ drafts: saved?.get(batch.id) ?? [],
224
323
  submission: { state: 'waiting' },
225
324
  lost: false,
226
325
  });
227
326
  }
228
327
  }
229
- }
328
+ };
230
329
  const draftStore = transaction.objectStore(ROOM_DRAFTS);
231
330
  for (const path of consumedRoomDrafts ?? [])
232
- draftStore.delete([next.locator, path]);
331
+ draftStore.delete([locator, path]);
233
332
  for (const record of roomDrafts ?? [])
234
333
  draftStore.put(record);
235
334
  await completed(transaction);
236
335
  },
336
+ async write({ next, previous, stored }) {
337
+ if (next === previous || fenced(next.locator))
338
+ return;
339
+ const transaction = database.transaction([ROOMS, DOCUMENTS, BINARY_FILES, TRANSFERS], 'readwrite');
340
+ transaction.objectStore(ROOMS).put(roomRecord(next));
341
+ const moved = previous?.transfer !== next.transfer;
342
+ if (moved && previous !== null) {
343
+ const range = underTransfer(previous.locator, previous.transfer);
344
+ transaction.objectStore(DOCUMENTS).delete(range);
345
+ transaction.objectStore(BINARY_FILES).delete(range);
346
+ }
347
+ if (moved)
348
+ transaction.objectStore(TRANSFERS).delete(next.locator);
349
+ writeLayer(transaction, next.locator, next.transfer, next, moved ? (stored ?? null) : previous);
350
+ await completed(transaction);
351
+ },
237
352
  async revokeModels(records) {
238
353
  const transaction = database.transaction(ROOMS, 'readwrite');
239
354
  const rooms = transaction.objectStore(ROOMS);
240
355
  const revoked = { status: 'unavailable', reason: 'revoked' };
241
- for (const { record, queue } of records) {
242
- rooms.put(roomRecord({ ...record, model: revoked, selectionModel: revoked }, queue));
356
+ for (const record of records) {
357
+ if (fenced(record.locator))
358
+ continue;
359
+ rooms.put(roomRecord({ ...record, model: revoked, selectionModel: revoked }));
243
360
  }
244
361
  await completed(transaction);
245
362
  },
@@ -262,36 +379,52 @@ export async function openRuntimeStore() {
262
379
  capabilities,
263
380
  }));
264
381
  },
382
+ async member() {
383
+ const transaction = database.transaction(DECLARATIONS, 'readonly');
384
+ const held = await settled(transaction.objectStore(DECLARATIONS).get(MEMBER));
385
+ return held ?? null;
386
+ },
387
+ async keepMember(member) {
388
+ const transaction = database.transaction(DECLARATIONS, 'readwrite');
389
+ transaction.objectStore(DECLARATIONS).put(member, MEMBER);
390
+ await completed(transaction);
391
+ },
265
392
  async queues() {
266
- const transaction = database.transaction([ROOMS, PENDING_CHANGES], 'readonly');
267
- const [rooms, pending] = await Promise.all([
268
- settled(transaction.objectStore(ROOMS).getAll()),
393
+ const transaction = authored.transaction([QUEUES, PENDING_CHANGES], 'readonly');
394
+ const [queues, pending] = await Promise.all([
395
+ settled(transaction.objectStore(QUEUES).getAll()),
269
396
  settled(transaction.objectStore(PENDING_CHANGES).getAll()),
270
397
  ]);
271
398
  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}`);
399
+ return queues
400
+ .filter((queue) => !fenced(queue.locator))
401
+ .map((queue) => ({
402
+ locator: queue.locator,
403
+ pending: queue.order.map((id) => {
404
+ const record = byId.get(`${queue.locator}\0${id}`);
276
405
  if (record === undefined)
277
- throw new Error(`Missing Pending Change ${room.locator}/${id}`);
406
+ throw new Error(`Missing Pending Change ${queue.locator}/${id}`);
278
407
  const { batch, drafts, submission, lost, upload, accepted } = record;
279
408
  return { batch, state: { drafts, submission, lost, upload, accepted } };
280
409
  }),
281
410
  }));
282
411
  },
283
- submit(locator, id, submission, lost) {
284
- return amendPending(database, locator, id, { submission, lost });
412
+ async submit(locator, id, submission, lost) {
413
+ if (!fenced(locator))
414
+ await amendPending(authored, locator, id, { submission, lost });
285
415
  },
286
- accept(locator, id, roomVersion) {
287
- return amendPending(database, locator, id, { accepted: roomVersion });
416
+ async accept(locator, id, roomVersion) {
417
+ if (!fenced(locator))
418
+ await amendPending(authored, locator, id, { accepted: roomVersion });
288
419
  },
289
- keepUpload(locator, id, upload) {
290
- return amendPending(database, locator, id, { upload });
420
+ async keepUpload(locator, id, upload) {
421
+ if (!fenced(locator))
422
+ await amendPending(authored, locator, id, { upload });
291
423
  },
292
424
  async roomDrafts() {
293
- const transaction = database.transaction(ROOM_DRAFTS, 'readonly');
294
- return settled(transaction.objectStore(ROOM_DRAFTS).getAll());
425
+ const transaction = authored.transaction(ROOM_DRAFTS, 'readonly');
426
+ const held = await settled(transaction.objectStore(ROOM_DRAFTS).getAll());
427
+ return held.filter((record) => !fenced(record.locator));
295
428
  },
296
429
  async presentation() {
297
430
  const transaction = database.transaction(PRESENTATION, 'readonly');
@@ -319,8 +452,8 @@ export async function openRuntimeStore() {
319
452
  }
320
453
  return [...held.values()];
321
454
  },
322
- async keepMessages({ conversation, messages, read, sealed }) {
323
- const transaction = database.transaction([CONVERSATIONS, MESSAGES, CHAT_DRAFTS], 'readwrite');
455
+ async keepMessages({ conversation, messages, read }) {
456
+ const transaction = database.transaction([CONVERSATIONS, MESSAGES], 'readwrite');
324
457
  const stored = transaction.objectStore(MESSAGES);
325
458
  for (const message of messages) {
326
459
  stored.put({ ...message, conversation });
@@ -330,16 +463,17 @@ export async function openRuntimeStore() {
330
463
  .objectStore(CONVERSATIONS)
331
464
  .put({ conversation, read });
332
465
  }
333
- if (sealed === true)
334
- transaction.objectStore(CHAT_DRAFTS).delete(conversation);
335
466
  await completed(transaction);
336
467
  },
337
468
  async dropConversations(conversations) {
338
- const transaction = database.transaction([CONVERSATIONS, MESSAGES, CHAT_DRAFTS], 'readwrite');
469
+ const drafts = authored.transaction(CHAT_DRAFTS, 'readwrite');
470
+ for (const conversation of conversations)
471
+ drafts.objectStore(CHAT_DRAFTS).delete(conversation);
472
+ await completed(drafts);
473
+ const transaction = database.transaction([CONVERSATIONS, MESSAGES], 'readwrite');
339
474
  const messages = transaction.objectStore(MESSAGES);
340
475
  for (const conversation of conversations) {
341
476
  transaction.objectStore(CONVERSATIONS).delete(conversation);
342
- transaction.objectStore(CHAT_DRAFTS).delete(conversation);
343
477
  const keys = messages.index('conversation').getAllKeys(conversation);
344
478
  keys.onsuccess = () => {
345
479
  for (const key of keys.result)
@@ -349,46 +483,108 @@ export async function openRuntimeStore() {
349
483
  await completed(transaction);
350
484
  },
351
485
  async chatDrafts() {
352
- const transaction = database.transaction(CHAT_DRAFTS, 'readonly');
353
- return settled(transaction.objectStore(CHAT_DRAFTS).getAll());
486
+ const transaction = authored.transaction(CHAT_DRAFTS, 'readonly');
487
+ const held = await settled(transaction.objectStore(CHAT_DRAFTS).getAll());
488
+ return held.map(chatDraftRecord);
354
489
  },
355
490
  async keepChatDraft(record) {
356
- const transaction = database.transaction(CHAT_DRAFTS, 'readwrite');
491
+ const transaction = authored.transaction(CHAT_DRAFTS, 'readwrite');
357
492
  transaction.objectStore(CHAT_DRAFTS).put(record);
358
493
  await completed(transaction);
359
494
  },
360
495
  async dropChatDraft(conversation) {
361
- const transaction = database.transaction(CHAT_DRAFTS, 'readwrite');
496
+ const transaction = authored.transaction(CHAT_DRAFTS, 'readwrite');
362
497
  transaction.objectStore(CHAT_DRAFTS).delete(conversation);
363
498
  await completed(transaction);
364
499
  },
365
500
  async keepRoomDraft(record) {
366
- const transaction = database.transaction(ROOM_DRAFTS, 'readwrite');
501
+ if (fenced(record.locator))
502
+ return;
503
+ const transaction = authored.transaction(ROOM_DRAFTS, 'readwrite');
367
504
  transaction.objectStore(ROOM_DRAFTS).put(record);
368
505
  await completed(transaction);
369
506
  },
370
507
  async discardRoomDraft(locator, path) {
371
- const transaction = database.transaction(ROOM_DRAFTS, 'readwrite');
508
+ if (fenced(locator))
509
+ return;
510
+ const transaction = authored.transaction(ROOM_DRAFTS, 'readwrite');
372
511
  transaction.objectStore(ROOM_DRAFTS).delete([locator, path]);
373
512
  await completed(transaction);
374
513
  },
375
- async dropRooms(locators) {
376
- const transaction = database.transaction([ROOMS, DOCUMENTS, BINARY_TARGETS, ATTACHMENTS, PENDING_CHANGES, ROOM_DRAFTS], 'readwrite');
514
+ async fences() {
515
+ const transaction = authored.transaction(FENCES, 'readonly');
516
+ const held = await settled(transaction.objectStore(FENCES).getAll());
517
+ return held.map((fence) => fence.locator);
518
+ },
519
+ async erase(locators) {
520
+ const work = authored.transaction([FENCES, QUEUES, PENDING_CHANGES, ROOM_DRAFTS], 'readwrite');
377
521
  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);
522
+ work.objectStore(FENCES).put({ locator });
523
+ work.objectStore(QUEUES).delete(locator);
524
+ for (const name of [PENDING_CHANGES, ROOM_DRAFTS]) {
525
+ removeRoomEntries(work.objectStore(name), locator);
381
526
  }
382
527
  }
528
+ await completed(work);
529
+ const transaction = database.transaction([ROOMS, DOCUMENTS, BINARY_FILES, TRANSFERS], 'readwrite');
530
+ for (const locator of locators) {
531
+ transaction.objectStore(ROOMS).delete(locator);
532
+ transaction.objectStore(TRANSFERS).delete(locator);
533
+ transaction.objectStore(DOCUMENTS).delete(underRoom(locator));
534
+ transaction.objectStore(BINARY_FILES).delete(underRoom(locator));
535
+ }
536
+ await completed(transaction);
537
+ const fences = authored.transaction(FENCES, 'readwrite');
538
+ for (const locator of locators)
539
+ fences.objectStore(FENCES).delete(locator);
540
+ await completed(fences);
541
+ },
542
+ async transfers() {
543
+ const transaction = database.transaction(TRANSFERS, 'readonly');
544
+ const held = await settled(transaction.objectStore(TRANSFERS).getAll());
545
+ return held.filter((record) => !fenced(record.locator));
546
+ },
547
+ staged(record) {
548
+ const transaction = database.transaction([DOCUMENTS, BINARY_FILES], 'readonly');
549
+ return readLayer(transaction, record.locator, record.transfer);
550
+ },
551
+ async stage(record, page) {
552
+ if (fenced(record.locator))
553
+ return;
554
+ const transaction = database.transaction([TRANSFERS, DOCUMENTS, BINARY_FILES], 'readwrite');
555
+ writeLayer(transaction, record.locator, record.transfer, page, null);
556
+ transaction.objectStore(TRANSFERS).put(record);
557
+ await completed(transaction);
558
+ },
559
+ async abandon(locator) {
560
+ if (fenced(locator))
561
+ return;
562
+ const transaction = database.transaction([TRANSFERS, DOCUMENTS, BINARY_FILES], 'readwrite');
563
+ const transfers = transaction.objectStore(TRANSFERS);
564
+ const held = transfers.get(locator);
565
+ held.onsuccess = () => {
566
+ const record = held.result;
567
+ if (record === undefined)
568
+ return;
569
+ transfers.delete(locator);
570
+ const range = underTransfer(locator, record.transfer);
571
+ transaction.objectStore(DOCUMENTS).delete(range);
572
+ transaction.objectStore(BINARY_FILES).delete(range);
573
+ };
383
574
  await completed(transaction);
384
575
  },
385
576
  async generation() {
386
- return (await declaration(database)).generation;
577
+ const transaction = authored.transaction(FACTS, 'readonly');
578
+ const held = await settled(transaction.objectStore(FACTS).get(GENERATION));
579
+ return held ?? null;
387
580
  },
388
- rememberGeneration(value) {
389
- return updateDeclaration(database, (record) => ({ ...record, generation: value }));
581
+ async rememberGeneration(value) {
582
+ const transaction = authored.transaction(FACTS, 'readwrite');
583
+ transaction.objectStore(FACTS).put(value, GENERATION);
584
+ await completed(transaction);
390
585
  },
391
586
  close() {
587
+ authored.close();
392
588
  database.close();
393
589
  },
394
590
  };