@agentshouse/mdruntime 0.1.0-ca7c26c6bc27e → 0.1.0-ca8707258fdde

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 (253) hide show
  1. package/README.md +25 -12
  2. package/dist/acceptance.d.ts +26 -0
  3. package/dist/acceptance.d.ts.map +1 -0
  4. package/dist/acceptance.js +384 -0
  5. package/dist/adapter.d.ts +200 -54
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/adapter.js +1 -9
  8. package/dist/attached-model.d.ts +7 -10
  9. package/dist/attached-model.d.ts.map +1 -1
  10. package/dist/attached-model.js +40 -28
  11. package/dist/attempt.d.ts +16 -0
  12. package/dist/attempt.d.ts.map +1 -0
  13. package/dist/attempt.js +70 -0
  14. package/dist/binary.d.ts +18 -0
  15. package/dist/binary.d.ts.map +1 -0
  16. package/dist/binary.js +8 -0
  17. package/dist/bootstrap.d.ts +37 -5
  18. package/dist/bootstrap.d.ts.map +1 -1
  19. package/dist/bootstrap.js +336 -51
  20. package/dist/chain.d.ts +12 -0
  21. package/dist/chain.d.ts.map +1 -0
  22. package/dist/chain.js +64 -0
  23. package/dist/change-set.d.ts +9 -14
  24. package/dist/change-set.d.ts.map +1 -1
  25. package/dist/change-set.js +11 -56
  26. package/dist/change.d.ts +46 -0
  27. package/dist/change.d.ts.map +1 -1
  28. package/dist/change.js +34 -0
  29. package/dist/chat-types.d.ts +3 -0
  30. package/dist/chat-types.d.ts.map +1 -0
  31. package/dist/chat-types.js +2 -0
  32. package/dist/client.d.ts +50 -18
  33. package/dist/client.d.ts.map +1 -1
  34. package/dist/client.js +162 -30
  35. package/dist/collection.d.ts +13 -14
  36. package/dist/collection.d.ts.map +1 -1
  37. package/dist/collection.js +52 -56
  38. package/dist/compile.d.ts +16 -0
  39. package/dist/compile.d.ts.map +1 -0
  40. package/dist/compile.js +717 -0
  41. package/dist/connection.d.ts +61 -43
  42. package/dist/connection.d.ts.map +1 -1
  43. package/dist/connection.js +2377 -1476
  44. package/dist/context.d.ts +2 -0
  45. package/dist/context.d.ts.map +1 -0
  46. package/dist/context.js +7 -0
  47. package/dist/conversation-row.d.ts +33 -0
  48. package/dist/conversation-row.d.ts.map +1 -0
  49. package/dist/conversation-row.js +47 -0
  50. package/dist/conversation.d.ts +71 -22
  51. package/dist/conversation.d.ts.map +1 -1
  52. package/dist/conversation.js +996 -273
  53. package/dist/digest.d.ts +7 -0
  54. package/dist/digest.d.ts.map +1 -0
  55. package/dist/digest.js +113 -0
  56. package/dist/durability.d.ts +10 -1
  57. package/dist/durability.d.ts.map +1 -1
  58. package/dist/durability.js +5 -0
  59. package/dist/entry.d.ts +20 -0
  60. package/dist/entry.d.ts.map +1 -0
  61. package/dist/entry.js +161 -0
  62. package/dist/expectation.d.ts +21 -0
  63. package/dist/expectation.d.ts.map +1 -0
  64. package/dist/expectation.js +70 -0
  65. package/dist/failure.d.ts +13 -0
  66. package/dist/failure.d.ts.map +1 -0
  67. package/dist/failure.js +19 -0
  68. package/dist/figures.d.ts +48 -0
  69. package/dist/figures.d.ts.map +1 -0
  70. package/dist/figures.js +197 -0
  71. package/dist/file.d.ts +2 -19
  72. package/dist/file.d.ts.map +1 -1
  73. package/dist/file.js +7 -122
  74. package/dist/host.d.ts +11 -2
  75. package/dist/host.d.ts.map +1 -1
  76. package/dist/host.js +134 -34
  77. package/dist/identity.d.ts +3 -0
  78. package/dist/identity.d.ts.map +1 -1
  79. package/dist/identity.js +13 -1
  80. package/dist/index.d.ts +31 -10
  81. package/dist/index.d.ts.map +1 -1
  82. package/dist/index.js +13 -0
  83. package/dist/install.d.ts +86 -0
  84. package/dist/install.d.ts.map +1 -0
  85. package/dist/install.js +537 -0
  86. package/dist/job.d.ts +33 -0
  87. package/dist/job.d.ts.map +1 -0
  88. package/dist/job.js +54 -0
  89. package/dist/live.d.ts +23 -0
  90. package/dist/live.d.ts.map +1 -0
  91. package/dist/live.js +165 -0
  92. package/dist/log.d.ts +1 -0
  93. package/dist/log.d.ts.map +1 -1
  94. package/dist/media.d.ts +8 -0
  95. package/dist/media.d.ts.map +1 -0
  96. package/dist/media.js +218 -0
  97. package/dist/memory.d.ts +22 -8
  98. package/dist/memory.d.ts.map +1 -1
  99. package/dist/memory.js +299 -94
  100. package/dist/messages.d.ts +52 -0
  101. package/dist/messages.d.ts.map +1 -0
  102. package/dist/messages.js +168 -0
  103. package/dist/model.d.ts +17 -12
  104. package/dist/model.d.ts.map +1 -1
  105. package/dist/model.js +9 -9
  106. package/dist/name.d.ts +4 -0
  107. package/dist/name.d.ts.map +1 -0
  108. package/dist/name.js +122 -0
  109. package/dist/observation.d.ts +22 -4
  110. package/dist/observation.d.ts.map +1 -1
  111. package/dist/observation.js +120 -14
  112. package/dist/open.d.ts +2 -0
  113. package/dist/open.d.ts.map +1 -0
  114. package/dist/open.js +1 -0
  115. package/dist/opened.d.ts +22 -0
  116. package/dist/opened.d.ts.map +1 -0
  117. package/dist/opened.js +107 -0
  118. package/dist/outcome.d.ts +73 -5
  119. package/dist/outcome.d.ts.map +1 -1
  120. package/dist/outcome.js +84 -1
  121. package/dist/overlay.d.ts +16 -5
  122. package/dist/overlay.d.ts.map +1 -1
  123. package/dist/overlay.js +147 -180
  124. package/dist/parts.d.ts +59 -0
  125. package/dist/parts.d.ts.map +1 -0
  126. package/dist/parts.js +118 -0
  127. package/dist/paths.d.ts +1 -1
  128. package/dist/paths.d.ts.map +1 -1
  129. package/dist/paths.js +4 -16
  130. package/dist/query.d.ts +52 -181
  131. package/dist/query.d.ts.map +1 -1
  132. package/dist/query.js +3 -423
  133. package/dist/questions.d.ts +32 -0
  134. package/dist/questions.d.ts.map +1 -0
  135. package/dist/questions.js +118 -0
  136. package/dist/reach.d.ts +19 -5
  137. package/dist/reach.d.ts.map +1 -1
  138. package/dist/reach.js +309 -92
  139. package/dist/react.d.ts +46 -0
  140. package/dist/react.d.ts.map +1 -0
  141. package/dist/react.js +54 -0
  142. package/dist/read.d.ts +7 -0
  143. package/dist/read.d.ts.map +1 -0
  144. package/dist/read.js +17 -0
  145. package/dist/readiness.d.ts +50 -0
  146. package/dist/readiness.d.ts.map +1 -0
  147. package/dist/readiness.js +27 -0
  148. package/dist/reads.d.ts +35 -0
  149. package/dist/reads.d.ts.map +1 -0
  150. package/dist/reads.js +128 -0
  151. package/dist/recovery.d.ts +26 -0
  152. package/dist/recovery.d.ts.map +1 -0
  153. package/dist/recovery.js +232 -0
  154. package/dist/refresh.d.ts +20 -7
  155. package/dist/refresh.d.ts.map +1 -1
  156. package/dist/refresh.js +155 -59
  157. package/dist/replica.d.ts +58 -34
  158. package/dist/replica.d.ts.map +1 -1
  159. package/dist/replica.js +355 -162
  160. package/dist/room-draft.d.ts +59 -11
  161. package/dist/room-draft.d.ts.map +1 -1
  162. package/dist/room-draft.js +276 -80
  163. package/dist/room-drafts.d.ts +29 -0
  164. package/dist/room-drafts.d.ts.map +1 -0
  165. package/dist/room-drafts.js +216 -0
  166. package/dist/rows.d.ts +32 -34
  167. package/dist/rows.d.ts.map +1 -1
  168. package/dist/rows.js +65 -87
  169. package/dist/save.d.ts +74 -6
  170. package/dist/save.d.ts.map +1 -1
  171. package/dist/save.js +902 -182
  172. package/dist/saves.d.ts +96 -0
  173. package/dist/saves.d.ts.map +1 -0
  174. package/dist/saves.js +999 -0
  175. package/dist/search.d.ts +15 -54
  176. package/dist/search.d.ts.map +1 -1
  177. package/dist/search.js +10 -121
  178. package/dist/seed.d.ts +30 -0
  179. package/dist/seed.d.ts.map +1 -0
  180. package/dist/seed.js +141 -0
  181. package/dist/shared.d.ts +15 -0
  182. package/dist/shared.d.ts.map +1 -0
  183. package/dist/shared.js +421 -0
  184. package/dist/socket/admission.d.ts +7 -0
  185. package/dist/socket/admission.d.ts.map +1 -0
  186. package/dist/socket/admission.js +39 -0
  187. package/dist/socket/answers.d.ts +6 -0
  188. package/dist/socket/answers.d.ts.map +1 -0
  189. package/dist/socket/answers.js +20 -0
  190. package/dist/socket/reads.d.ts +10 -0
  191. package/dist/socket/reads.d.ts.map +1 -0
  192. package/dist/socket/reads.js +31 -0
  193. package/dist/socket.d.ts +43 -0
  194. package/dist/socket.d.ts.map +1 -0
  195. package/dist/socket.js +96 -0
  196. package/dist/source.d.ts +61 -0
  197. package/dist/source.d.ts.map +1 -0
  198. package/dist/source.js +81 -0
  199. package/dist/status.d.ts +7 -4
  200. package/dist/status.d.ts.map +1 -1
  201. package/dist/status.js +3 -15
  202. package/dist/store/fences.d.ts +16 -0
  203. package/dist/store/fences.d.ts.map +1 -0
  204. package/dist/store/fences.js +26 -0
  205. package/dist/store/installs.d.ts +30 -0
  206. package/dist/store/installs.d.ts.map +1 -0
  207. package/dist/store/installs.js +34 -0
  208. package/dist/store/records.d.ts +212 -0
  209. package/dist/store/records.d.ts.map +1 -0
  210. package/dist/store/records.js +408 -0
  211. package/dist/store/room-held.d.ts +8 -0
  212. package/dist/store/room-held.d.ts.map +1 -0
  213. package/dist/store/room-held.js +17 -0
  214. package/dist/store/watchers.d.ts +15 -0
  215. package/dist/store/watchers.d.ts.map +1 -0
  216. package/dist/store/watchers.js +36 -0
  217. package/dist/store/writes.d.ts +24 -0
  218. package/dist/store/writes.d.ts.map +1 -0
  219. package/dist/store/writes.js +125 -0
  220. package/dist/store.d.ts +77 -93
  221. package/dist/store.d.ts.map +1 -1
  222. package/dist/store.js +118 -389
  223. package/dist/stored.d.ts +156 -0
  224. package/dist/stored.d.ts.map +1 -0
  225. package/dist/stored.js +637 -0
  226. package/dist/tables.d.ts +407 -0
  227. package/dist/tables.d.ts.map +1 -0
  228. package/dist/tables.js +724 -0
  229. package/dist/tallies.d.ts +18 -0
  230. package/dist/tallies.d.ts.map +1 -0
  231. package/dist/tallies.js +129 -0
  232. package/dist/transfer.d.ts +0 -5
  233. package/dist/transfer.d.ts.map +1 -1
  234. package/dist/turn.d.ts +9 -0
  235. package/dist/turn.d.ts.map +1 -0
  236. package/dist/turn.js +75 -0
  237. package/dist/validation.d.ts +29 -0
  238. package/dist/validation.d.ts.map +1 -0
  239. package/dist/validation.js +104 -0
  240. package/dist/windows.d.ts +11 -0
  241. package/dist/windows.d.ts.map +1 -0
  242. package/dist/windows.js +16 -0
  243. package/dist/wire.d.ts +185 -45
  244. package/dist/wire.d.ts.map +1 -1
  245. package/dist/wire.js +75 -46
  246. package/package.json +33 -10
  247. package/AGENTS.md +0 -215
  248. package/dist/attachment.d.ts +0 -3
  249. package/dist/attachment.d.ts.map +0 -1
  250. package/dist/attachment.js +0 -8
  251. package/dist/read-side.d.ts +0 -18
  252. package/dist/read-side.d.ts.map +0 -1
  253. package/dist/read-side.js +0 -377
