@agentshouse/mdruntime 0.1.0-c6e4c19c13638 → 0.1.0-c6fd2cbbff187
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 +277 -219
- package/README.md +20 -10
- package/dist/acquisition.d.ts +14 -0
- package/dist/acquisition.d.ts.map +1 -0
- package/dist/acquisition.js +24 -0
- package/dist/adapter.d.ts +94 -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 +635 -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 +27 -12
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +77 -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 +46 -15
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +1946 -414
- package/dist/conversation.d.ts +30 -10
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +485 -159
- 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 +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 +18 -2
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +101 -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 +12 -4
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +94 -25
- 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 +63 -24
- package/dist/react.d.ts +31 -18
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +15 -11
- package/dist/read-side.d.ts +13 -6
- package/dist/read-side.d.ts.map +1 -1
- package/dist/read-side.js +428 -94
- 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 +392 -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 +45 -0
- package/dist/windows.d.ts +19 -0
- package/dist/windows.d.ts.map +1 -0
- package/dist/windows.js +83 -0
- package/dist/wire.d.ts +149 -33
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +63 -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,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,8 +16,45 @@ const PRESENTATION = 'presentation';
|
|
|
11
16
|
const CONVERSATIONS = 'conversations';
|
|
12
17
|
const MESSAGES = 'messages';
|
|
13
18
|
const CHAT_DRAFTS = 'chatDrafts';
|
|
19
|
+
const CALLS = 'calls';
|
|
20
|
+
const QUEUES = 'queues';
|
|
21
|
+
const FACTS = 'facts';
|
|
14
22
|
const CURRENT = 'current';
|
|
15
|
-
const
|
|
23
|
+
const MEMBER = 'member';
|
|
24
|
+
const TARGETS = 'targets';
|
|
25
|
+
const GENERATION = 'generation';
|
|
26
|
+
const HOLDER = 'holder';
|
|
27
|
+
const BINDING = 'binding';
|
|
28
|
+
export const STORE_SCHEMA = 25;
|
|
29
|
+
const REPLICA_SHAPE = 23;
|
|
30
|
+
export const AUTHORED_SCHEMA = 3;
|
|
31
|
+
const AUTHORED_STORES = [
|
|
32
|
+
[QUEUES, { keyPath: 'locator' }],
|
|
33
|
+
[PENDING_CHANGES, { keyPath: ['locator', 'id'] }],
|
|
34
|
+
[ROOM_DRAFTS, { keyPath: ['locator', 'path'] }],
|
|
35
|
+
[CHAT_DRAFTS, { keyPath: 'conversation' }],
|
|
36
|
+
[CALLS, { keyPath: 'operation' }],
|
|
37
|
+
[FACTS, undefined],
|
|
38
|
+
[FENCES, { keyPath: 'locator' }],
|
|
39
|
+
];
|
|
40
|
+
const REPLICA_STORES = [
|
|
41
|
+
[ROOMS, { keyPath: 'locator' }],
|
|
42
|
+
[DOCUMENTS, { keyPath: ['locator', 'transfer', 'path'] }],
|
|
43
|
+
[BINARY_FILES, { keyPath: ['locator', 'transfer', 'path'] }],
|
|
44
|
+
[TRANSFERS, { keyPath: 'locator' }],
|
|
45
|
+
[RECEIVED, { keyPath: 'key' }],
|
|
46
|
+
[RECEIVED_PARTS, { keyPath: ['key', 'index'] }],
|
|
47
|
+
[DECLARATIONS, undefined],
|
|
48
|
+
[PRESENTATION, { keyPath: 'app' }],
|
|
49
|
+
[CONVERSATIONS, { keyPath: 'conversation' }],
|
|
50
|
+
[MESSAGES, { keyPath: ['conversation', 'identity'] }],
|
|
51
|
+
];
|
|
52
|
+
const READ_BATCH = 256;
|
|
53
|
+
const MESSAGE_INDEXES = [
|
|
54
|
+
['conversation', 'conversation'],
|
|
55
|
+
['position', ['conversation', 'position']],
|
|
56
|
+
['turn', ['conversation', 'turn']],
|
|
57
|
+
];
|
|
16
58
|
function settled(request) {
|
|
17
59
|
return new Promise((resolve, reject) => {
|
|
18
60
|
request.onsuccess = () => resolve(request.result);
|
|
@@ -28,7 +70,6 @@ function completed(transaction) {
|
|
|
28
70
|
}
|
|
29
71
|
const EMPTY_DECLARATIONS = {
|
|
30
72
|
lastCompleted: null,
|
|
31
|
-
generation: null,
|
|
32
73
|
providerLimits: [],
|
|
33
74
|
capabilities: [],
|
|
34
75
|
};
|
|
@@ -56,9 +97,110 @@ function amendPending(database, locator, id, amendment) {
|
|
|
56
97
|
};
|
|
57
98
|
return completed(transaction);
|
|
58
99
|
}
|
|
59
|
-
function
|
|
60
|
-
|
|
61
|
-
|
|
100
|
+
function accepted({ conversation: _held, ...message }) {
|
|
101
|
+
return message;
|
|
102
|
+
}
|
|
103
|
+
function roomRecord(record) {
|
|
104
|
+
const { documents, binaries, revisions, fileIds, changes, written, ...room } = record;
|
|
105
|
+
return room;
|
|
106
|
+
}
|
|
107
|
+
function chatDraftRecord(record) {
|
|
108
|
+
return { ...record, revision: record.revision ?? '' };
|
|
109
|
+
}
|
|
110
|
+
function underTransfer(locator, transfer, after) {
|
|
111
|
+
return after === undefined
|
|
112
|
+
? IDBKeyRange.bound([locator, transfer], [locator, transfer, []])
|
|
113
|
+
: IDBKeyRange.bound([locator, transfer, after], [locator, transfer, []], true);
|
|
114
|
+
}
|
|
115
|
+
function underRoom(locator) {
|
|
116
|
+
return IDBKeyRange.bound([locator], [locator, []]);
|
|
117
|
+
}
|
|
118
|
+
async function readTransfer(store, locator, transfer, receive) {
|
|
119
|
+
let range = underTransfer(locator, transfer);
|
|
120
|
+
for (;;) {
|
|
121
|
+
const batch = await settled(store.getAll(range, READ_BATCH));
|
|
122
|
+
for (const record of batch)
|
|
123
|
+
receive(record);
|
|
124
|
+
if (batch.length < READ_BATCH)
|
|
125
|
+
return;
|
|
126
|
+
range = underTransfer(locator, transfer, batch[batch.length - 1].path);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function emptyLayer() {
|
|
130
|
+
return { documents: {}, binaries: {}, revisions: {}, fileIds: {}, changes: {}, written: {} };
|
|
131
|
+
}
|
|
132
|
+
async function readLayer(transaction, locator, transfer) {
|
|
133
|
+
const layer = emptyLayer();
|
|
134
|
+
const head = (held) => {
|
|
135
|
+
if (held.revision !== null)
|
|
136
|
+
layer.revisions[held.path] = held.revision;
|
|
137
|
+
if (held.fileId !== null)
|
|
138
|
+
layer.fileIds[held.path] = held.fileId;
|
|
139
|
+
if (held.change !== null)
|
|
140
|
+
layer.changes[held.path] = held.change;
|
|
141
|
+
};
|
|
142
|
+
await readTransfer(transaction.objectStore(DOCUMENTS), locator, transfer, (document) => {
|
|
143
|
+
layer.documents[document.path] = document.content;
|
|
144
|
+
if (document.written !== null)
|
|
145
|
+
layer.written[document.path] = document.written;
|
|
146
|
+
head(document);
|
|
147
|
+
});
|
|
148
|
+
await readTransfer(transaction.objectStore(BINARY_FILES), locator, transfer, (file) => {
|
|
149
|
+
layer.binaries[file.path] = file.binary;
|
|
150
|
+
head(file);
|
|
151
|
+
});
|
|
152
|
+
return layer;
|
|
153
|
+
}
|
|
154
|
+
function documentRecord(locator, transfer, layer, path) {
|
|
155
|
+
return {
|
|
156
|
+
locator,
|
|
157
|
+
transfer,
|
|
158
|
+
path,
|
|
159
|
+
content: layer.documents[path],
|
|
160
|
+
revision: layer.revisions[path] ?? null,
|
|
161
|
+
fileId: layer.fileIds[path] ?? null,
|
|
162
|
+
change: layer.changes[path] ?? null,
|
|
163
|
+
written: layer.written[path] ?? null,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
function binaryFileRecord(locator, transfer, layer, path) {
|
|
167
|
+
return {
|
|
168
|
+
locator,
|
|
169
|
+
transfer,
|
|
170
|
+
path,
|
|
171
|
+
revision: layer.revisions[path] ?? null,
|
|
172
|
+
fileId: layer.fileIds[path] ?? null,
|
|
173
|
+
change: layer.changes[path] ?? null,
|
|
174
|
+
binary: layer.binaries[path],
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
function writeLayer(transaction, locator, transfer, next, base) {
|
|
178
|
+
const documents = transaction.objectStore(DOCUMENTS);
|
|
179
|
+
const files = transaction.objectStore(BINARY_FILES);
|
|
180
|
+
const paths = base === null
|
|
181
|
+
? new Set([...Object.keys(next.documents), ...Object.keys(next.binaries)])
|
|
182
|
+
: new Set([
|
|
183
|
+
...differing(base.documents, next.documents),
|
|
184
|
+
...differing(base.revisions, next.revisions),
|
|
185
|
+
...differing(base.fileIds, next.fileIds),
|
|
186
|
+
...differing(base.written, next.written),
|
|
187
|
+
...differing(base.changes, next.changes, sameChange),
|
|
188
|
+
...differing(base.binaries, next.binaries, sameBinary),
|
|
189
|
+
]);
|
|
190
|
+
for (const path of paths) {
|
|
191
|
+
if (next.documents[path] !== undefined) {
|
|
192
|
+
documents.put(documentRecord(locator, transfer, next, path));
|
|
193
|
+
}
|
|
194
|
+
else if (base?.documents[path] !== undefined) {
|
|
195
|
+
documents.delete([locator, transfer, path]);
|
|
196
|
+
}
|
|
197
|
+
if (next.binaries[path] !== undefined) {
|
|
198
|
+
files.put(binaryFileRecord(locator, transfer, next, path));
|
|
199
|
+
}
|
|
200
|
+
else if (base?.binaries[path] !== undefined) {
|
|
201
|
+
files.delete([locator, transfer, path]);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
62
204
|
}
|
|
63
205
|
function removeRoomEntries(store, locator) {
|
|
64
206
|
const keys = store.getAllKeys();
|
|
@@ -69,177 +211,207 @@ function removeRoomEntries(store, locator) {
|
|
|
69
211
|
}
|
|
70
212
|
};
|
|
71
213
|
}
|
|
72
|
-
|
|
214
|
+
function removeDatabase(name) {
|
|
73
215
|
return new Promise((resolve, reject) => {
|
|
74
|
-
const removal = indexedDB.deleteDatabase(
|
|
216
|
+
const removal = indexedDB.deleteDatabase(name);
|
|
75
217
|
removal.onsuccess = () => resolve();
|
|
76
218
|
removal.onerror = () => reject(removal.error);
|
|
77
219
|
});
|
|
78
220
|
}
|
|
79
|
-
export async function
|
|
80
|
-
const
|
|
81
|
-
|
|
221
|
+
export async function authoredHolder() {
|
|
222
|
+
const authored = await openAuthored();
|
|
223
|
+
try {
|
|
224
|
+
const transaction = authored.transaction(FACTS, 'readonly');
|
|
225
|
+
const held = transaction.objectStore(FACTS).get(HOLDER);
|
|
226
|
+
await completed(transaction);
|
|
227
|
+
return typeof held.result === 'string' ? held.result : null;
|
|
228
|
+
}
|
|
229
|
+
finally {
|
|
230
|
+
authored.close();
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
export async function authoredBy(holder) {
|
|
234
|
+
const authored = await openAuthored();
|
|
235
|
+
try {
|
|
236
|
+
const transaction = authored.transaction(FACTS, 'readwrite');
|
|
237
|
+
const facts = transaction.objectStore(FACTS);
|
|
238
|
+
const held = facts.get(HOLDER);
|
|
239
|
+
let owned = true;
|
|
240
|
+
held.onsuccess = () => {
|
|
241
|
+
if (held.result === undefined)
|
|
242
|
+
facts.put(holder, HOLDER);
|
|
243
|
+
else
|
|
244
|
+
owned = held.result === holder;
|
|
245
|
+
};
|
|
246
|
+
await completed(transaction);
|
|
247
|
+
return owned;
|
|
248
|
+
}
|
|
249
|
+
finally {
|
|
250
|
+
authored.close();
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
export async function authoredBinding() {
|
|
254
|
+
const authored = await openAuthored();
|
|
255
|
+
try {
|
|
256
|
+
const transaction = authored.transaction(FACTS, 'readonly');
|
|
257
|
+
const held = transaction.objectStore(FACTS).get(BINDING);
|
|
258
|
+
await completed(transaction);
|
|
259
|
+
return typeof held.result === 'string' ? held.result : null;
|
|
260
|
+
}
|
|
261
|
+
finally {
|
|
262
|
+
authored.close();
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
export async function bindAuthored(binding) {
|
|
266
|
+
const authored = await openAuthored();
|
|
267
|
+
try {
|
|
268
|
+
const transaction = authored.transaction(FACTS, 'readwrite');
|
|
269
|
+
transaction.objectStore(FACTS).put(binding, BINDING);
|
|
270
|
+
await completed(transaction);
|
|
271
|
+
}
|
|
272
|
+
finally {
|
|
273
|
+
authored.close();
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
export async function removeRuntimeStore(holder) {
|
|
277
|
+
await removeDatabase(DATABASE);
|
|
278
|
+
if (holder !== undefined && (await authoredBy(holder)))
|
|
279
|
+
return true;
|
|
280
|
+
await removeDatabase(AUTHORED);
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
export function openDatabase(name, version, upgrade) {
|
|
284
|
+
return new Promise((resolve, reject) => {
|
|
285
|
+
const opening = indexedDB.open(name, version);
|
|
286
|
+
opening.onupgradeneeded = (event) => upgrade(opening, event.oldVersion);
|
|
287
|
+
opening.onsuccess = () => resolve(opening.result);
|
|
288
|
+
opening.onerror = () => {
|
|
289
|
+
if (opening.error?.name !== 'VersionError') {
|
|
290
|
+
reject(opening.error);
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
removeDatabase(name)
|
|
294
|
+
.then(() => openDatabase(name, version, upgrade))
|
|
295
|
+
.then(resolve, reject);
|
|
296
|
+
};
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
function openReplica() {
|
|
300
|
+
return openDatabase(DATABASE, STORE_SCHEMA, (opening, oldVersion) => {
|
|
82
301
|
const database = opening.result;
|
|
83
|
-
|
|
84
|
-
database.
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
302
|
+
if (oldVersion < REPLICA_SHAPE) {
|
|
303
|
+
for (const name of [...database.objectStoreNames])
|
|
304
|
+
database.deleteObjectStore(name);
|
|
305
|
+
}
|
|
306
|
+
for (const [name, parameters] of REPLICA_STORES) {
|
|
307
|
+
if (database.objectStoreNames.contains(name))
|
|
308
|
+
continue;
|
|
309
|
+
database.createObjectStore(name, parameters);
|
|
310
|
+
}
|
|
311
|
+
const messages = opening.transaction.objectStore(MESSAGES);
|
|
312
|
+
for (const [name, keyPath] of MESSAGE_INDEXES) {
|
|
313
|
+
if (!messages.indexNames.contains(name))
|
|
314
|
+
messages.createIndex(name, keyPath);
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
function openAuthored() {
|
|
319
|
+
return openDatabase(AUTHORED, AUTHORED_SCHEMA, (opening) => {
|
|
320
|
+
const database = opening.result;
|
|
321
|
+
for (const [name, parameters] of AUTHORED_STORES) {
|
|
322
|
+
if (!database.objectStoreNames.contains(name))
|
|
323
|
+
database.createObjectStore(name, parameters);
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
export async function openRuntimeStore(fenced) {
|
|
328
|
+
const authored = await openAuthored();
|
|
329
|
+
const database = await openReplica().catch((error) => {
|
|
330
|
+
authored.close();
|
|
331
|
+
throw error;
|
|
332
|
+
});
|
|
101
333
|
return {
|
|
102
334
|
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
|
-
}
|
|
335
|
+
const transaction = database.transaction([ROOMS, DOCUMENTS, BINARY_FILES], 'readonly');
|
|
336
|
+
const rooms = await settled(transaction.objectStore(ROOMS).getAll());
|
|
337
|
+
const records = [];
|
|
338
|
+
for (const room of rooms.filter((held) => !fenced(held.locator))) {
|
|
339
|
+
records.push({ ...room, ...(await readLayer(transaction, room.locator, room.transfer)) });
|
|
196
340
|
}
|
|
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));
|
|
341
|
+
return records;
|
|
342
|
+
},
|
|
343
|
+
async keepAuthored({ locator, queue, founded, saved, roomDrafts, consumedRoomDrafts }) {
|
|
344
|
+
if (fenced(locator))
|
|
345
|
+
return;
|
|
346
|
+
const transaction = authored.transaction([QUEUES, PENDING_CHANGES, ROOM_DRAFTS], 'readwrite');
|
|
347
|
+
const order = transaction.objectStore(QUEUES);
|
|
348
|
+
const pending = transaction.objectStore(PENDING_CHANGES);
|
|
349
|
+
const held = order.get(locator);
|
|
350
|
+
held.onsuccess = () => {
|
|
351
|
+
const before = new Set(held.result?.order ?? []);
|
|
212
352
|
const after = new Set(queue.map((batch) => batch.id));
|
|
353
|
+
if (queue.length === 0)
|
|
354
|
+
order.delete(locator);
|
|
355
|
+
else
|
|
356
|
+
order.put({ locator, order: [...after] });
|
|
213
357
|
for (const id of before) {
|
|
214
358
|
if (!after.has(id))
|
|
215
|
-
pending.delete([
|
|
359
|
+
pending.delete([locator, id]);
|
|
216
360
|
}
|
|
217
361
|
for (const batch of queue) {
|
|
218
362
|
if (!before.has(batch.id)) {
|
|
219
363
|
pending.put({
|
|
220
|
-
locator
|
|
364
|
+
locator,
|
|
221
365
|
id: batch.id,
|
|
222
366
|
batch,
|
|
223
|
-
drafts: saved ?? [],
|
|
367
|
+
drafts: saved?.get(batch.id) ?? [],
|
|
224
368
|
submission: { state: 'waiting' },
|
|
225
369
|
lost: false,
|
|
226
370
|
});
|
|
227
371
|
}
|
|
228
372
|
}
|
|
229
|
-
|
|
373
|
+
for (const batch of founded ?? []) {
|
|
374
|
+
if (!before.has(batch.id))
|
|
375
|
+
continue;
|
|
376
|
+
const stored = pending.get([locator, batch.id]);
|
|
377
|
+
stored.onsuccess = () => {
|
|
378
|
+
const record = stored.result;
|
|
379
|
+
if (record !== undefined)
|
|
380
|
+
pending.put({ ...record, batch });
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
};
|
|
230
384
|
const draftStore = transaction.objectStore(ROOM_DRAFTS);
|
|
231
385
|
for (const path of consumedRoomDrafts ?? [])
|
|
232
|
-
draftStore.delete([
|
|
386
|
+
draftStore.delete([locator, path]);
|
|
233
387
|
for (const record of roomDrafts ?? [])
|
|
234
388
|
draftStore.put(record);
|
|
235
389
|
await completed(transaction);
|
|
236
390
|
},
|
|
391
|
+
async write({ next, previous, stored }) {
|
|
392
|
+
if (next === previous || fenced(next.locator))
|
|
393
|
+
return;
|
|
394
|
+
const transaction = database.transaction([ROOMS, DOCUMENTS, BINARY_FILES, TRANSFERS], 'readwrite');
|
|
395
|
+
transaction.objectStore(ROOMS).put(roomRecord(next));
|
|
396
|
+
const moved = previous?.transfer !== next.transfer;
|
|
397
|
+
if (moved && previous !== null) {
|
|
398
|
+
const range = underTransfer(previous.locator, previous.transfer);
|
|
399
|
+
transaction.objectStore(DOCUMENTS).delete(range);
|
|
400
|
+
transaction.objectStore(BINARY_FILES).delete(range);
|
|
401
|
+
}
|
|
402
|
+
if (moved)
|
|
403
|
+
transaction.objectStore(TRANSFERS).delete(next.locator);
|
|
404
|
+
writeLayer(transaction, next.locator, next.transfer, next, moved ? (stored ?? null) : previous);
|
|
405
|
+
await completed(transaction);
|
|
406
|
+
},
|
|
237
407
|
async revokeModels(records) {
|
|
238
408
|
const transaction = database.transaction(ROOMS, 'readwrite');
|
|
239
409
|
const rooms = transaction.objectStore(ROOMS);
|
|
240
410
|
const revoked = { status: 'unavailable', reason: 'revoked' };
|
|
241
|
-
for (const
|
|
242
|
-
|
|
411
|
+
for (const record of records) {
|
|
412
|
+
if (fenced(record.locator))
|
|
413
|
+
continue;
|
|
414
|
+
rooms.put(roomRecord({ ...record, model: revoked, selectionModel: revoked }));
|
|
243
415
|
}
|
|
244
416
|
await completed(transaction);
|
|
245
417
|
},
|
|
@@ -262,36 +434,62 @@ export async function openRuntimeStore() {
|
|
|
262
434
|
capabilities,
|
|
263
435
|
}));
|
|
264
436
|
},
|
|
437
|
+
async member() {
|
|
438
|
+
const transaction = database.transaction(DECLARATIONS, 'readonly');
|
|
439
|
+
const held = await settled(transaction.objectStore(DECLARATIONS).get(MEMBER));
|
|
440
|
+
return held ?? null;
|
|
441
|
+
},
|
|
442
|
+
async keepMember(member) {
|
|
443
|
+
const transaction = database.transaction(DECLARATIONS, 'readwrite');
|
|
444
|
+
transaction.objectStore(DECLARATIONS).put(member, MEMBER);
|
|
445
|
+
await completed(transaction);
|
|
446
|
+
},
|
|
447
|
+
async targets() {
|
|
448
|
+
const transaction = database.transaction(DECLARATIONS, 'readonly');
|
|
449
|
+
const held = await settled(transaction.objectStore(DECLARATIONS).get(TARGETS));
|
|
450
|
+
return held ?? {};
|
|
451
|
+
},
|
|
452
|
+
async keepTargets(targets) {
|
|
453
|
+
const transaction = database.transaction(DECLARATIONS, 'readwrite');
|
|
454
|
+
transaction.objectStore(DECLARATIONS).put(targets, TARGETS);
|
|
455
|
+
await completed(transaction);
|
|
456
|
+
},
|
|
265
457
|
async queues() {
|
|
266
|
-
const transaction =
|
|
267
|
-
const [
|
|
268
|
-
settled(transaction.objectStore(
|
|
458
|
+
const transaction = authored.transaction([QUEUES, PENDING_CHANGES], 'readonly');
|
|
459
|
+
const [queues, pending] = await Promise.all([
|
|
460
|
+
settled(transaction.objectStore(QUEUES).getAll()),
|
|
269
461
|
settled(transaction.objectStore(PENDING_CHANGES).getAll()),
|
|
270
462
|
]);
|
|
271
463
|
const byId = new Map(pending.map((record) => [`${record.locator}\0${record.id}`, record]));
|
|
272
|
-
return
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
464
|
+
return queues
|
|
465
|
+
.filter((queue) => !fenced(queue.locator))
|
|
466
|
+
.map((queue) => ({
|
|
467
|
+
locator: queue.locator,
|
|
468
|
+
pending: queue.order.map((id) => {
|
|
469
|
+
const record = byId.get(`${queue.locator}\0${id}`);
|
|
276
470
|
if (record === undefined)
|
|
277
|
-
throw new Error(`Missing Pending Change ${
|
|
471
|
+
throw new Error(`Missing Pending Change ${queue.locator}/${id}`);
|
|
278
472
|
const { batch, drafts, submission, lost, upload, accepted } = record;
|
|
279
473
|
return { batch, state: { drafts, submission, lost, upload, accepted } };
|
|
280
474
|
}),
|
|
281
475
|
}));
|
|
282
476
|
},
|
|
283
|
-
submit(locator, id, submission, lost) {
|
|
284
|
-
|
|
477
|
+
async submit(locator, id, submission, lost) {
|
|
478
|
+
if (!fenced(locator))
|
|
479
|
+
await amendPending(authored, locator, id, { submission, lost });
|
|
285
480
|
},
|
|
286
|
-
accept(locator, id, roomVersion) {
|
|
287
|
-
|
|
481
|
+
async accept(locator, id, roomVersion) {
|
|
482
|
+
if (!fenced(locator))
|
|
483
|
+
await amendPending(authored, locator, id, { accepted: roomVersion });
|
|
288
484
|
},
|
|
289
|
-
keepUpload(locator, id, upload) {
|
|
290
|
-
|
|
485
|
+
async keepUpload(locator, id, upload) {
|
|
486
|
+
if (!fenced(locator))
|
|
487
|
+
await amendPending(authored, locator, id, { upload });
|
|
291
488
|
},
|
|
292
489
|
async roomDrafts() {
|
|
293
|
-
const transaction =
|
|
294
|
-
|
|
490
|
+
const transaction = authored.transaction(ROOM_DRAFTS, 'readonly');
|
|
491
|
+
const held = await settled(transaction.objectStore(ROOM_DRAFTS).getAll());
|
|
492
|
+
return held.filter((record) => !fenced(record.locator));
|
|
295
493
|
},
|
|
296
494
|
async presentation() {
|
|
297
495
|
const transaction = database.transaction(PRESENTATION, 'readonly');
|
|
@@ -304,42 +502,108 @@ export async function openRuntimeStore() {
|
|
|
304
502
|
},
|
|
305
503
|
async conversations() {
|
|
306
504
|
const transaction = database.transaction([CONVERSATIONS, MESSAGES], 'readonly');
|
|
307
|
-
const
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
505
|
+
const records = await settled(transaction.objectStore(CONVERSATIONS).getAll());
|
|
506
|
+
const held = new Map(records.map(({ conversation, read, partial }) => [
|
|
507
|
+
conversation,
|
|
508
|
+
{ conversation, read, partial: partial === true },
|
|
509
|
+
]));
|
|
510
|
+
const keys = transaction.objectStore(MESSAGES).index('conversation');
|
|
511
|
+
await new Promise((resolve, reject) => {
|
|
512
|
+
const cursor = keys.openKeyCursor(null, 'nextunique');
|
|
513
|
+
cursor.onerror = () => reject(cursor.error);
|
|
514
|
+
cursor.onsuccess = () => {
|
|
515
|
+
const at = cursor.result;
|
|
516
|
+
if (at === null) {
|
|
517
|
+
resolve();
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
const conversation = at.key;
|
|
521
|
+
if (!held.has(conversation)) {
|
|
522
|
+
held.set(conversation, { conversation, read: undefined, partial: true });
|
|
523
|
+
}
|
|
524
|
+
at.continue();
|
|
525
|
+
};
|
|
526
|
+
});
|
|
320
527
|
return [...held.values()];
|
|
321
528
|
},
|
|
322
|
-
async keepMessages({ conversation, messages, read
|
|
323
|
-
const transaction = database.transaction([CONVERSATIONS, MESSAGES
|
|
529
|
+
async keepMessages({ conversation, messages, read }) {
|
|
530
|
+
const transaction = database.transaction([CONVERSATIONS, MESSAGES], 'readwrite');
|
|
324
531
|
const stored = transaction.objectStore(MESSAGES);
|
|
325
532
|
for (const message of messages) {
|
|
326
533
|
stored.put({ ...message, conversation });
|
|
327
534
|
}
|
|
328
535
|
if (read !== undefined) {
|
|
329
|
-
transaction
|
|
330
|
-
|
|
331
|
-
.
|
|
536
|
+
transaction.objectStore(CONVERSATIONS).put({
|
|
537
|
+
conversation,
|
|
538
|
+
read: read.at,
|
|
539
|
+
...(read.partial ? { partial: true } : {}),
|
|
540
|
+
});
|
|
332
541
|
}
|
|
333
|
-
if (sealed === true)
|
|
334
|
-
transaction.objectStore(CHAT_DRAFTS).delete(conversation);
|
|
335
542
|
await completed(transaction);
|
|
336
543
|
},
|
|
544
|
+
async messageWindow(conversation, limit, before) {
|
|
545
|
+
const transaction = database.transaction(MESSAGES, 'readonly');
|
|
546
|
+
const index = transaction.objectStore(MESSAGES).index('position');
|
|
547
|
+
const range = IDBKeyRange.bound([conversation], [conversation, before?.[0] ?? []]);
|
|
548
|
+
const earlier = ({ position, identity }) => before === null || position < before[0] || (position === before[0] && identity < before[1]);
|
|
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 (!earlier(record)) {
|
|
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
|
};
|