@agentshouse/mdruntime 0.1.0-c315a76f079f0 → 0.1.0-c31fdf9e58db7

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 (234) hide show
  1. package/README.md +18 -15
  2. package/dist/adapter.d.ts +111 -72
  3. package/dist/adapter.d.ts.map +1 -1
  4. package/dist/attached-model.d.ts +5 -1
  5. package/dist/attached-model.d.ts.map +1 -1
  6. package/dist/attached-model.js +38 -4
  7. package/dist/binary.d.ts +0 -5
  8. package/dist/binary.d.ts.map +1 -1
  9. package/dist/binary.js +1 -1
  10. package/dist/bootstrap.d.ts +25 -10
  11. package/dist/bootstrap.d.ts.map +1 -1
  12. package/dist/bootstrap.js +180 -103
  13. package/dist/chain.d.ts +11 -0
  14. package/dist/chain.d.ts.map +1 -0
  15. package/dist/chain.js +64 -0
  16. package/dist/change-set.d.ts +9 -16
  17. package/dist/change-set.d.ts.map +1 -1
  18. package/dist/change-set.js +11 -89
  19. package/dist/change.d.ts +31 -1
  20. package/dist/change.d.ts.map +1 -1
  21. package/dist/change.js +10 -1
  22. package/dist/client.d.ts +14 -9
  23. package/dist/client.d.ts.map +1 -1
  24. package/dist/client.js +82 -7
  25. package/dist/collection.d.ts +10 -21
  26. package/dist/collection.d.ts.map +1 -1
  27. package/dist/collection.js +39 -291
  28. package/dist/compile.d.ts +16 -0
  29. package/dist/compile.d.ts.map +1 -0
  30. package/dist/compile.js +786 -0
  31. package/dist/connection.d.ts +23 -14
  32. package/dist/connection.d.ts.map +1 -1
  33. package/dist/connection.js +2866 -1412
  34. package/dist/conversation.d.ts +24 -11
  35. package/dist/conversation.d.ts.map +1 -1
  36. package/dist/conversation.js +411 -194
  37. package/dist/digest.d.ts +7 -0
  38. package/dist/digest.d.ts.map +1 -0
  39. package/dist/digest.js +113 -0
  40. package/dist/durability.d.ts +2 -5
  41. package/dist/durability.d.ts.map +1 -1
  42. package/dist/durability.js +5 -0
  43. package/dist/entry.d.ts +4 -1
  44. package/dist/entry.d.ts.map +1 -1
  45. package/dist/entry.js +18 -23
  46. package/dist/file.d.ts +2 -19
  47. package/dist/file.d.ts.map +1 -1
  48. package/dist/file.js +7 -122
  49. package/dist/fulltext.d.ts +27 -0
  50. package/dist/fulltext.d.ts.map +1 -0
  51. package/dist/fulltext.js +370 -0
  52. package/dist/host.d.ts +3 -8
  53. package/dist/host.d.ts.map +1 -1
  54. package/dist/host.js +52 -24
  55. package/dist/identity.d.ts +0 -1
  56. package/dist/identity.d.ts.map +1 -1
  57. package/dist/identity.js +0 -3
  58. package/dist/index.d.ts +15 -12
  59. package/dist/index.d.ts.map +1 -1
  60. package/dist/index.js +6 -2
  61. package/dist/live.d.ts +22 -0
  62. package/dist/live.d.ts.map +1 -0
  63. package/dist/live.js +123 -0
  64. package/dist/media.d.ts +8 -0
  65. package/dist/media.d.ts.map +1 -0
  66. package/dist/media.js +218 -0
  67. package/dist/memory.d.ts +10 -7
  68. package/dist/memory.d.ts.map +1 -1
  69. package/dist/memory.js +196 -105
  70. package/dist/messages.d.ts +28 -0
  71. package/dist/messages.d.ts.map +1 -0
  72. package/dist/messages.js +90 -0
  73. package/dist/model.d.ts +6 -1
  74. package/dist/model.d.ts.map +1 -1
  75. package/dist/model.js +7 -10
  76. package/dist/name.d.ts +4 -0
  77. package/dist/name.d.ts.map +1 -0
  78. package/dist/name.js +122 -0
  79. package/dist/observation.d.ts +15 -0
  80. package/dist/observation.d.ts.map +1 -1
  81. package/dist/observation.js +93 -3
  82. package/dist/opened.d.ts +22 -0
  83. package/dist/opened.d.ts.map +1 -0
  84. package/dist/opened.js +107 -0
  85. package/dist/outcome.d.ts +40 -8
  86. package/dist/outcome.d.ts.map +1 -1
  87. package/dist/outcome.js +38 -2
  88. package/dist/overlay.d.ts +2 -5
  89. package/dist/overlay.d.ts.map +1 -1
  90. package/dist/overlay.js +87 -172
  91. package/dist/paths.d.ts +1 -0
  92. package/dist/paths.d.ts.map +1 -1
  93. package/dist/paths.js +4 -0
  94. package/dist/query.d.ts +32 -2
  95. package/dist/query.d.ts.map +1 -1
  96. package/dist/query.js +41 -6
  97. package/dist/reach.d.ts +13 -4
  98. package/dist/reach.d.ts.map +1 -1
  99. package/dist/reach.js +209 -77
  100. package/dist/react.d.ts +1 -2
  101. package/dist/react.d.ts.map +1 -1
  102. package/dist/react.js +2 -14
  103. package/dist/readiness.d.ts +14 -15
  104. package/dist/readiness.d.ts.map +1 -1
  105. package/dist/readiness.js +2 -8
  106. package/dist/recovery.d.ts +3 -2
  107. package/dist/recovery.d.ts.map +1 -1
  108. package/dist/recovery.js +34 -7
  109. package/dist/refresh.d.ts +18 -15
  110. package/dist/refresh.d.ts.map +1 -1
  111. package/dist/refresh.js +136 -58
  112. package/dist/replica.d.ts +54 -36
  113. package/dist/replica.d.ts.map +1 -1
  114. package/dist/replica.js +279 -189
  115. package/dist/room-draft.d.ts +21 -3
  116. package/dist/room-draft.d.ts.map +1 -1
  117. package/dist/room-draft.js +102 -28
  118. package/dist/rows.d.ts +38 -30
  119. package/dist/rows.d.ts.map +1 -1
  120. package/dist/rows.js +173 -96
  121. package/dist/save.d.ts +52 -14
  122. package/dist/save.d.ts.map +1 -1
  123. package/dist/save.js +513 -185
  124. package/dist/search.d.ts +23 -42
  125. package/dist/search.d.ts.map +1 -1
  126. package/dist/search.js +28 -182
  127. package/dist/seed.d.ts.map +1 -1
  128. package/dist/seed.js +18 -4
  129. package/dist/shared.d.ts +7 -3
  130. package/dist/shared.d.ts.map +1 -1
  131. package/dist/shared.js +119 -66
  132. package/dist/status.d.ts +4 -3
  133. package/dist/status.d.ts.map +1 -1
  134. package/dist/status.js +3 -1
  135. package/dist/store.d.ts +83 -98
  136. package/dist/store.d.ts.map +1 -1
  137. package/dist/store.js +300 -685
  138. package/dist/stored.d.ts +130 -0
  139. package/dist/stored.d.ts.map +1 -0
  140. package/dist/stored.js +560 -0
  141. package/dist/tables.d.ts +325 -0
  142. package/dist/tables.d.ts.map +1 -0
  143. package/dist/tables.js +578 -0
  144. package/dist/transfer.d.ts +0 -5
  145. package/dist/transfer.d.ts.map +1 -1
  146. package/dist/turn.d.ts +5 -3
  147. package/dist/turn.d.ts.map +1 -1
  148. package/dist/turn.js +51 -21
  149. package/dist/validation.d.ts +29 -0
  150. package/dist/validation.d.ts.map +1 -0
  151. package/dist/validation.js +101 -0
  152. package/dist/windows.d.ts +22 -0
  153. package/dist/windows.d.ts.map +1 -0
  154. package/dist/windows.js +89 -0
  155. package/dist/wire.d.ts +25 -5
  156. package/dist/wire.d.ts.map +1 -1
  157. package/dist/wire.js +7 -2
  158. package/dist/words.d.ts +19 -0
  159. package/dist/words.d.ts.map +1 -0
  160. package/dist/words.js +156 -0
  161. package/package.json +18 -19
  162. package/AGENTS.md +0 -307
  163. package/dist/acquisition.d.ts +0 -14
  164. package/dist/acquisition.d.ts.map +0 -1
  165. package/dist/acquisition.js +0 -24
  166. package/dist/chat/acts.d.ts +0 -33
  167. package/dist/chat/acts.d.ts.map +0 -1
  168. package/dist/chat/acts.js +0 -36
  169. package/dist/chat/context.d.ts +0 -3
  170. package/dist/chat/context.d.ts.map +0 -1
  171. package/dist/chat/context.js +0 -10
  172. package/dist/chat/input.d.ts +0 -9
  173. package/dist/chat/input.d.ts.map +0 -1
  174. package/dist/chat/input.js +0 -99
  175. package/dist/chat/kit.d.ts +0 -61
  176. package/dist/chat/kit.d.ts.map +0 -1
  177. package/dist/chat/kit.js +0 -123
  178. package/dist/chat/lasting.d.ts +0 -6
  179. package/dist/chat/lasting.d.ts.map +0 -1
  180. package/dist/chat/lasting.js +0 -19
  181. package/dist/chat/list.d.ts +0 -27
  182. package/dist/chat/list.d.ts.map +0 -1
  183. package/dist/chat/list.js +0 -63
  184. package/dist/chat/markdown.d.ts +0 -20
  185. package/dist/chat/markdown.d.ts.map +0 -1
  186. package/dist/chat/markdown.js +0 -188
  187. package/dist/chat/message.d.ts +0 -6
  188. package/dist/chat/message.d.ts.map +0 -1
  189. package/dist/chat/message.js +0 -192
  190. package/dist/chat/messages.d.ts +0 -22
  191. package/dist/chat/messages.d.ts.map +0 -1
  192. package/dist/chat/messages.js +0 -155
  193. package/dist/chat/name.d.ts +0 -6
  194. package/dist/chat/name.d.ts.map +0 -1
  195. package/dist/chat/name.js +0 -37
  196. package/dist/chat/opened.d.ts +0 -11
  197. package/dist/chat/opened.d.ts.map +0 -1
  198. package/dist/chat/opened.js +0 -17
  199. package/dist/chat/overlay.d.ts +0 -31
  200. package/dist/chat/overlay.d.ts.map +0 -1
  201. package/dist/chat/overlay.js +0 -97
  202. package/dist/chat/parts.d.ts +0 -57
  203. package/dist/chat/parts.d.ts.map +0 -1
  204. package/dist/chat/parts.js +0 -119
  205. package/dist/chat/process.d.ts +0 -20
  206. package/dist/chat/process.d.ts.map +0 -1
  207. package/dist/chat/process.js +0 -32
  208. package/dist/chat/provider.d.ts +0 -63
  209. package/dist/chat/provider.d.ts.map +0 -1
  210. package/dist/chat/provider.js +0 -629
  211. package/dist/chat/question.d.ts +0 -9
  212. package/dist/chat/question.d.ts.map +0 -1
  213. package/dist/chat/question.js +0 -69
  214. package/dist/chat/questions.d.ts +0 -31
  215. package/dist/chat/questions.d.ts.map +0 -1
  216. package/dist/chat/questions.js +0 -111
  217. package/dist/chat/read.d.ts +0 -5
  218. package/dist/chat/read.d.ts.map +0 -1
  219. package/dist/chat/read.js +0 -13
  220. package/dist/chat/ready.d.ts +0 -34
  221. package/dist/chat/ready.d.ts.map +0 -1
  222. package/dist/chat/ready.js +0 -91
  223. package/dist/chat/style.d.ts +0 -4
  224. package/dist/chat/style.d.ts.map +0 -1
  225. package/dist/chat/style.js +0 -302
  226. package/dist/chat.d.ts +0 -14
  227. package/dist/chat.d.ts.map +0 -1
  228. package/dist/chat.js +0 -8
  229. package/dist/read-side.d.ts +0 -25
  230. package/dist/read-side.d.ts.map +0 -1
  231. package/dist/read-side.js +0 -642
  232. package/dist/reads.d.ts +0 -11
  233. package/dist/reads.d.ts.map +0 -1
  234. package/dist/reads.js +0 -98