package/dist/store.js CHANGED
@@ -1,395 +1,124 @@
1
- import { sameChange } from './overlay.js';
2
- const DATABASE = 'mdruntime';
3
- const ROOMS = 'rooms';
4
- const DOCUMENTS = 'documents';
5
- const BINARY_TARGETS = 'binaryTargets';
6
- const ATTACHMENTS = 'attachments';
7
- const PENDING_CHANGES = 'pendingChanges';
8
- const DECLARATIONS = 'declarations';
9
- const ROOM_DRAFTS = 'roomDrafts';
10
- const PRESENTATION = 'presentation';
11
- const CONVERSATIONS = 'conversations';
12
- const MESSAGES = 'messages';
13
- const CHAT_DRAFTS = 'chatDrafts';
14
- const CURRENT = 'current';
15
- const SCHEMA = 20;
16
- function settled(request) {
17
- return new Promise((resolve, reject) => {
18
- request.onsuccess = () => resolve(request.result);
19
- request.onerror = () => reject(request.error);
20
- });
21
- }
22
- function completed(transaction) {
23
- return new Promise((resolve, reject) => {
24
- transaction.oncomplete = () => resolve();
25
- transaction.onerror = () => reject(transaction.error);
26
- transaction.onabort = () => reject(transaction.error);
27
- });
28
- }
29
- const EMPTY_DECLARATIONS = {
30
- lastCompleted: null,
31
- generation: null,
32
- providerLimits: [],
33
- capabilities: [],
34
- };
35
- function declaration(database) {
36
- const transaction = database.transaction(DECLARATIONS, 'readonly');
37
- return settled(transaction.objectStore(DECLARATIONS).get(CURRENT)).then((record) => record ?? EMPTY_DECLARATIONS);
38
- }
39
- function updateDeclaration(database, update) {
40
- const transaction = database.transaction(DECLARATIONS, 'readwrite');
41
- const store = transaction.objectStore(DECLARATIONS);
42
- const prior = store.get(CURRENT);
43
- prior.onsuccess = () => {
44
- store.put(update(prior.result ?? EMPTY_DECLARATIONS), CURRENT);
45
- };
46
- return completed(transaction);
47
- }
48
- function amendPending(database, locator, id, amendment) {
49
- const transaction = database.transaction(PENDING_CHANGES, 'readwrite');
50
- const pending = transaction.objectStore(PENDING_CHANGES);
51
- const request = pending.get([locator, id]);
52
- request.onsuccess = () => {
53
- const record = request.result;
54
- if (record !== undefined)
55
- pending.put({ ...record, ...amendment });
56
- };
57
- return completed(transaction);
58
- }
59
- function roomRecord(record, queue) {
60
- const { documents, binaryTargets, revisions, changes, written, attachments, ...room } = record;
61
- return { ...room, order: queue.map((batch) => batch.id) };
62
- }
63
- function removeRoomEntries(store, locator) {
64
- const keys = store.getAllKeys();
65
- keys.onsuccess = () => {
66
- for (const key of keys.result) {
67
- if (Array.isArray(key) && key[0] === locator)
68
- store.delete(key);
69
- }
70
- };
71
- }
72
- export function removeRuntimeStore() {
73
- return new Promise((resolve, reject) => {
74
- const removal = indexedDB.deleteDatabase(DATABASE);
75
- removal.onsuccess = () => resolve();
76
- removal.onerror = () => reject(removal.error);
77
- });
78
- }
79
- export async function openRuntimeStore() {
80
- const opening = indexedDB.open(DATABASE, SCHEMA);
81
- opening.onupgradeneeded = () => {
82
- const database = opening.result;
83
- for (const name of [...database.objectStoreNames])
84
- database.deleteObjectStore(name);
85
- database.createObjectStore(ROOMS, { keyPath: 'locator' });
86
- database.createObjectStore(DOCUMENTS, { keyPath: ['locator', 'path'] });
87
- database.createObjectStore(BINARY_TARGETS, { keyPath: ['locator', 'path'] });
88
- database.createObjectStore(ATTACHMENTS, { keyPath: ['locator', 'reference'] });
89
- database.createObjectStore(PENDING_CHANGES, { keyPath: ['locator', 'id'] });
90
- database.createObjectStore(DECLARATIONS);
91
- database.createObjectStore(ROOM_DRAFTS, { keyPath: ['locator', 'path'] });
92
- database.createObjectStore(PRESENTATION, { keyPath: 'app' });
93
- database.createObjectStore(CONVERSATIONS, { keyPath: 'conversation' });
94
- const messages = database.createObjectStore(MESSAGES, {
95
- keyPath: ['conversation', 'identity'],
1
+ import { schemaTypes } from './attached-model.js';
2
+ import { openedFiles } from './opened.js';
3
+ import { compositionOf } from './replica.js';
4
+ import { createFences } from './store/fences.js';
5
+ import { createInstalls } from './store/installs.js';
6
+ import { openRuntimeStore, } from './store/records.js';
7
+ import { createRoomHeld } from './store/room-held.js';
8
+ import { createWatchers } from './store/watchers.js';
9
+ import { createWrites } from './store/writes.js';
10
+ export { NO_INSTALL } from './store/installs.js';
11
+ export function createStore(options) {
12
+ const writes = createWrites();
13
+ const watchers = createWatchers(options.unpublished);
14
+ const fences = createFences();
15
+ const installs = createInstalls();
16
+ const seeds = new Map();
17
+ const landed = createRoomHeld();
18
+ const attached = createRoomHeld();
19
+ const typesHeld = new Map();
20
+ let opening;
21
+ function open() {
22
+ opening ??= options.opening(openRuntimeStore(options.database)).catch((error) => {
23
+ opening = undefined;
24
+ throw error;
96
25
  });
97
- messages.createIndex('conversation', 'conversation');
98
- database.createObjectStore(CHAT_DRAFTS, { keyPath: 'conversation' });
99
- };
100
- const database = await settled(opening);
26
+ return opening;
27
+ }
28
+ function typed(locator, persisted) {
29
+ if (persisted.status === 'unavailable')
30
+ return persisted;
31
+ const schema = persisted.snapshot.schema;
32
+ const held = typesHeld.get(locator);
33
+ if (held !== undefined && held.schema === schema)
34
+ return { ...persisted, types: held.types };
35
+ const types = schemaTypes(schema, options.roomTypes);
36
+ typesHeld.set(locator, { schema, types });
37
+ return { ...persisted, types };
38
+ }
39
+ function recordOf(stored) {
40
+ return { ...stored, model: typed(stored.locator, stored.model) };
41
+ }
42
+ async function rooms(locators) {
43
+ const held = await (await open()).rooms(locators);
44
+ return new Map(held
45
+ .filter(({ record }) => !fences.has(record.locator))
46
+ .map(({ record, indexed, documents }) => [
47
+ record.locator,
48
+ { record: recordOf(record), indexed, documents },
49
+ ]));
50
+ }
51
+ async function work(locator) {
52
+ if (fences.has(locator))
53
+ return { replica: undefined, queue: [], states: new Map() };
54
+ const { room, queue } = await (await open()).room(locator);
55
+ const batches = queue.map(({ batch }) => batch);
56
+ const states = new Map(queue.map(({ batch, state }) => [batch.id, state]));
57
+ return {
58
+ replica: room === null
59
+ ? undefined
60
+ : { record: recordOf(room.record), queue: batches, states, overlaid: room.overlaid },
61
+ queue: batches,
62
+ states,
63
+ };
64
+ }
65
+ async function drafts(locator, paths) {
66
+ const held = await (await open()).roomDrafts(locator, paths);
67
+ return held.filter((record) => !fences.has(record.locator));
68
+ }
101
69
  return {
102
- async all() {
103
- const transaction = database.transaction([ROOMS, DOCUMENTS, BINARY_TARGETS, ATTACHMENTS], 'readonly');
104
- const [rooms, documents, targets, attachments] = await Promise.all([
105
- settled(transaction.objectStore(ROOMS).getAll()),
106
- settled(transaction.objectStore(DOCUMENTS).getAll()),
107
- settled(transaction.objectStore(BINARY_TARGETS).getAll()),
108
- settled(transaction.objectStore(ATTACHMENTS).getAll()),
109
- ]);
110
- const byRoom = new Map();
111
- for (const { order, ...room } of rooms) {
112
- byRoom.set(room.locator, {
113
- ...room,
114
- documents: {},
115
- revisions: {},
116
- changes: {},
117
- written: {},
118
- binaryTargets: [],
119
- attachments: [],
120
- });
121
- }
122
- const head = (room, held) => {
123
- if (held.revision !== null) {
124
- room.revisions[held.path] = held.revision;
125
- }
126
- if (held.change !== null) {
127
- room.changes[held.path] = held.change;
128
- }
70
+ open,
71
+ durably: (work) => writes.exclusive(async () => {
72
+ const local = await open();
73
+ return work(local);
74
+ }),
75
+ exclusive: writes.exclusive,
76
+ gathered: writes.gathered,
77
+ written: writes.written,
78
+ startingWrite: writes.startingWrite,
79
+ finishingWrite: writes.finishingWrite,
80
+ ahead: writes.ahead,
81
+ rederive: writes.rederive,
82
+ refreshPage: writes.refreshPage,
83
+ stageBytes: writes.stageBytes,
84
+ deriveBytes: writes.deriveBytes,
85
+ rooms,
86
+ record: async (locator) => (await rooms([locator])).get(locator)?.record,
87
+ work,
88
+ replica: async (locator) => (await work(locator)).replica,
89
+ async covering() {
90
+ const held = await (await open()).covering();
91
+ return {
92
+ rooms: new Map([...held.rooms].filter(([locator]) => !fences.has(locator))),
93
+ queues: new Map([...held.queues].filter(([locator]) => !fences.has(locator))),
129
94
  };
130
- for (const document of documents) {
131
- const room = byRoom.get(document.locator);
132
- if (room === undefined)
133
- continue;
134
- room.documents[document.path] = document.content;
135
- if (document.written !== null) {
136
- room.written[document.path] = document.written;
137
- }
138
- head(room, document);
139
- }
140
- for (const target of targets) {
141
- const room = byRoom.get(target.locator);
142
- if (room === undefined)
143
- continue;
144
- room.binaryTargets.push(target.path);
145
- head(room, target);
146
- }
147
- for (const { locator, ...attachment } of attachments) {
148
- const room = byRoom.get(locator);
149
- if (room === undefined)
150
- continue;
151
- room.attachments.push(attachment);
152
- }
153
- return [...byRoom.values()];
154
- },
155
- async write({ next, previous, queue, previousQueue, saved, roomDrafts, consumedRoomDrafts }) {
156
- const transaction = database.transaction([ROOMS, DOCUMENTS, BINARY_TARGETS, ATTACHMENTS, PENDING_CHANGES, ROOM_DRAFTS], 'readwrite');
157
- transaction.objectStore(ROOMS).put(roomRecord(next, queue));
158
- const documents = transaction.objectStore(DOCUMENTS);
159
- for (const path of Object.keys(previous?.documents ?? {})) {
160
- if (next.documents[path] === undefined)
161
- documents.delete([next.locator, path]);
162
- }
163
- for (const [path, content] of Object.entries(next.documents)) {
164
- if (previous?.documents[path] === content &&
165
- previous.revisions[path] === next.revisions[path] &&
166
- previous.written[path] === next.written[path] &&
167
- sameChange(previous.changes[path], next.changes[path]))
168
- continue;
169
- documents.put({
170
- locator: next.locator,
171
- path,
172
- content,
173
- revision: next.revisions[path] ?? null,
174
- change: next.changes[path] ?? null,
175
- written: next.written[path] ?? null,
176
- });
177
- }
178
- const targets = transaction.objectStore(BINARY_TARGETS);
179
- const beforeTargets = new Set(previous?.binaryTargets ?? []);
180
- const afterTargets = new Set(next.binaryTargets);
181
- for (const path of beforeTargets) {
182
- if (!afterTargets.has(path))
183
- targets.delete([next.locator, path]);
184
- }
185
- for (const path of afterTargets) {
186
- if (!beforeTargets.has(path) ||
187
- previous?.revisions[path] !== next.revisions[path] ||
188
- !sameChange(previous?.changes[path], next.changes[path])) {
189
- targets.put({
190
- locator: next.locator,
191
- path,
192
- revision: next.revisions[path] ?? null,
193
- change: next.changes[path] ?? null,
194
- });
195
- }
196
- }
197
- const attachments = transaction.objectStore(ATTACHMENTS);
198
- const held = new Map((previous?.attachments ?? []).map((attachment) => [attachment.reference, attachment]));
199
- const declared = new Set(next.attachments.map((attachment) => attachment.reference));
200
- for (const reference of held.keys()) {
201
- if (!declared.has(reference))
202
- attachments.delete([next.locator, reference]);
203
- }
204
- for (const attachment of next.attachments) {
205
- if (held.get(attachment.reference) === attachment)
206
- continue;
207
- attachments.put({ ...attachment, locator: next.locator });
208
- }
209
- if (previousQueue !== undefined) {
210
- const pending = transaction.objectStore(PENDING_CHANGES);
211
- const before = new Set(previousQueue.map((batch) => batch.id));
212
- const after = new Set(queue.map((batch) => batch.id));
213
- for (const id of before) {
214
- if (!after.has(id))
215
- pending.delete([next.locator, id]);
216
- }
217
- for (const batch of queue) {
218
- if (!before.has(batch.id)) {
219
- pending.put({
220
- locator: next.locator,
221
- id: batch.id,
222
- batch,
223
- drafts: saved ?? [],
224
- submission: { state: 'waiting' },
225
- lost: false,
226
- });
227
- }
228
- }
229
- }
230
- const draftStore = transaction.objectStore(ROOM_DRAFTS);
231
- for (const path of consumedRoomDrafts ?? [])
232
- draftStore.delete([next.locator, path]);
233
- for (const record of roomDrafts ?? [])
234
- draftStore.put(record);
235
- await completed(transaction);
236
- },
237
- async revokeModels(records) {
238
- const transaction = database.transaction(ROOMS, 'readwrite');
239
- const rooms = transaction.objectStore(ROOMS);
240
- const revoked = { status: 'unavailable', reason: 'revoked' };
241
- for (const { record, queue } of records) {
242
- rooms.put(roomRecord({ ...record, model: revoked, selectionModel: revoked }, queue));
243
- }
244
- await completed(transaction);
245
- },
246
- async lastCompleted() {
247
- return (await declaration(database)).lastCompleted;
248
- },
249
- complete(at) {
250
- return updateDeclaration(database, (record) => ({ ...record, lastCompleted: at }));
251
- },
252
- async providerLimits() {
253
- return (await declaration(database)).providerLimits;
254
- },
255
- async capabilities() {
256
- return (await declaration(database)).capabilities;
257
- },
258
- declare(limits, capabilities) {
259
- return updateDeclaration(database, (record) => ({
260
- ...record,
261
- providerLimits: limits,
262
- capabilities,
263
- }));
264
- },
265
- async queues() {
266
- const transaction = database.transaction([ROOMS, PENDING_CHANGES], 'readonly');
267
- const [rooms, pending] = await Promise.all([
268
- settled(transaction.objectStore(ROOMS).getAll()),
269
- settled(transaction.objectStore(PENDING_CHANGES).getAll()),
270
- ]);
271
- const byId = new Map(pending.map((record) => [`${record.locator}\0${record.id}`, record]));
272
- return rooms.map((room) => ({
273
- locator: room.locator,
274
- pending: room.order.map((id) => {
275
- const record = byId.get(`${room.locator}\0${id}`);
276
- if (record === undefined)
277
- throw new Error(`Missing Pending Change ${room.locator}/${id}`);
278
- const { batch, drafts, submission, lost, upload, accepted } = record;
279
- return { batch, state: { drafts, submission, lost, upload, accepted } };
280
- }),
281
- }));
282
- },
283
- submit(locator, id, submission, lost) {
284
- return amendPending(database, locator, id, { submission, lost });
285
- },
286
- accept(locator, id, roomVersion) {
287
- return amendPending(database, locator, id, { accepted: roomVersion });
288
- },
289
- keepUpload(locator, id, upload) {
290
- return amendPending(database, locator, id, { upload });
291
- },
292
- async roomDrafts() {
293
- const transaction = database.transaction(ROOM_DRAFTS, 'readonly');
294
- return settled(transaction.objectStore(ROOM_DRAFTS).getAll());
295
- },
296
- async presentation() {
297
- const transaction = database.transaction(PRESENTATION, 'readonly');
298
- return settled(transaction.objectStore(PRESENTATION).getAll());
299
- },
300
- async present(record) {
301
- const transaction = database.transaction(PRESENTATION, 'readwrite');
302
- transaction.objectStore(PRESENTATION).put(record);
303
- await completed(transaction);
304
- },
305
- async conversations() {
306
- const transaction = database.transaction([CONVERSATIONS, MESSAGES], 'readonly');
307
- const [reads, messages] = await Promise.all([
308
- settled(transaction.objectStore(CONVERSATIONS).getAll()),
309
- settled(transaction.objectStore(MESSAGES).getAll()),
310
- ]);
311
- const held = new Map();
312
- for (const { conversation, read } of reads) {
313
- held.set(conversation, { conversation, read, messages: [] });
314
- }
315
- for (const { conversation, ...message } of messages) {
316
- const entry = held.get(conversation) ?? { conversation, read: undefined, messages: [] };
317
- entry.messages.push(message);
318
- held.set(conversation, entry);
319
- }
320
- return [...held.values()];
321
- },
322
- async keepMessages({ conversation, messages, read, sealed }) {
323
- const transaction = database.transaction([CONVERSATIONS, MESSAGES, CHAT_DRAFTS], 'readwrite');
324
- const stored = transaction.objectStore(MESSAGES);
325
- for (const message of messages) {
326
- stored.put({ ...message, conversation });
327
- }
328
- if (read !== undefined) {
329
- transaction
330
- .objectStore(CONVERSATIONS)
331
- .put({ conversation, read });
332
- }
333
- if (sealed === true)
334
- transaction.objectStore(CHAT_DRAFTS).delete(conversation);
335
- await completed(transaction);
336
- },
337
- async dropConversations(conversations) {
338
- const transaction = database.transaction([CONVERSATIONS, MESSAGES, CHAT_DRAFTS], 'readwrite');
339
- const messages = transaction.objectStore(MESSAGES);
340
- for (const conversation of conversations) {
341
- transaction.objectStore(CONVERSATIONS).delete(conversation);
342
- transaction.objectStore(CHAT_DRAFTS).delete(conversation);
343
- const keys = messages.index('conversation').getAllKeys(conversation);
344
- keys.onsuccess = () => {
345
- for (const key of keys.result)
346
- messages.delete(key);
347
- };
348
- }
349
- await completed(transaction);
350
- },
351
- async chatDrafts() {
352
- const transaction = database.transaction(CHAT_DRAFTS, 'readonly');
353
- return settled(transaction.objectStore(CHAT_DRAFTS).getAll());
354
- },
355
- async keepChatDraft(record) {
356
- const transaction = database.transaction(CHAT_DRAFTS, 'readwrite');
357
- transaction.objectStore(CHAT_DRAFTS).put(record);
358
- await completed(transaction);
359
- },
360
- async dropChatDraft(conversation) {
361
- const transaction = database.transaction(CHAT_DRAFTS, 'readwrite');
362
- transaction.objectStore(CHAT_DRAFTS).delete(conversation);
363
- await completed(transaction);
364
- },
365
- async keepRoomDraft(record) {
366
- const transaction = database.transaction(ROOM_DRAFTS, 'readwrite');
367
- transaction.objectStore(ROOM_DRAFTS).put(record);
368
- await completed(transaction);
369
- },
370
- async discardRoomDraft(locator, path) {
371
- const transaction = database.transaction(ROOM_DRAFTS, 'readwrite');
372
- transaction.objectStore(ROOM_DRAFTS).delete([locator, path]);
373
- await completed(transaction);
374
- },
375
- async dropRooms(locators) {
376
- const transaction = database.transaction([ROOMS, DOCUMENTS, BINARY_TARGETS, ATTACHMENTS, PENDING_CHANGES, ROOM_DRAFTS], 'readwrite');
377
- for (const locator of locators) {
378
- transaction.objectStore(ROOMS).delete(locator);
379
- for (const name of [DOCUMENTS, BINARY_TARGETS, ATTACHMENTS, PENDING_CHANGES, ROOM_DRAFTS]) {
380
- removeRoomEntries(transaction.objectStore(name), locator);
381
- }
382
- }
383
- await completed(transaction);
384
- },
385
- async generation() {
386
- return (await declaration(database)).generation;
387
- },
388
- rememberGeneration(value) {
389
- return updateDeclaration(database, (record) => ({ ...record, generation: value }));
390
- },
391
- close() {
392
- database.close();
393
95
  },
96
+ drafts,
97
+ draft: async (locator, path) => (await drafts(locator, [path]))[0],
98
+ composed: (record) => compositionOf(record, options.builtIn, options.typedPaths),
99
+ publication: watchers.publication,
100
+ watch: watchers.watch,
101
+ watching: watchers.watching,
102
+ fences,
103
+ installs,
104
+ seeds: {
105
+ of: (key) => seeds.get(key),
106
+ keep(key, seed) {
107
+ seeds.set(key, seed);
108
+ },
109
+ drop(key) {
110
+ seeds.delete(key);
111
+ },
112
+ },
113
+ landed,
114
+ attached,
115
+ opened: openedFiles(options.database, fences.has),
116
+ part: (file, part) => options.database.ask({ kind: 'file.part', key: file, part }),
117
+ forget(locator) {
118
+ typesHeld.delete(locator);
119
+ landed.forget(locator);
120
+ attached.forget(locator);
121
+ },
122
+ close: watchers.close,
394
123
  };
395
124
  }
@@ -0,0 +1,156 @@
1
+ import type { SavedFacts, HeadChange } from './adapter.js';
2
+ import { type Composed, type Composition } from './collection.js';
3
+ import type { HeldBinary } from './overlay.js';
4
+ import type { Connection } from './tables.js';
5
+ export declare const REPLICA_TABLES: readonly string[];
6
+ export type View = 'confirmed' | 'visible' | 'refreshed';
7
+ export type StoredEntry = {
8
+ readonly path: string;
9
+ readonly content: string | null;
10
+ readonly binary: HeldBinary | null;
11
+ readonly revision: string | null;
12
+ readonly fileId: string | null;
13
+ readonly created: string | null;
14
+ readonly change: HeadChange | null;
15
+ readonly written: string | null;
16
+ readonly facts: SavedFacts | null;
17
+ };
18
+ export type OverlayEntry = StoredEntry & {
19
+ readonly present: boolean;
20
+ };
21
+ export type HeldEntry = StoredEntry & {
22
+ readonly type: string | null;
23
+ };
24
+ export type TypedEntry = {
25
+ readonly path: string;
26
+ readonly type: string | null;
27
+ readonly binary: boolean;
28
+ };
29
+ export type Referrer = {
30
+ readonly path: string;
31
+ readonly target: string;
32
+ };
33
+ export type HeldRoomRow = {
34
+ readonly locator: string;
35
+ readonly key: number;
36
+ readonly record: string;
37
+ readonly indexed: boolean;
38
+ readonly documents: number;
39
+ };
40
+ export type HeldRoomWork = {
41
+ readonly room: (HeldRoomRow & {
42
+ readonly overlaid: readonly string[];
43
+ }) | null;
44
+ readonly queue: readonly {
45
+ readonly id: string;
46
+ readonly batch: string;
47
+ readonly state: string;
48
+ }[];
49
+ };
50
+ export type Covering = {
51
+ readonly rooms: readonly {
52
+ readonly locator: string;
53
+ readonly key: number;
54
+ readonly transfer: string;
55
+ readonly roomVersion: string;
56
+ readonly indexed: boolean;
57
+ readonly documents: number;
58
+ readonly bytes: number;
59
+ readonly offline: string;
60
+ }[];
61
+ readonly queues: readonly {
62
+ readonly locator: string;
63
+ readonly ids: readonly string[];
64
+ }[];
65
+ };
66
+ export type HeldLocators = {
67
+ readonly held: readonly string[];
68
+ readonly staged: readonly string[];
69
+ };
70
+ export type ReplicaOpened = {
71
+ readonly erasing: boolean;
72
+ readonly orphans: readonly number[];
73
+ };
74
+ export type RoomCommit = {
75
+ readonly locator: string;
76
+ readonly key: number;
77
+ readonly record?: string;
78
+ readonly roots?: readonly string[];
79
+ readonly published?: {
80
+ readonly from: number | null;
81
+ };
82
+ readonly refreshed?: true;
83
+ readonly composed: Composed;
84
+ readonly confirmed?: {
85
+ readonly set: readonly StoredEntry[];
86
+ readonly removed: readonly string[];
87
+ };
88
+ readonly overlay?: {
89
+ readonly set: readonly OverlayEntry[];
90
+ readonly cleared: readonly string[];
91
+ };
92
+ };
93
+ export type Committed = {
94
+ readonly changed: Readonly<Record<string, readonly string[] | null>>;
95
+ readonly indexed: Readonly<Record<string, boolean>>;
96
+ };
97
+ export declare function entered(connection: Connection, locator: string, composition: Composition, rows: readonly StoredEntry[]): number;
98
+ export declare function stage(connection: Connection, request: {
99
+ readonly locator: string;
100
+ readonly transfer: string;
101
+ readonly key: number | null;
102
+ readonly fingerprint: string;
103
+ readonly entries: readonly StoredEntry[];
104
+ readonly folders: readonly string[];
105
+ readonly part: string | null;
106
+ }): number;
107
+ export type Resumed = {
108
+ readonly key: number | null;
109
+ readonly transfer: string;
110
+ readonly staged: readonly string[];
111
+ };
112
+ export declare function resume(connection: Connection, request: {
113
+ readonly locator: string;
114
+ readonly transfer: string;
115
+ readonly fingerprint: string;
116
+ readonly parts: readonly string[];
117
+ readonly published: number | null;
118
+ }): Resumed;
119
+ export declare function refreshing(connection: Connection, request: {
120
+ readonly key: number;
121
+ readonly composed: Composed;
122
+ readonly entries: readonly StoredEntry[];
123
+ readonly removals: readonly string[];
124
+ readonly folders: readonly string[];
125
+ readonly folderRemovals: readonly string[];
126
+ }): null;
127
+ export declare function unrefresh(connection: Connection, key: number): null;
128
+ export declare function commit(connection: Connection, rooms: readonly RoomCommit[]): Committed;
129
+ export declare function fenceRooms(connection: Connection, locators: readonly string[]): null;
130
+ export declare function abandon(connection: Connection, locator: string): null;
131
+ export declare function release(connection: Connection, key: number): null;
132
+ export declare function erase(connection: Connection, locator: string | null, limit: number): {
133
+ readonly remaining: boolean;
134
+ };
135
+ export declare function rederive(connection: Connection, key: number, composed: Composed, bytes: number, ms: number, floor: number): {
136
+ readonly done: boolean;
137
+ readonly text: number;
138
+ };
139
+ export declare function entries(connection: Connection, key: number, view: View, list: readonly string[]): HeldEntry[];
140
+ export declare function typed(connection: Connection, key: number, view: View, list: readonly string[]): TypedEntry[];
141
+ export declare function referrers(connection: Connection, key: number, view: View, targets: readonly string[]): Referrer[];
142
+ export declare function folders(connection: Connection, key: number, view: View, list: readonly string[]): string[];
143
+ export declare function typedWithin(connection: Connection, key: number, view: View, roots: readonly string[], within: boolean): boolean;
144
+ export declare function opened(connection: Connection): ReplicaOpened;
145
+ export declare function heldRooms(connection: Connection, locators: readonly string[] | null): readonly HeldRoomRow[];
146
+ export declare function heldRoom(connection: Connection, locator: string): HeldRoomRow | null;
147
+ export declare function overlaid(connection: Connection, key: number): readonly string[];
148
+ export declare function covering(connection: Connection): Covering['rooms'];
149
+ export declare function offlineMode(connection: Connection, locator: string): string | null;
150
+ export declare function stagedLocators(connection: Connection): readonly string[];
151
+ export declare function heldLocators(connection: Connection): readonly string[];
152
+ export declare function demoted(connection: Connection): null;
153
+ export declare function facts(connection: Connection): Readonly<Record<string, string>>;
154
+ export declare function presentation(connection: Connection, app: string): string | null;
155
+ export declare function ended(connection: Connection): void;
156
+ //# sourceMappingURL=stored.d.ts.map