@agentshouse/mdruntime 0.1.0-cacb4c3f6734e → 0.1.0-cb0eb0d87ff49
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 +164 -99
- package/README.md +13 -3
- package/dist/adapter.d.ts +72 -16
- 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 +17 -5
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +109 -37
- package/dist/change-set.d.ts +2 -0
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +48 -16
- package/dist/change.d.ts +15 -0
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +26 -1
- package/dist/chat/acts.d.ts +33 -0
- package/dist/chat/acts.d.ts.map +1 -0
- package/dist/chat/acts.js +36 -0
- package/dist/chat/context.d.ts +3 -0
- package/dist/chat/context.d.ts.map +1 -0
- package/dist/chat/context.js +10 -0
- package/dist/chat/input.d.ts +9 -0
- package/dist/chat/input.d.ts.map +1 -0
- package/dist/chat/input.js +99 -0
- package/dist/chat/kit.d.ts +60 -0
- package/dist/chat/kit.d.ts.map +1 -0
- package/dist/chat/kit.js +123 -0
- package/dist/chat/lasting.d.ts +6 -0
- package/dist/chat/lasting.d.ts.map +1 -0
- package/dist/chat/lasting.js +19 -0
- package/dist/chat/list.d.ts +26 -0
- package/dist/chat/list.d.ts.map +1 -0
- package/dist/chat/list.js +62 -0
- package/dist/chat/markdown.d.ts +20 -0
- package/dist/chat/markdown.d.ts.map +1 -0
- package/dist/chat/markdown.js +188 -0
- package/dist/chat/message.d.ts +6 -0
- package/dist/chat/message.d.ts.map +1 -0
- package/dist/chat/message.js +192 -0
- package/dist/chat/messages.d.ts +21 -0
- package/dist/chat/messages.d.ts.map +1 -0
- package/dist/chat/messages.js +151 -0
- package/dist/chat/name.d.ts +6 -0
- package/dist/chat/name.d.ts.map +1 -0
- package/dist/chat/name.js +37 -0
- package/dist/chat/opened.d.ts +11 -0
- package/dist/chat/opened.d.ts.map +1 -0
- package/dist/chat/opened.js +17 -0
- package/dist/chat/overlay.d.ts +31 -0
- package/dist/chat/overlay.d.ts.map +1 -0
- package/dist/chat/overlay.js +97 -0
- package/dist/chat/parts.d.ts +57 -0
- package/dist/chat/parts.d.ts.map +1 -0
- package/dist/chat/parts.js +119 -0
- package/dist/chat/process.d.ts +20 -0
- package/dist/chat/process.d.ts.map +1 -0
- package/dist/chat/process.js +32 -0
- package/dist/chat/provider.d.ts +63 -0
- package/dist/chat/provider.d.ts.map +1 -0
- package/dist/chat/provider.js +620 -0
- package/dist/chat/question.d.ts +9 -0
- package/dist/chat/question.d.ts.map +1 -0
- package/dist/chat/question.js +80 -0
- package/dist/chat/questions.d.ts +31 -0
- package/dist/chat/questions.d.ts.map +1 -0
- package/dist/chat/questions.js +111 -0
- package/dist/chat/read.d.ts +5 -0
- package/dist/chat/read.d.ts.map +1 -0
- package/dist/chat/read.js +13 -0
- package/dist/chat/ready.d.ts +33 -0
- package/dist/chat/ready.d.ts.map +1 -0
- package/dist/chat/ready.js +91 -0
- package/dist/chat/style.d.ts +4 -0
- package/dist/chat/style.d.ts.map +1 -0
- package/dist/chat/style.js +302 -0
- package/dist/chat.d.ts +14 -0
- package/dist/chat.d.ts.map +1 -0
- package/dist/chat.js +8 -0
- package/dist/client.d.ts +20 -11
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +66 -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 +29 -14
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +1262 -377
- package/dist/conversation.d.ts +23 -10
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +304 -136
- package/dist/entry.d.ts +17 -0
- package/dist/entry.d.ts.map +1 -0
- package/dist/entry.js +156 -0
- package/dist/expectation.d.ts +21 -0
- package/dist/expectation.d.ts.map +1 -0
- package/dist/expectation.js +70 -0
- package/dist/failure.d.ts +13 -0
- package/dist/failure.d.ts.map +1 -0
- package/dist/failure.js +19 -0
- package/dist/fence.d.ts +6 -0
- package/dist/fence.d.ts.map +1 -0
- package/dist/fence.js +10 -0
- package/dist/host.d.ts +4 -1
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +17 -12
- package/dist/identity.d.ts +5 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +19 -1
- package/dist/index.d.ts +12 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/memory.d.ts +5 -3
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +70 -21
- package/dist/model.d.ts +11 -2
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +11 -0
- package/dist/observation.d.ts +5 -2
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +22 -11
- package/dist/open.d.ts +2 -0
- package/dist/open.d.ts.map +1 -0
- package/dist/open.js +1 -0
- package/dist/outcome.d.ts +32 -3
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +42 -1
- package/dist/overlay.d.ts +15 -3
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +94 -77
- package/dist/query.d.ts +32 -3
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +5 -31
- package/dist/reach.d.ts +2 -0
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +45 -21
- package/dist/react.d.ts +26 -17
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +8 -6
- package/dist/read-side.d.ts +13 -6
- package/dist/read-side.d.ts.map +1 -1
- package/dist/read-side.js +291 -85
- package/dist/recovery.d.ts +25 -0
- package/dist/recovery.d.ts.map +1 -0
- package/dist/recovery.js +197 -0
- package/dist/refresh.d.ts +10 -0
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +43 -38
- package/dist/replica.d.ts +9 -6
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +116 -68
- package/dist/room-draft.d.ts +26 -9
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +132 -23
- package/dist/rows.d.ts +8 -13
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +12 -14
- package/dist/save.d.ts +31 -1
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +376 -45
- package/dist/search.d.ts +31 -8
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +109 -44
- package/dist/shared.d.ts +11 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +368 -0
- package/dist/status.d.ts +2 -2
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +0 -8
- package/dist/store.d.ts +66 -17
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +506 -196
- 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 +7 -3
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +15 -2
- package/package.json +26 -14
- package/dist/attachment.d.ts +0 -3
- package/dist/attachment.d.ts.map +0 -1
- package/dist/attachment.js +0 -8
package/dist/store.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import { sameChange } from './overlay.js';
|
|
1
|
+
import { sameBinary, sameChange } from './overlay.js';
|
|
2
|
+
import { differing } from './shared.js';
|
|
2
3
|
const DATABASE = 'mdruntime';
|
|
4
|
+
const AUTHORED = 'mdruntime-authored';
|
|
5
|
+
const FENCES = 'fences';
|
|
3
6
|
const ROOMS = 'rooms';
|
|
4
7
|
const DOCUMENTS = 'documents';
|
|
5
|
-
const
|
|
6
|
-
const
|
|
8
|
+
const BINARY_FILES = 'binaryFiles';
|
|
9
|
+
const TRANSFERS = 'transfers';
|
|
10
|
+
const RECEIVED = 'received';
|
|
11
|
+
const RECEIVED_PARTS = 'receivedParts';
|
|
7
12
|
const PENDING_CHANGES = 'pendingChanges';
|
|
8
13
|
const DECLARATIONS = 'declarations';
|
|
9
14
|
const ROOM_DRAFTS = 'roomDrafts';
|
|
@@ -11,8 +16,42 @@ const PRESENTATION = 'presentation';
|
|
|
11
16
|
const CONVERSATIONS = 'conversations';
|
|
12
17
|
const MESSAGES = 'messages';
|
|
13
18
|
const CHAT_DRAFTS = 'chatDrafts';
|
|
19
|
+
const QUEUES = 'queues';
|
|
20
|
+
const FACTS = 'facts';
|
|
14
21
|
const CURRENT = 'current';
|
|
15
|
-
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
|
+
];
|
|
16
55
|
function settled(request) {
|
|
17
56
|
return new Promise((resolve, reject) => {
|
|
18
57
|
request.onsuccess = () => resolve(request.result);
|
|
@@ -28,7 +67,6 @@ function completed(transaction) {
|
|
|
28
67
|
}
|
|
29
68
|
const EMPTY_DECLARATIONS = {
|
|
30
69
|
lastCompleted: null,
|
|
31
|
-
generation: null,
|
|
32
70
|
providerLimits: [],
|
|
33
71
|
capabilities: [],
|
|
34
72
|
};
|
|
@@ -56,9 +94,105 @@ function amendPending(database, locator, id, amendment) {
|
|
|
56
94
|
};
|
|
57
95
|
return completed(transaction);
|
|
58
96
|
}
|
|
59
|
-
function
|
|
60
|
-
|
|
61
|
-
|
|
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
|
+
}
|
|
62
196
|
}
|
|
63
197
|
function removeRoomEntries(store, locator) {
|
|
64
198
|
const keys = store.getAllKeys();
|
|
@@ -69,177 +203,185 @@ function removeRoomEntries(store, locator) {
|
|
|
69
203
|
}
|
|
70
204
|
};
|
|
71
205
|
}
|
|
72
|
-
|
|
206
|
+
function removeDatabase(name) {
|
|
73
207
|
return new Promise((resolve, reject) => {
|
|
74
|
-
const removal = indexedDB.deleteDatabase(
|
|
208
|
+
const removal = indexedDB.deleteDatabase(name);
|
|
75
209
|
removal.onsuccess = () => resolve();
|
|
76
210
|
removal.onerror = () => reject(removal.error);
|
|
77
211
|
});
|
|
78
212
|
}
|
|
79
|
-
export async function
|
|
80
|
-
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);
|
|
81
298
|
opening.onupgradeneeded = () => {
|
|
82
299
|
const database = opening.result;
|
|
83
|
-
for (const name of
|
|
84
|
-
database.
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
database.createObjectStore(BINARY_TARGETS, { keyPath: ['locator', 'path'] });
|
|
88
|
-
database.createObjectStore(ATTACHMENTS, { keyPath: ['locator', 'reference'] });
|
|
89
|
-
database.createObjectStore(PENDING_CHANGES, { keyPath: ['locator', 'id'] });
|
|
90
|
-
database.createObjectStore(DECLARATIONS);
|
|
91
|
-
database.createObjectStore(ROOM_DRAFTS, { keyPath: ['locator', 'path'] });
|
|
92
|
-
database.createObjectStore(PRESENTATION, { keyPath: 'app' });
|
|
93
|
-
database.createObjectStore(CONVERSATIONS, { keyPath: 'conversation' });
|
|
94
|
-
const messages = database.createObjectStore(MESSAGES, {
|
|
95
|
-
keyPath: ['conversation', 'identity'],
|
|
96
|
-
});
|
|
97
|
-
messages.createIndex('conversation', 'conversation');
|
|
98
|
-
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
|
+
}
|
|
99
304
|
};
|
|
100
|
-
|
|
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
|
+
});
|
|
101
313
|
return {
|
|
102
314
|
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]);
|
|
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)) });
|
|
184
320
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
}
|
|
197
|
-
const attachments = transaction.objectStore(ATTACHMENTS);
|
|
198
|
-
const held = new Map((previous?.attachments ?? []).map((attachment) => [attachment.reference, attachment]));
|
|
199
|
-
const declared = new Set(next.attachments.map((attachment) => attachment.reference));
|
|
200
|
-
for (const reference of held.keys()) {
|
|
201
|
-
if (!declared.has(reference))
|
|
202
|
-
attachments.delete([next.locator, reference]);
|
|
203
|
-
}
|
|
204
|
-
for (const attachment of next.attachments) {
|
|
205
|
-
if (held.get(attachment.reference) === attachment)
|
|
206
|
-
continue;
|
|
207
|
-
attachments.put({ ...attachment, locator: next.locator });
|
|
208
|
-
}
|
|
209
|
-
if (previousQueue !== undefined) {
|
|
210
|
-
const pending = transaction.objectStore(PENDING_CHANGES);
|
|
211
|
-
const before = new Set(previousQueue.map((batch) => batch.id));
|
|
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 ?? []);
|
|
212
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] });
|
|
213
337
|
for (const id of before) {
|
|
214
338
|
if (!after.has(id))
|
|
215
|
-
pending.delete([
|
|
339
|
+
pending.delete([locator, id]);
|
|
216
340
|
}
|
|
217
341
|
for (const batch of queue) {
|
|
218
342
|
if (!before.has(batch.id)) {
|
|
219
343
|
pending.put({
|
|
220
|
-
locator
|
|
344
|
+
locator,
|
|
221
345
|
id: batch.id,
|
|
222
346
|
batch,
|
|
223
|
-
drafts: saved ?? [],
|
|
347
|
+
drafts: saved?.get(batch.id) ?? [],
|
|
224
348
|
submission: { state: 'waiting' },
|
|
225
349
|
lost: false,
|
|
226
350
|
});
|
|
227
351
|
}
|
|
228
352
|
}
|
|
229
|
-
}
|
|
353
|
+
};
|
|
230
354
|
const draftStore = transaction.objectStore(ROOM_DRAFTS);
|
|
231
355
|
for (const path of consumedRoomDrafts ?? [])
|
|
232
|
-
draftStore.delete([
|
|
356
|
+
draftStore.delete([locator, path]);
|
|
233
357
|
for (const record of roomDrafts ?? [])
|
|
234
358
|
draftStore.put(record);
|
|
235
359
|
await completed(transaction);
|
|
236
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
|
+
},
|
|
237
377
|
async revokeModels(records) {
|
|
238
378
|
const transaction = database.transaction(ROOMS, 'readwrite');
|
|
239
379
|
const rooms = transaction.objectStore(ROOMS);
|
|
240
380
|
const revoked = { status: 'unavailable', reason: 'revoked' };
|
|
241
|
-
for (const
|
|
242
|
-
|
|
381
|
+
for (const record of records) {
|
|
382
|
+
if (fenced(record.locator))
|
|
383
|
+
continue;
|
|
384
|
+
rooms.put(roomRecord({ ...record, model: revoked, selectionModel: revoked }));
|
|
243
385
|
}
|
|
244
386
|
await completed(transaction);
|
|
245
387
|
},
|
|
@@ -262,36 +404,52 @@ export async function openRuntimeStore() {
|
|
|
262
404
|
capabilities,
|
|
263
405
|
}));
|
|
264
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
|
+
},
|
|
265
417
|
async queues() {
|
|
266
|
-
const transaction =
|
|
267
|
-
const [
|
|
268
|
-
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()),
|
|
269
421
|
settled(transaction.objectStore(PENDING_CHANGES).getAll()),
|
|
270
422
|
]);
|
|
271
423
|
const byId = new Map(pending.map((record) => [`${record.locator}\0${record.id}`, record]));
|
|
272
|
-
return
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
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}`);
|
|
276
430
|
if (record === undefined)
|
|
277
|
-
throw new Error(`Missing Pending Change ${
|
|
431
|
+
throw new Error(`Missing Pending Change ${queue.locator}/${id}`);
|
|
278
432
|
const { batch, drafts, submission, lost, upload, accepted } = record;
|
|
279
433
|
return { batch, state: { drafts, submission, lost, upload, accepted } };
|
|
280
434
|
}),
|
|
281
435
|
}));
|
|
282
436
|
},
|
|
283
|
-
submit(locator, id, submission, lost) {
|
|
284
|
-
|
|
437
|
+
async submit(locator, id, submission, lost) {
|
|
438
|
+
if (!fenced(locator))
|
|
439
|
+
await amendPending(authored, locator, id, { submission, lost });
|
|
285
440
|
},
|
|
286
|
-
accept(locator, id, roomVersion) {
|
|
287
|
-
|
|
441
|
+
async accept(locator, id, roomVersion) {
|
|
442
|
+
if (!fenced(locator))
|
|
443
|
+
await amendPending(authored, locator, id, { accepted: roomVersion });
|
|
288
444
|
},
|
|
289
|
-
keepUpload(locator, id, upload) {
|
|
290
|
-
|
|
445
|
+
async keepUpload(locator, id, upload) {
|
|
446
|
+
if (!fenced(locator))
|
|
447
|
+
await amendPending(authored, locator, id, { upload });
|
|
291
448
|
},
|
|
292
449
|
async roomDrafts() {
|
|
293
|
-
const transaction =
|
|
294
|
-
|
|
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));
|
|
295
453
|
},
|
|
296
454
|
async presentation() {
|
|
297
455
|
const transaction = database.transaction(PRESENTATION, 'readonly');
|
|
@@ -304,42 +462,100 @@ export async function openRuntimeStore() {
|
|
|
304
462
|
},
|
|
305
463
|
async conversations() {
|
|
306
464
|
const transaction = database.transaction([CONVERSATIONS, MESSAGES], 'readonly');
|
|
307
|
-
const
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
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
|
+
});
|
|
320
487
|
return [...held.values()];
|
|
321
488
|
},
|
|
322
|
-
async keepMessages({ conversation, messages, read
|
|
323
|
-
const transaction = database.transaction([CONVERSATIONS, MESSAGES
|
|
489
|
+
async keepMessages({ conversation, messages, read }) {
|
|
490
|
+
const transaction = database.transaction([CONVERSATIONS, MESSAGES], 'readwrite');
|
|
324
491
|
const stored = transaction.objectStore(MESSAGES);
|
|
325
492
|
for (const message of messages) {
|
|
326
493
|
stored.put({ ...message, conversation });
|
|
327
494
|
}
|
|
328
495
|
if (read !== undefined) {
|
|
329
|
-
transaction
|
|
330
|
-
|
|
331
|
-
.
|
|
496
|
+
transaction.objectStore(CONVERSATIONS).put({
|
|
497
|
+
conversation,
|
|
498
|
+
read: read.at,
|
|
499
|
+
...(read.partial ? { partial: true } : {}),
|
|
500
|
+
});
|
|
332
501
|
}
|
|
333
|
-
if (sealed === true)
|
|
334
|
-
transaction.objectStore(CHAT_DRAFTS).delete(conversation);
|
|
335
502
|
await completed(transaction);
|
|
336
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
|
+
},
|
|
337
550
|
async dropConversations(conversations) {
|
|
338
|
-
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');
|
|
339
556
|
const messages = transaction.objectStore(MESSAGES);
|
|
340
557
|
for (const conversation of conversations) {
|
|
341
558
|
transaction.objectStore(CONVERSATIONS).delete(conversation);
|
|
342
|
-
transaction.objectStore(CHAT_DRAFTS).delete(conversation);
|
|
343
559
|
const keys = messages.index('conversation').getAllKeys(conversation);
|
|
344
560
|
keys.onsuccess = () => {
|
|
345
561
|
for (const key of keys.result)
|
|
@@ -349,46 +565,140 @@ export async function openRuntimeStore() {
|
|
|
349
565
|
await completed(transaction);
|
|
350
566
|
},
|
|
351
567
|
async chatDrafts() {
|
|
352
|
-
const transaction =
|
|
353
|
-
|
|
568
|
+
const transaction = authored.transaction(CHAT_DRAFTS, 'readonly');
|
|
569
|
+
const held = await settled(transaction.objectStore(CHAT_DRAFTS).getAll());
|
|
570
|
+
return held.map(chatDraftRecord);
|
|
354
571
|
},
|
|
355
572
|
async keepChatDraft(record) {
|
|
356
|
-
const transaction =
|
|
573
|
+
const transaction = authored.transaction(CHAT_DRAFTS, 'readwrite');
|
|
357
574
|
transaction.objectStore(CHAT_DRAFTS).put(record);
|
|
358
575
|
await completed(transaction);
|
|
359
576
|
},
|
|
360
577
|
async dropChatDraft(conversation) {
|
|
361
|
-
const transaction =
|
|
578
|
+
const transaction = authored.transaction(CHAT_DRAFTS, 'readwrite');
|
|
362
579
|
transaction.objectStore(CHAT_DRAFTS).delete(conversation);
|
|
363
580
|
await completed(transaction);
|
|
364
581
|
},
|
|
365
582
|
async keepRoomDraft(record) {
|
|
366
|
-
|
|
583
|
+
if (fenced(record.locator))
|
|
584
|
+
return;
|
|
585
|
+
const transaction = authored.transaction(ROOM_DRAFTS, 'readwrite');
|
|
367
586
|
transaction.objectStore(ROOM_DRAFTS).put(record);
|
|
368
587
|
await completed(transaction);
|
|
369
588
|
},
|
|
370
589
|
async discardRoomDraft(locator, path) {
|
|
371
|
-
|
|
590
|
+
if (fenced(locator))
|
|
591
|
+
return;
|
|
592
|
+
const transaction = authored.transaction(ROOM_DRAFTS, 'readwrite');
|
|
372
593
|
transaction.objectStore(ROOM_DRAFTS).delete([locator, path]);
|
|
373
594
|
await completed(transaction);
|
|
374
595
|
},
|
|
375
|
-
async
|
|
376
|
-
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');
|
|
377
603
|
for (const locator of locators) {
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
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);
|
|
381
608
|
}
|
|
382
609
|
}
|
|
610
|
+
await completed(work);
|
|
611
|
+
const transaction = database.transaction([ROOMS, DOCUMENTS, BINARY_FILES, TRANSFERS], 'readwrite');
|
|
612
|
+
for (const locator of locators) {
|
|
613
|
+
transaction.objectStore(ROOMS).delete(locator);
|
|
614
|
+
transaction.objectStore(TRANSFERS).delete(locator);
|
|
615
|
+
transaction.objectStore(DOCUMENTS).delete(underRoom(locator));
|
|
616
|
+
transaction.objectStore(BINARY_FILES).delete(underRoom(locator));
|
|
617
|
+
}
|
|
618
|
+
await completed(transaction);
|
|
619
|
+
const fences = authored.transaction(FENCES, 'readwrite');
|
|
620
|
+
for (const locator of locators)
|
|
621
|
+
fences.objectStore(FENCES).delete(locator);
|
|
622
|
+
await completed(fences);
|
|
623
|
+
},
|
|
624
|
+
async discard(locator) {
|
|
625
|
+
const transaction = authored.transaction([QUEUES, PENDING_CHANGES], 'readwrite');
|
|
626
|
+
transaction.objectStore(QUEUES).delete(locator);
|
|
627
|
+
removeRoomEntries(transaction.objectStore(PENDING_CHANGES), locator);
|
|
628
|
+
await completed(transaction);
|
|
629
|
+
},
|
|
630
|
+
async transfers() {
|
|
631
|
+
const transaction = database.transaction(TRANSFERS, 'readonly');
|
|
632
|
+
const held = await settled(transaction.objectStore(TRANSFERS).getAll());
|
|
633
|
+
return held.filter((record) => !fenced(record.locator));
|
|
634
|
+
},
|
|
635
|
+
staged(record) {
|
|
636
|
+
const transaction = database.transaction([DOCUMENTS, BINARY_FILES], 'readonly');
|
|
637
|
+
return readLayer(transaction, record.locator, record.transfer);
|
|
638
|
+
},
|
|
639
|
+
async stage(record, page) {
|
|
640
|
+
if (fenced(record.locator))
|
|
641
|
+
return;
|
|
642
|
+
const transaction = database.transaction([TRANSFERS, DOCUMENTS, BINARY_FILES], 'readwrite');
|
|
643
|
+
writeLayer(transaction, record.locator, record.transfer, page, null);
|
|
644
|
+
transaction.objectStore(TRANSFERS).put(record);
|
|
645
|
+
await completed(transaction);
|
|
646
|
+
},
|
|
647
|
+
async abandon(locator) {
|
|
648
|
+
if (fenced(locator))
|
|
649
|
+
return;
|
|
650
|
+
const transaction = database.transaction([TRANSFERS, DOCUMENTS, BINARY_FILES], 'readwrite');
|
|
651
|
+
const transfers = transaction.objectStore(TRANSFERS);
|
|
652
|
+
const held = transfers.get(locator);
|
|
653
|
+
held.onsuccess = () => {
|
|
654
|
+
const record = held.result;
|
|
655
|
+
if (record === undefined)
|
|
656
|
+
return;
|
|
657
|
+
transfers.delete(locator);
|
|
658
|
+
const range = underTransfer(locator, record.transfer);
|
|
659
|
+
transaction.objectStore(DOCUMENTS).delete(range);
|
|
660
|
+
transaction.objectStore(BINARY_FILES).delete(range);
|
|
661
|
+
};
|
|
662
|
+
await completed(transaction);
|
|
663
|
+
},
|
|
664
|
+
async receivedHeads() {
|
|
665
|
+
const transaction = database.transaction(RECEIVED, 'readonly');
|
|
666
|
+
return settled(transaction.objectStore(RECEIVED).getAll());
|
|
667
|
+
},
|
|
668
|
+
async receivedParts(key, count) {
|
|
669
|
+
const transaction = database.transaction(RECEIVED_PARTS, 'readonly');
|
|
670
|
+
const kept = await settled(transaction
|
|
671
|
+
.objectStore(RECEIVED_PARTS)
|
|
672
|
+
.getAll(IDBKeyRange.bound([key, 0], [key, count], false, true)));
|
|
673
|
+
return kept.map((part) => part.value);
|
|
674
|
+
},
|
|
675
|
+
async keepReceived(key, head, part) {
|
|
676
|
+
const transaction = database.transaction([RECEIVED, RECEIVED_PARTS], 'readwrite');
|
|
677
|
+
transaction.objectStore(RECEIVED).put({ key, head });
|
|
678
|
+
if (part !== undefined)
|
|
679
|
+
transaction.objectStore(RECEIVED_PARTS).put({ key, ...part });
|
|
680
|
+
await completed(transaction);
|
|
681
|
+
},
|
|
682
|
+
async dropReceived(keys) {
|
|
683
|
+
const transaction = database.transaction([RECEIVED, RECEIVED_PARTS], 'readwrite');
|
|
684
|
+
for (const key of keys) {
|
|
685
|
+
transaction.objectStore(RECEIVED).delete(key);
|
|
686
|
+
transaction.objectStore(RECEIVED_PARTS).delete(IDBKeyRange.bound([key], [key, []]));
|
|
687
|
+
}
|
|
383
688
|
await completed(transaction);
|
|
384
689
|
},
|
|
385
690
|
async generation() {
|
|
386
|
-
|
|
691
|
+
const transaction = authored.transaction(FACTS, 'readonly');
|
|
692
|
+
const held = await settled(transaction.objectStore(FACTS).get(GENERATION));
|
|
693
|
+
return held ?? null;
|
|
387
694
|
},
|
|
388
|
-
rememberGeneration(value) {
|
|
389
|
-
|
|
695
|
+
async rememberGeneration(value) {
|
|
696
|
+
const transaction = authored.transaction(FACTS, 'readwrite');
|
|
697
|
+
transaction.objectStore(FACTS).put(value, GENERATION);
|
|
698
|
+
await completed(transaction);
|
|
390
699
|
},
|
|
391
700
|
close() {
|
|
701
|
+
authored.close();
|
|
392
702
|
database.close();
|
|
393
703
|
},
|
|
394
704
|
};
|