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