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