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