package/dist/store.js CHANGED
@@ -1,760 +1,375 @@
1
- import { sameBinary, sameChange } from './overlay.js';
2
- import { differing } from './shared.js';
3
- const DATABASE = 'mdruntime';
4
- const AUTHORED = 'mdruntime-authored';
5
- const FENCES = 'fences';
6
- const ROOMS = 'rooms';
7
- const DOCUMENTS = 'documents';
8
- const BINARY_FILES = 'binaryFiles';
9
- const TRANSFERS = 'transfers';
10
- const RECEIVED = 'received';
11
- const RECEIVED_PARTS = 'receivedParts';
12
- const PENDING_CHANGES = 'pendingChanges';
13
- const DECLARATIONS = 'declarations';
14
- const ROOM_DRAFTS = 'roomDrafts';
15
- const PRESENTATION = 'presentation';
16
- const CONVERSATIONS = 'conversations';
17
- const MESSAGES = 'messages';
18
- const CHAT_DRAFTS = 'chatDrafts';
19
- const CALLS = 'calls';
20
- const QUEUES = 'queues';
21
- const FACTS = 'facts';
22
- const CURRENT = 'current';
23
- const MEMBER = 'member';
24
- const TARGETS = 'targets';
25
- const GENERATION = 'generation';
26
- const HOLDER = 'holder';
27
- const BINDING = 'binding';
28
- export const STORE_SCHEMA = 25;
29
- const REPLICA_SHAPE = 23;
30
- export const AUTHORED_SCHEMA = 3;
31
- const AUTHORED_STORES = [
32
- [QUEUES, { keyPath: 'locator' }],
33
- [PENDING_CHANGES, { keyPath: ['locator', 'id'] }],
34
- [ROOM_DRAFTS, { keyPath: ['locator', 'path'] }],
35
- [CHAT_DRAFTS, { keyPath: 'conversation' }],
36
- [CALLS, { keyPath: 'operation' }],
37
- [FACTS, undefined],
38
- [FENCES, { keyPath: 'locator' }],
39
- ];
40
- const REPLICA_STORES = [
41
- [ROOMS, { keyPath: 'locator' }],
42
- [DOCUMENTS, { keyPath: ['locator', 'transfer', 'path'] }],
43
- [BINARY_FILES, { keyPath: ['locator', 'transfer', 'path'] }],
44
- [TRANSFERS, { keyPath: 'locator' }],
45
- [RECEIVED, { keyPath: 'key' }],
46
- [RECEIVED_PARTS, { keyPath: ['key', 'index'] }],
47
- [DECLARATIONS, undefined],
48
- [PRESENTATION, { keyPath: 'app' }],
49
- [CONVERSATIONS, { keyPath: 'conversation' }],
50
- [MESSAGES, { keyPath: ['conversation', 'identity'] }],
51
- ];
52
- const READ_BATCH = 256;
53
- const MESSAGE_INDEXES = [
54
- ['conversation', 'conversation'],
55
- ['position', ['conversation', 'position']],
56
- ['turn', ['conversation', 'turn']],
57
- ];
58
- function settled(request) {
59
- return new Promise((resolve, reject) => {
60
- request.onsuccess = () => resolve(request.result);
61
- request.onerror = () => reject(request.error);
62
- });
63
- }
64
- function completed(transaction) {
65
- return new Promise((resolve, reject) => {
66
- transaction.oncomplete = () => resolve();
67
- transaction.onerror = () => reject(transaction.error);
68
- transaction.onabort = () => reject(transaction.error);
69
- });
70
- }
71
- const EMPTY_DECLARATIONS = {
72
- lastCompleted: null,
73
- providerLimits: [],
74
- capabilities: [],
75
- };
76
- function declaration(database) {
77
- const transaction = database.transaction(DECLARATIONS, 'readonly');
78
- return settled(transaction.objectStore(DECLARATIONS).get(CURRENT)).then((record) => record ?? EMPTY_DECLARATIONS);
79
- }
80
- function updateDeclaration(database, update) {
81
- const transaction = database.transaction(DECLARATIONS, 'readwrite');
82
- const store = transaction.objectStore(DECLARATIONS);
83
- const prior = store.get(CURRENT);
84
- prior.onsuccess = () => {
85
- store.put(update(prior.result ?? EMPTY_DECLARATIONS), CURRENT);
86
- };
87
- return completed(transaction);
88
- }
89
- function amendPending(database, locator, id, amendment) {
90
- const transaction = database.transaction(PENDING_CHANGES, 'readwrite');
91
- const pending = transaction.objectStore(PENDING_CHANGES);
92
- const request = pending.get([locator, id]);
93
- request.onsuccess = () => {
94
- const record = request.result;
95
- if (record !== undefined)
96
- pending.put({ ...record, ...amendment });
97
- };
98
- return completed(transaction);
99
- }
100
- function accepted({ conversation: _held, ...message }) {
101
- return message;
102
- }
103
- function roomRecord(record) {
104
- const { documents, binaries, revisions, fileIds, changes, written, ...room } = record;
105
- return room;
106
- }
107
- function chatDraftRecord(record) {
108
- return { ...record, revision: record.revision ?? '' };
1
+ import { composedOf } from './collection.js';
2
+ import { binaryLocalId } from './identity.js';
3
+ const FILE = '\u0000file';
4
+ function batchFiles(batch) {
5
+ return batch.changes.flatMap((change) => (change.kind === 'binary' ? [change.localId] : []));
109
6
  }
110
- function underTransfer(locator, transfer, after) {
111
- return after === undefined
112
- ? IDBKeyRange.bound([locator, transfer], [locator, transfer, []])
113
- : IDBKeyRange.bound([locator, transfer, after], [locator, transfer, []], true);
114
- }
115
- function underRoom(locator) {
116
- return IDBKeyRange.bound([locator], [locator, []]);
117
- }
118
- async function readTransfer(store, locator, transfer, receive) {
119
- let range = underTransfer(locator, transfer);
120
- for (;;) {
121
- const batch = await settled(store.getAll(range, READ_BATCH));
122
- for (const record of batch)
123
- receive(record);
124
- if (batch.length < READ_BATCH)
125
- return;
126
- range = underTransfer(locator, transfer, batch[batch.length - 1].path);
127
- }
7
+ function draftFiles(drafts) {
8
+ return drafts.flatMap(({ parts }) => (parts.file === undefined ? [] : [parts.file.localId]));
128
9
  }
129
- function emptyLayer() {
130
- return { documents: {}, binaries: {}, revisions: {}, fileIds: {}, changes: {}, written: {} };
10
+ function roomDraftRow(record) {
11
+ return { path: record.path, record: JSON.stringify(record), files: draftFiles([record]) };
131
12
  }
132
- async function readLayer(transaction, locator, transfer) {
133
- const layer = emptyLayer();
134
- const head = (held) => {
135
- if (held.revision !== null)
136
- layer.revisions[held.path] = held.revision;
137
- if (held.fileId !== null)
138
- layer.fileIds[held.path] = held.fileId;
139
- if (held.change !== null)
140
- layer.changes[held.path] = held.change;
141
- };
142
- await readTransfer(transaction.objectStore(DOCUMENTS), locator, transfer, (document) => {
143
- layer.documents[document.path] = document.content;
144
- if (document.written !== null)
145
- layer.written[document.path] = document.written;
146
- head(document);
147
- });
148
- await readTransfer(transaction.objectStore(BINARY_FILES), locator, transfer, (file) => {
149
- layer.binaries[file.path] = file.binary;
150
- head(file);
151
- });
152
- return layer;
153
- }
154
- function documentRecord(locator, transfer, layer, path) {
13
+ function authoredRows({ locator, queue, founded, saved, roomDrafts, consumedRoomDrafts }, held) {
155
14
  return {
156
15
  locator,
157
- transfer,
158
- path,
159
- content: layer.documents[path],
160
- revision: layer.revisions[path] ?? null,
161
- fileId: layer.fileIds[path] ?? null,
162
- change: layer.changes[path] ?? null,
163
- written: layer.written[path] ?? null,
164
- };
165
- }
166
- function binaryFileRecord(locator, transfer, layer, path) {
167
- return {
168
- locator,
169
- transfer,
170
- path,
171
- revision: layer.revisions[path] ?? null,
172
- fileId: layer.fileIds[path] ?? null,
173
- change: layer.changes[path] ?? null,
174
- binary: layer.binaries[path],
16
+ ...(queue === undefined
17
+ ? {}
18
+ : {
19
+ order: queue.map(({ id }) => id),
20
+ added: queue
21
+ .filter(({ id }) => held?.has(id) !== true)
22
+ .map((batch) => {
23
+ const drafts = saved?.get(batch.id) ?? [];
24
+ return {
25
+ id: batch.id,
26
+ batch: JSON.stringify(batch),
27
+ state: JSON.stringify({
28
+ drafts,
29
+ submission: { state: 'waiting' },
30
+ lost: false,
31
+ }),
32
+ files: [...batchFiles(batch), ...draftFiles(drafts)],
33
+ };
34
+ }),
35
+ }),
36
+ ...(founded === undefined
37
+ ? {}
38
+ : {
39
+ founded: founded.map((batch) => ({
40
+ id: batch.id,
41
+ batch: JSON.stringify(batch),
42
+ files: batchFiles(batch),
43
+ })),
44
+ }),
45
+ ...(roomDrafts === undefined ? {} : { roomDrafts: roomDrafts.map(roomDraftRow) }),
46
+ ...(consumedRoomDrafts === undefined ? {} : { consumedRoomDrafts }),
175
47
  };
176
48
  }
177
- function writeLayer(transaction, locator, transfer, next, base) {
178
- const documents = transaction.objectStore(DOCUMENTS);
179
- const files = transaction.objectStore(BINARY_FILES);
180
- const paths = base === null
181
- ? new Set([...Object.keys(next.documents), ...Object.keys(next.binaries)])
182
- : new Set([
183
- ...differing(base.documents, next.documents),
184
- ...differing(base.revisions, next.revisions),
185
- ...differing(base.fileIds, next.fileIds),
186
- ...differing(base.written, next.written),
187
- ...differing(base.changes, next.changes, sameChange),
188
- ...differing(base.binaries, next.binaries, sameBinary),
189
- ]);
190
- for (const path of paths) {
191
- if (next.documents[path] !== undefined) {
192
- documents.put(documentRecord(locator, transfer, next, path));
193
- }
194
- else if (base?.documents[path] !== undefined) {
195
- documents.delete([locator, transfer, path]);
196
- }
197
- if (next.binaries[path] !== undefined) {
198
- files.put(binaryFileRecord(locator, transfer, next, path));
199
- }
200
- else if (base?.binaries[path] !== undefined) {
201
- files.delete([locator, transfer, path]);
202
- }
203
- }
49
+ function marked(value) {
50
+ return typeof value === 'object' && value !== null && FILE in value;
204
51
  }
205
- function removeRoomEntries(store, locator) {
206
- const keys = store.getAllKeys();
207
- keys.onsuccess = () => {
208
- for (const key of keys.result) {
209
- if (Array.isArray(key) && key[0] === locator)
210
- store.delete(key);
52
+ function keyed(keys, value) {
53
+ const stored = [];
54
+ const files = [];
55
+ const record = JSON.stringify(value, (_name, held) => {
56
+ if (!(held instanceof Blob))
57
+ return held;
58
+ let key = keys.get(held);
59
+ if (key === undefined) {
60
+ key = binaryLocalId();
61
+ keys.set(held, key);
211
62
  }
212
- };
213
- }
214
- function removeDatabase(name) {
215
- return new Promise((resolve, reject) => {
216
- const removal = indexedDB.deleteDatabase(name);
217
- removal.onsuccess = () => resolve();
218
- removal.onerror = () => reject(removal.error);
63
+ stored.push({ key, blob: held });
64
+ files.push(key);
65
+ return { [FILE]: key, type: held.type };
219
66
  });
67
+ return { record, files, stored };
220
68
  }
221
- export async function authoredHolder() {
222
- const authored = await openAuthored();
223
- try {
224
- const transaction = authored.transaction(FACTS, 'readonly');
225
- const held = transaction.objectStore(FACTS).get(HOLDER);
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 authoredBy(holder) {
234
- const authored = await openAuthored();
235
- try {
236
- const transaction = authored.transaction(FACTS, 'readwrite');
237
- const facts = transaction.objectStore(FACTS);
238
- const held = facts.get(HOLDER);
239
- let owned = true;
240
- held.onsuccess = () => {
241
- if (held.result === undefined)
242
- facts.put(holder, HOLDER);
243
- else
244
- owned = held.result === holder;
245
- };
246
- await completed(transaction);
247
- return owned;
248
- }
249
- finally {
250
- authored.close();
251
- }
252
- }
253
- export async function authoredBinding() {
254
- const authored = await openAuthored();
255
- try {
256
- const transaction = authored.transaction(FACTS, 'readonly');
257
- const held = transaction.objectStore(FACTS).get(BINDING);
258
- await completed(transaction);
259
- return typeof held.result === 'string' ? held.result : null;
260
- }
261
- finally {
262
- authored.close();
263
- }
69
+ async function filed(value, read) {
70
+ if (Array.isArray(value))
71
+ return Promise.all(value.map((item) => filed(item, read)));
72
+ if (marked(value))
73
+ return read(value[FILE], value.type);
74
+ if (typeof value !== 'object' || value === null)
75
+ return value;
76
+ const restored = {};
77
+ for (const [name, item] of Object.entries(value))
78
+ restored[name] = await filed(item, read);
79
+ return restored;
264
80
  }
265
- export async function bindAuthored(binding) {
266
- const authored = await openAuthored();
267
- try {
268
- const transaction = authored.transaction(FACTS, 'readwrite');
269
- transaction.objectStore(FACTS).put(binding, BINDING);
270
- await completed(transaction);
271
- }
272
- finally {
273
- authored.close();
274
- }
81
+ function json(value, fallback) {
82
+ return value === undefined ? fallback : JSON.parse(value);
275
83
  }
276
- export async function removeRuntimeStore(holder) {
277
- await removeDatabase(DATABASE);
278
- if (holder !== undefined && (await authoredBy(holder)))
279
- return true;
280
- await removeDatabase(AUTHORED);
281
- return false;
282
- }
283
- export function openDatabase(name, version, upgrade) {
284
- return new Promise((resolve, reject) => {
285
- const opening = indexedDB.open(name, version);
286
- opening.onupgradeneeded = (event) => upgrade(opening, event.oldVersion);
287
- opening.onsuccess = () => resolve(opening.result);
288
- opening.onerror = () => {
289
- if (opening.error?.name !== 'VersionError') {
290
- reject(opening.error);
291
- return;
292
- }
293
- removeDatabase(name)
294
- .then(() => openDatabase(name, version, upgrade))
295
- .then(resolve, reject);
296
- };
297
- });
298
- }
299
- function openReplica() {
300
- return openDatabase(DATABASE, STORE_SCHEMA, (opening, oldVersion) => {
301
- const database = opening.result;
302
- if (oldVersion < REPLICA_SHAPE) {
303
- for (const name of [...database.objectStoreNames])
304
- database.deleteObjectStore(name);
305
- }
306
- for (const [name, parameters] of REPLICA_STORES) {
307
- if (database.objectStoreNames.contains(name))
308
- continue;
309
- database.createObjectStore(name, parameters);
310
- }
311
- const messages = opening.transaction.objectStore(MESSAGES);
312
- for (const [name, keyPath] of MESSAGE_INDEXES) {
313
- if (!messages.indexNames.contains(name))
314
- messages.createIndex(name, keyPath);
315
- }
316
- });
317
- }
318
- function openAuthored() {
319
- return openDatabase(AUTHORED, AUTHORED_SCHEMA, (opening) => {
320
- const database = opening.result;
321
- for (const [name, parameters] of AUTHORED_STORES) {
322
- if (!database.objectStoreNames.contains(name))
323
- database.createObjectStore(name, parameters);
84
+ export function openRuntimeStore(fenced, authored) {
85
+ const keys = new WeakMap();
86
+ const read = async (key, type) => {
87
+ const blob = await authored.ask({ kind: 'authored.file', key, type });
88
+ keys.set(blob, key);
89
+ return blob;
90
+ };
91
+ const restored = async (records) => Promise.all(records.map(async (record) => (await filed(JSON.parse(record), read))));
92
+ const amend = async (locator, id, amendment) => {
93
+ if (fenced(locator))
94
+ return;
95
+ await authored.ask({
96
+ kind: 'authored.amend',
97
+ locator,
98
+ id,
99
+ amendment: JSON.stringify(amendment),
100
+ });
101
+ };
102
+ const fact = (name) => authored.ask({ kind: 'authored.fact', name });
103
+ const queued = new Map();
104
+ const changed = new Map();
105
+ const change = (key) => changed.set(key, (changed.get(key) ?? 0) + 1);
106
+ const rows = (writes) => writes.map((write) => authoredRows(write, queued.get(write.locator)));
107
+ const noted = (writes) => {
108
+ for (const { locator, queue } of writes) {
109
+ if (queue !== undefined)
110
+ queued.set(locator, new Set(queue.map(({ id }) => id)));
324
111
  }
325
- });
326
- }
327
- export async function openRuntimeStore(fenced) {
328
- const authored = await openAuthored();
329
- const database = await openReplica().catch((error) => {
330
- authored.close();
331
- throw error;
332
- });
112
+ };
333
113
  return {
334
- async all() {
335
- const transaction = database.transaction([ROOMS, DOCUMENTS, BINARY_FILES], 'readonly');
336
- const rooms = await settled(transaction.objectStore(ROOMS).getAll());
337
- const records = [];
338
- for (const room of rooms.filter((held) => !fenced(held.locator))) {
339
- records.push({ ...room, ...(await readLayer(transaction, room.locator, room.transfer)) });
114
+ async opened() {
115
+ const held = await authored.ask({ kind: 'replica.open' });
116
+ const unindexed = new Set(held.keys.flatMap(({ key, rederiving, searching }) => rederiving === null && searching === null ? [] : [key]));
117
+ const overlaid = new Map();
118
+ for (const { key, path } of held.overlaid) {
119
+ const paths = overlaid.get(key) ?? new Set();
120
+ paths.add(path);
121
+ overlaid.set(key, paths);
340
122
  }
341
- return records;
342
- },
343
- async keepAuthored({ locator, queue, founded, saved, roomDrafts, consumedRoomDrafts }) {
344
- if (fenced(locator))
345
- return;
346
- const transaction = authored.transaction([QUEUES, PENDING_CHANGES, ROOM_DRAFTS], 'readwrite');
347
- const order = transaction.objectStore(QUEUES);
348
- const pending = transaction.objectStore(PENDING_CHANGES);
349
- const held = order.get(locator);
350
- held.onsuccess = () => {
351
- const before = new Set(held.result?.order ?? []);
352
- const after = new Set(queue.map((batch) => batch.id));
353
- if (queue.length === 0)
354
- order.delete(locator);
355
- else
356
- order.put({ locator, order: [...after] });
357
- for (const id of before) {
358
- if (!after.has(id))
359
- pending.delete([locator, id]);
360
- }
361
- for (const batch of queue) {
362
- if (!before.has(batch.id)) {
363
- pending.put({
364
- locator,
365
- id: batch.id,
366
- batch,
367
- drafts: saved?.get(batch.id) ?? [],
368
- submission: { state: 'waiting' },
369
- lost: false,
370
- });
371
- }
372
- }
373
- for (const batch of founded ?? []) {
374
- if (!before.has(batch.id))
375
- continue;
376
- const stored = pending.get([locator, batch.id]);
377
- stored.onsuccess = () => {
378
- const record = stored.result;
379
- if (record !== undefined)
380
- pending.put({ ...record, batch });
381
- };
382
- }
123
+ return {
124
+ rooms: new Map(held.rooms
125
+ .filter(({ locator }) => !fenced(locator))
126
+ .map(({ locator, key, record }) => [
127
+ locator,
128
+ { ...JSON.parse(record), key },
129
+ ])),
130
+ unindexed,
131
+ overlaid,
132
+ erasing: held.erasing.length > 0,
133
+ orphans: held.orphans,
134
+ lastCompleted: json(held.facts.lastCompleted, null),
135
+ providerLimits: json(held.facts.providerLimits, []),
136
+ capabilities: json(held.facts.capabilities, []),
137
+ member: json(held.facts.member, null),
138
+ targets: json(held.facts.targets, {}),
139
+ presentation: held.presentation.map(({ app, value }) => ({
140
+ app,
141
+ value: JSON.parse(value),
142
+ })),
143
+ conversationList: json(held.facts.conversationList, null),
144
+ conversationCursor: json(held.facts.conversationCursor, null),
383
145
  };
384
- const draftStore = transaction.objectStore(ROOM_DRAFTS);
385
- for (const path of consumedRoomDrafts ?? [])
386
- draftStore.delete([locator, path]);
387
- for (const record of roomDrafts ?? [])
388
- draftStore.put(record);
389
- await completed(transaction);
390
- },
391
- async write({ next, previous, stored }) {
392
- if (next === previous || fenced(next.locator))
393
- return;
394
- const transaction = database.transaction([ROOMS, DOCUMENTS, BINARY_FILES, TRANSFERS], 'readwrite');
395
- transaction.objectStore(ROOMS).put(roomRecord(next));
396
- const moved = previous?.transfer !== next.transfer;
397
- if (moved && previous !== null) {
398
- const range = underTransfer(previous.locator, previous.transfer);
399
- transaction.objectStore(DOCUMENTS).delete(range);
400
- transaction.objectStore(BINARY_FILES).delete(range);
401
- }
402
- if (moved)
403
- transaction.objectStore(TRANSFERS).delete(next.locator);
404
- writeLayer(transaction, next.locator, next.transfer, next, moved ? (stored ?? null) : previous);
405
- await completed(transaction);
406
- },
407
- async revokeModels(records) {
408
- const transaction = database.transaction(ROOMS, 'readwrite');
409
- const rooms = transaction.objectStore(ROOMS);
410
- const revoked = { status: 'unavailable', reason: 'revoked' };
411
- for (const record of records) {
412
- if (fenced(record.locator))
413
- continue;
414
- rooms.put(roomRecord({ ...record, model: revoked, selectionModel: revoked }));
415
- }
416
- await completed(transaction);
417
146
  },
418
- async lastCompleted() {
419
- return (await declaration(database)).lastCompleted;
147
+ counts(rooms) {
148
+ return authored.ask({ kind: 'replica.counts', rooms });
420
149
  },
421
- complete(at) {
422
- return updateDeclaration(database, (record) => ({ ...record, lastCompleted: at }));
150
+ query(plan, rooms, budget) {
151
+ return authored.ask({ kind: 'replica.query', plan, rooms, budget });
423
152
  },
424
- async providerLimits() {
425
- return (await declaration(database)).providerLimits;
153
+ changes(keys) {
154
+ return keys.reduce((total, key) => total + (changed.get(key) ?? 0), 0);
426
155
  },
427
- async capabilities() {
428
- return (await declaration(database)).capabilities;
156
+ entries(key, view, paths) {
157
+ return authored.ask({ kind: 'replica.entries', key, view, paths });
429
158
  },
430
- declare(limits, capabilities) {
431
- return updateDeclaration(database, (record) => ({
432
- ...record,
433
- providerLimits: limits,
434
- capabilities,
435
- }));
159
+ typed(key, view, paths) {
160
+ return authored.ask({ kind: 'replica.typed', key, view, paths });
436
161
  },
437
- async member() {
438
- const transaction = database.transaction(DECLARATIONS, 'readonly');
439
- const held = await settled(transaction.objectStore(DECLARATIONS).get(MEMBER));
440
- return held ?? null;
162
+ referrers(key, view, targets, kinds) {
163
+ return authored.ask({ kind: 'replica.referrers', key, view, targets, kinds });
441
164
  },
442
- async keepMember(member) {
443
- const transaction = database.transaction(DECLARATIONS, 'readwrite');
444
- transaction.objectStore(DECLARATIONS).put(member, MEMBER);
445
- await completed(transaction);
165
+ files(key, view, after, limit) {
166
+ return authored.ask({ kind: 'replica.files', key, view, after, limit });
446
167
  },
447
- async targets() {
448
- const transaction = database.transaction(DECLARATIONS, 'readonly');
449
- const held = await settled(transaction.objectStore(DECLARATIONS).get(TARGETS));
450
- return held ?? {};
168
+ folders(key, view, paths) {
169
+ return authored.ask({ kind: 'replica.folders', key, view, paths });
170
+ },
171
+ typedWithin(key, view, roots, within) {
172
+ return authored.ask({ kind: 'replica.typedWithin', key, view, roots, within });
173
+ },
174
+ search(ask) {
175
+ return authored.ask({ kind: 'replica.search', ...ask });
176
+ },
177
+ async stage(record, composition, page) {
178
+ if (fenced(record.locator))
179
+ return null;
180
+ return authored.ask({
181
+ kind: 'replica.stage',
182
+ locator: record.locator,
183
+ transfer: record.transfer,
184
+ key: record.key,
185
+ fingerprint: composedOf(composition).fingerprint,
186
+ entries: page.entries,
187
+ folders: page.folders,
188
+ });
189
+ },
190
+ async refresh(key, composition, page) {
191
+ await authored.ask({
192
+ kind: 'replica.refresh',
193
+ key,
194
+ composed: composedOf(composition),
195
+ entries: page.entries,
196
+ removals: page.removals,
197
+ folders: page.folders,
198
+ folderRemovals: page.folderRemovals,
199
+ });
451
200
  },
452
- async keepTargets(targets) {
453
- const transaction = database.transaction(DECLARATIONS, 'readwrite');
454
- transaction.objectStore(DECLARATIONS).put(targets, TARGETS);
455
- await completed(transaction);
201
+ async unrefresh(key) {
202
+ await authored.ask({ kind: 'replica.unrefresh', key });
203
+ },
204
+ async commit({ rooms, writes = [], files = [], whole = false, discards = [] }) {
205
+ const kept = whole ? writes : writes.filter(({ locator }) => !fenced(locator));
206
+ const refused = whole ? kept.find(({ locator }) => fenced(locator)) : undefined;
207
+ if (refused !== undefined) {
208
+ return { fenced: refused.locator, counts: {}, changed: {}, indexed: {} };
209
+ }
210
+ for (const room of rooms)
211
+ change(room.key);
212
+ const committed = await authored.ask({
213
+ kind: 'replica.commit',
214
+ ...(kept.length === 0 && files.length === 0 && discards.length === 0
215
+ ? {}
216
+ : { authored: { writes: rows(kept), files, whole, discards } }),
217
+ rooms: rooms.filter(({ locator }) => !fenced(locator)),
218
+ });
219
+ if (committed.fenced === null || !whole)
220
+ noted(kept);
221
+ for (const locator of discards)
222
+ queued.delete(locator);
223
+ return committed;
224
+ },
225
+ async fence(locators) {
226
+ await authored.ask({ kind: 'replica.fence', locators });
227
+ for (const locator of locators)
228
+ queued.delete(locator);
229
+ },
230
+ async erase(locator, limit) {
231
+ return (await authored.ask({ kind: 'replica.erase', locator, limit })).remaining;
232
+ },
233
+ async abandon(locator) {
234
+ if (fenced(locator))
235
+ return;
236
+ await authored.ask({ kind: 'replica.abandon', locator });
237
+ },
238
+ async release(key) {
239
+ await authored.ask({ kind: 'replica.release', key });
240
+ },
241
+ async rederive(key, composition, bytes, ms, floor) {
242
+ change(key);
243
+ return authored.ask({
244
+ kind: 'replica.rederive',
245
+ key,
246
+ composed: composedOf(composition),
247
+ bytes,
248
+ ms,
249
+ floor,
250
+ });
251
+ },
252
+ async fact(name, value) {
253
+ await authored.ask({ kind: 'replica.fact', name, value: JSON.stringify(value) });
456
254
  },
457
255
  async queues() {
458
- const transaction = authored.transaction([QUEUES, PENDING_CHANGES], 'readonly');
459
- const [queues, pending] = await Promise.all([
460
- settled(transaction.objectStore(QUEUES).getAll()),
461
- settled(transaction.objectStore(PENDING_CHANGES).getAll()),
462
- ]);
256
+ const { queues, pending } = await authored.ask({ kind: 'authored.queues' });
257
+ queued.clear();
258
+ for (const queue of queues)
259
+ queued.set(queue.locator, new Set(queue.ids));
463
260
  const byId = new Map(pending.map((record) => [`${record.locator}\0${record.id}`, record]));
464
261
  return queues
465
262
  .filter((queue) => !fenced(queue.locator))
466
263
  .map((queue) => ({
467
264
  locator: queue.locator,
468
- pending: queue.order.map((id) => {
265
+ pending: queue.ids.map((id) => {
469
266
  const record = byId.get(`${queue.locator}\0${id}`);
470
267
  if (record === undefined)
471
268
  throw new Error(`Missing Pending Change ${queue.locator}/${id}`);
472
- const { batch, drafts, submission, lost, upload, accepted } = record;
473
- return { batch, state: { drafts, submission, lost, upload, accepted } };
269
+ return {
270
+ batch: JSON.parse(record.batch),
271
+ state: JSON.parse(record.state),
272
+ };
474
273
  }),
475
274
  }));
476
275
  },
477
- async submit(locator, id, submission, lost) {
478
- if (!fenced(locator))
479
- await amendPending(authored, locator, id, { submission, lost });
480
- },
481
- async accept(locator, id, roomVersion) {
482
- if (!fenced(locator))
483
- await amendPending(authored, locator, id, { accepted: roomVersion });
276
+ submit(locator, id, submission, lost) {
277
+ return amend(locator, id, { submission, lost });
484
278
  },
485
- async keepUpload(locator, id, upload) {
486
- if (!fenced(locator))
487
- await amendPending(authored, locator, id, { upload });
279
+ keepUpload(locator, id, upload) {
280
+ return amend(locator, id, { upload });
488
281
  },
489
282
  async roomDrafts() {
490
- const transaction = authored.transaction(ROOM_DRAFTS, 'readonly');
491
- const held = await settled(transaction.objectStore(ROOM_DRAFTS).getAll());
492
- return held.filter((record) => !fenced(record.locator));
493
- },
494
- async presentation() {
495
- const transaction = database.transaction(PRESENTATION, 'readonly');
496
- return settled(transaction.objectStore(PRESENTATION).getAll());
283
+ const held = await authored.ask({ kind: 'authored.roomDrafts' });
284
+ return held
285
+ .map((record) => JSON.parse(record))
286
+ .filter((record) => !fenced(record.locator));
497
287
  },
498
288
  async present(record) {
499
- const transaction = database.transaction(PRESENTATION, 'readwrite');
500
- transaction.objectStore(PRESENTATION).put(record);
501
- await completed(transaction);
502
- },
503
- async conversations() {
504
- const transaction = database.transaction([CONVERSATIONS, MESSAGES], 'readonly');
505
- const records = await settled(transaction.objectStore(CONVERSATIONS).getAll());
506
- const held = new Map(records.map(({ conversation, read, partial }) => [
507
- conversation,
508
- { conversation, read, partial: partial === true },
509
- ]));
510
- const keys = transaction.objectStore(MESSAGES).index('conversation');
511
- await new Promise((resolve, reject) => {
512
- const cursor = keys.openKeyCursor(null, 'nextunique');
513
- cursor.onerror = () => reject(cursor.error);
514
- cursor.onsuccess = () => {
515
- const at = cursor.result;
516
- if (at === null) {
517
- resolve();
518
- return;
519
- }
520
- const conversation = at.key;
521
- if (!held.has(conversation)) {
522
- held.set(conversation, { conversation, read: undefined, partial: true });
523
- }
524
- at.continue();
525
- };
289
+ await authored.ask({
290
+ kind: 'replica.present',
291
+ app: record.app,
292
+ value: JSON.stringify(record.value ?? null),
526
293
  });
527
- return [...held.values()];
528
294
  },
529
- async keepMessages({ conversation, messages, read }) {
530
- const transaction = database.transaction([CONVERSATIONS, MESSAGES], 'readwrite');
531
- const stored = transaction.objectStore(MESSAGES);
532
- for (const message of messages) {
533
- stored.put({ ...message, conversation });
534
- }
535
- if (read !== undefined) {
536
- transaction.objectStore(CONVERSATIONS).put({
537
- conversation,
538
- read: read.at,
539
- ...(read.partial ? { partial: true } : {}),
540
- });
541
- }
542
- await completed(transaction);
543
- },
544
- async messageWindow(conversation, limit) {
545
- const transaction = database.transaction(MESSAGES, 'readonly');
546
- const index = transaction.objectStore(MESSAGES).index('position');
547
- const range = IDBKeyRange.bound([conversation], [conversation, []]);
548
- if (limit === undefined) {
549
- return (await settled(index.getAll(range))).map(accepted);
550
- }
551
- const latest = [];
552
- if (limit === 0)
553
- return latest;
554
- await new Promise((resolve, reject) => {
555
- const cursor = index.openCursor(range, 'prev');
556
- cursor.onerror = () => reject(cursor.error);
557
- cursor.onsuccess = () => {
558
- const at = cursor.result;
559
- if (at === null) {
560
- resolve();
561
- return;
562
- }
563
- latest.push(accepted(at.value));
564
- if (latest.length === limit)
565
- resolve();
566
- else
567
- at.continue();
568
- };
569
- });
570
- return latest.reverse();
571
- },
572
- async messagesAfter(conversation, identity) {
573
- const transaction = database.transaction(MESSAGES, 'readonly');
574
- const range = identity === null
575
- ? IDBKeyRange.bound([conversation], [conversation, []])
576
- : IDBKeyRange.bound([conversation, identity], [conversation, []], true);
577
- const held = await settled(transaction.objectStore(MESSAGES).getAll(range, READ_BATCH));
578
- return held.map(accepted);
579
- },
580
- async messageIdentities(conversation) {
581
- const transaction = database.transaction(MESSAGES, 'readonly');
582
- const keys = await settled(transaction.objectStore(MESSAGES).index('conversation').getAllKeys(conversation));
583
- return keys.map((key) => key[1]);
584
- },
585
- async turnArrived(conversation, turn) {
586
- const transaction = database.transaction(MESSAGES, 'readonly');
587
- const held = await settled(transaction.objectStore(MESSAGES).index('turn').count([conversation, turn]));
588
- return held > 0;
295
+ conversations() {
296
+ return authored.ask({ kind: 'conversation.held' });
297
+ },
298
+ async keepMessages(write) {
299
+ await authored.ask({ kind: 'conversation.keep', ...write });
300
+ },
301
+ messageWindow(conversation, limit, before) {
302
+ return authored.ask({ kind: 'conversation.window', conversation, limit, before });
303
+ },
304
+ messageCount(conversation) {
305
+ return authored.ask({ kind: 'conversation.count', conversation });
306
+ },
307
+ turnArrived(conversation, turn) {
308
+ return authored.ask({ kind: 'conversation.turn', conversation, turn });
589
309
  },
590
310
  async dropConversations(conversations) {
591
- const drafts = authored.transaction(CHAT_DRAFTS, 'readwrite');
592
- for (const conversation of conversations)
593
- drafts.objectStore(CHAT_DRAFTS).delete(conversation);
594
- await completed(drafts);
595
- const transaction = database.transaction([CONVERSATIONS, MESSAGES], 'readwrite');
596
- const messages = transaction.objectStore(MESSAGES);
597
- for (const conversation of conversations) {
598
- transaction.objectStore(CONVERSATIONS).delete(conversation);
599
- const keys = messages.index('conversation').getAllKeys(conversation);
600
- keys.onsuccess = () => {
601
- for (const key of keys.result)
602
- messages.delete(key);
603
- };
604
- }
605
- await completed(transaction);
311
+ await authored.ask({ kind: 'conversation.drop', conversations });
606
312
  },
607
313
  async chatDrafts() {
608
- const transaction = authored.transaction(CHAT_DRAFTS, 'readonly');
609
- const held = await settled(transaction.objectStore(CHAT_DRAFTS).getAll());
610
- return held.map(chatDraftRecord);
314
+ const records = await authored.ask({ kind: 'authored.chatDrafts' });
315
+ return records.map((record) => JSON.parse(record));
611
316
  },
612
317
  async keepChatDrafts(records) {
613
- const transaction = authored.transaction(CHAT_DRAFTS, 'readwrite');
614
- for (const record of records)
615
- transaction.objectStore(CHAT_DRAFTS).put(record);
616
- await completed(transaction);
318
+ await authored.ask({
319
+ kind: 'authored.chatDraft',
320
+ kept: records.map((record) => ({
321
+ conversation: record.conversation,
322
+ record: JSON.stringify(record),
323
+ })),
324
+ dropped: [],
325
+ });
617
326
  },
618
327
  async dropChatDraft(conversation) {
619
- const transaction = authored.transaction(CHAT_DRAFTS, 'readwrite');
620
- transaction.objectStore(CHAT_DRAFTS).delete(conversation);
621
- await completed(transaction);
328
+ await authored.ask({ kind: 'authored.chatDraft', kept: [], dropped: [conversation] });
622
329
  },
623
330
  async calls() {
624
- const transaction = authored.transaction(CALLS, 'readonly');
625
- return settled(transaction.objectStore(CALLS).getAll());
331
+ return restored(await authored.ask({ kind: 'authored.calls' }));
626
332
  },
627
333
  async keepCall(record) {
628
- const transaction = authored.transaction(CALLS, 'readwrite');
629
- transaction.objectStore(CALLS).put(record);
630
- await completed(transaction);
334
+ const { stored, ...kept } = keyed(keys, record);
335
+ await authored.ask({
336
+ kind: 'authored.call',
337
+ operation: record.operation,
338
+ kept,
339
+ files: stored,
340
+ });
631
341
  },
632
342
  async dropCall(operation) {
633
- const transaction = authored.transaction(CALLS, 'readwrite');
634
- transaction.objectStore(CALLS).delete(operation);
635
- await completed(transaction);
343
+ await authored.ask({ kind: 'authored.call', operation, kept: null, files: [] });
636
344
  },
637
- async keepRoomDraft(record) {
345
+ async keepRoomDraft(record, files) {
638
346
  if (fenced(record.locator))
639
347
  return;
640
- const transaction = authored.transaction(ROOM_DRAFTS, 'readwrite');
641
- transaction.objectStore(ROOM_DRAFTS).put(record);
642
- await completed(transaction);
348
+ const { path, ...kept } = roomDraftRow(record);
349
+ await authored.ask({
350
+ kind: 'authored.roomDraft',
351
+ locator: record.locator,
352
+ path,
353
+ kept,
354
+ files,
355
+ });
643
356
  },
644
357
  async discardRoomDraft(locator, path) {
645
358
  if (fenced(locator))
646
359
  return;
647
- const transaction = authored.transaction(ROOM_DRAFTS, 'readwrite');
648
- transaction.objectStore(ROOM_DRAFTS).delete([locator, path]);
649
- await completed(transaction);
650
- },
651
- async fences() {
652
- const transaction = authored.transaction(FENCES, 'readonly');
653
- const held = await settled(transaction.objectStore(FENCES).getAll());
654
- return held.map((fence) => fence.locator);
655
- },
656
- async erase(locators) {
657
- const work = authored.transaction([FENCES, QUEUES, PENDING_CHANGES, ROOM_DRAFTS], 'readwrite');
658
- for (const locator of locators) {
659
- work.objectStore(FENCES).put({ locator });
660
- work.objectStore(QUEUES).delete(locator);
661
- for (const name of [PENDING_CHANGES, ROOM_DRAFTS]) {
662
- removeRoomEntries(work.objectStore(name), locator);
663
- }
664
- }
665
- await completed(work);
666
- const transaction = database.transaction([ROOMS, DOCUMENTS, BINARY_FILES, TRANSFERS], 'readwrite');
667
- for (const locator of locators) {
668
- transaction.objectStore(ROOMS).delete(locator);
669
- transaction.objectStore(TRANSFERS).delete(locator);
670
- transaction.objectStore(DOCUMENTS).delete(underRoom(locator));
671
- transaction.objectStore(BINARY_FILES).delete(underRoom(locator));
672
- }
673
- await completed(transaction);
674
- const fences = authored.transaction(FENCES, 'readwrite');
675
- for (const locator of locators)
676
- fences.objectStore(FENCES).delete(locator);
677
- await completed(fences);
678
- },
679
- async discard(locator) {
680
- const transaction = authored.transaction([QUEUES, PENDING_CHANGES], 'readwrite');
681
- transaction.objectStore(QUEUES).delete(locator);
682
- removeRoomEntries(transaction.objectStore(PENDING_CHANGES), locator);
683
- await completed(transaction);
684
- },
685
- async transfers() {
686
- const transaction = database.transaction(TRANSFERS, 'readonly');
687
- const held = await settled(transaction.objectStore(TRANSFERS).getAll());
688
- return held.filter((record) => !fenced(record.locator));
360
+ await authored.ask({ kind: 'authored.roomDraft', locator, path, kept: null, files: [] });
689
361
  },
690
- staged(record) {
691
- const transaction = database.transaction([DOCUMENTS, BINARY_FILES], 'readonly');
692
- return readLayer(transaction, record.locator, record.transfer);
693
- },
694
- async stage(record, page) {
695
- if (fenced(record.locator))
696
- return;
697
- const transaction = database.transaction([TRANSFERS, DOCUMENTS, BINARY_FILES], 'readwrite');
698
- writeLayer(transaction, record.locator, record.transfer, page, null);
699
- transaction.objectStore(TRANSFERS).put(record);
700
- await completed(transaction);
701
- },
702
- async abandon(locator) {
703
- if (fenced(locator))
704
- return;
705
- const transaction = database.transaction([TRANSFERS, DOCUMENTS, BINARY_FILES], 'readwrite');
706
- const transfers = transaction.objectStore(TRANSFERS);
707
- const held = transfers.get(locator);
708
- held.onsuccess = () => {
709
- const record = held.result;
710
- if (record === undefined)
711
- return;
712
- transfers.delete(locator);
713
- const range = underTransfer(locator, record.transfer);
714
- transaction.objectStore(DOCUMENTS).delete(range);
715
- transaction.objectStore(BINARY_FILES).delete(range);
716
- };
717
- await completed(transaction);
718
- },
719
- async receivedHeads() {
720
- const transaction = database.transaction(RECEIVED, 'readonly');
721
- return settled(transaction.objectStore(RECEIVED).getAll());
722
- },
723
- async receivedParts(key, count) {
724
- const transaction = database.transaction(RECEIVED_PARTS, 'readonly');
725
- const kept = await settled(transaction
726
- .objectStore(RECEIVED_PARTS)
727
- .getAll(IDBKeyRange.bound([key, 0], [key, count], false, true)));
728
- return kept.map((part) => part.value);
729
- },
730
- async keepReceived(key, head, part) {
731
- const transaction = database.transaction([RECEIVED, RECEIVED_PARTS], 'readwrite');
732
- transaction.objectStore(RECEIVED).put({ key, head });
733
- if (part !== undefined)
734
- transaction.objectStore(RECEIVED_PARTS).put({ key, ...part });
735
- await completed(transaction);
736
- },
737
- async dropReceived(keys) {
738
- const transaction = database.transaction([RECEIVED, RECEIVED_PARTS], 'readwrite');
739
- for (const key of keys) {
740
- transaction.objectStore(RECEIVED).delete(key);
741
- transaction.objectStore(RECEIVED_PARTS).delete(IDBKeyRange.bound([key], [key, []]));
742
- }
743
- await completed(transaction);
362
+ fences() {
363
+ return authored.ask({ kind: 'authored.fences' });
744
364
  },
745
- async generation() {
746
- const transaction = authored.transaction(FACTS, 'readonly');
747
- const held = await settled(transaction.objectStore(FACTS).get(GENERATION));
748
- return held ?? null;
365
+ generation() {
366
+ return fact('generation');
749
367
  },
750
368
  async rememberGeneration(value) {
751
- const transaction = authored.transaction(FACTS, 'readwrite');
752
- transaction.objectStore(FACTS).put(value, GENERATION);
753
- await completed(transaction);
369
+ await authored.ask({ kind: 'authored.remember', name: 'generation', value });
754
370
  },
755
- close() {
756
- authored.close();
757
- database.close();
371
+ file(key, type) {
372
+ return authored.ask({ kind: 'authored.file', key, type });
758
373
  },
759
374
  };
760
375
  }