@agentshouse/mdruntime 0.1.0-c315a76f079f0 → 0.1.0-c31fdf9e58db7
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/README.md +18 -15
- package/dist/adapter.d.ts +111 -72
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attached-model.d.ts +5 -1
- package/dist/attached-model.d.ts.map +1 -1
- package/dist/attached-model.js +38 -4
- package/dist/binary.d.ts +0 -5
- package/dist/binary.d.ts.map +1 -1
- package/dist/binary.js +1 -1
- package/dist/bootstrap.d.ts +25 -10
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +180 -103
- package/dist/chain.d.ts +11 -0
- package/dist/chain.d.ts.map +1 -0
- package/dist/chain.js +64 -0
- package/dist/change-set.d.ts +9 -16
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +11 -89
- package/dist/change.d.ts +31 -1
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +10 -1
- package/dist/client.d.ts +14 -9
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +82 -7
- package/dist/collection.d.ts +10 -21
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +39 -291
- package/dist/compile.d.ts +16 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +786 -0
- package/dist/connection.d.ts +23 -14
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +2866 -1412
- package/dist/conversation.d.ts +24 -11
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +411 -194
- package/dist/digest.d.ts +7 -0
- package/dist/digest.d.ts.map +1 -0
- package/dist/digest.js +113 -0
- package/dist/durability.d.ts +2 -5
- package/dist/durability.d.ts.map +1 -1
- package/dist/durability.js +5 -0
- package/dist/entry.d.ts +4 -1
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +18 -23
- package/dist/file.d.ts +2 -19
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +7 -122
- package/dist/fulltext.d.ts +27 -0
- package/dist/fulltext.d.ts.map +1 -0
- package/dist/fulltext.js +370 -0
- package/dist/host.d.ts +3 -8
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +52 -24
- package/dist/identity.d.ts +0 -1
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +0 -3
- package/dist/index.d.ts +15 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -2
- package/dist/live.d.ts +22 -0
- package/dist/live.d.ts.map +1 -0
- package/dist/live.js +123 -0
- package/dist/media.d.ts +8 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/media.js +218 -0
- package/dist/memory.d.ts +10 -7
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +196 -105
- package/dist/messages.d.ts +28 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +90 -0
- package/dist/model.d.ts +6 -1
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +7 -10
- package/dist/name.d.ts +4 -0
- package/dist/name.d.ts.map +1 -0
- package/dist/name.js +122 -0
- package/dist/observation.d.ts +15 -0
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +93 -3
- package/dist/opened.d.ts +22 -0
- package/dist/opened.d.ts.map +1 -0
- package/dist/opened.js +107 -0
- package/dist/outcome.d.ts +40 -8
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +38 -2
- package/dist/overlay.d.ts +2 -5
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +87 -172
- package/dist/paths.d.ts +1 -0
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +4 -0
- package/dist/query.d.ts +32 -2
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +41 -6
- package/dist/reach.d.ts +13 -4
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +209 -77
- package/dist/react.d.ts +1 -2
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +2 -14
- package/dist/readiness.d.ts +14 -15
- package/dist/readiness.d.ts.map +1 -1
- package/dist/readiness.js +2 -8
- package/dist/recovery.d.ts +3 -2
- package/dist/recovery.d.ts.map +1 -1
- package/dist/recovery.js +34 -7
- package/dist/refresh.d.ts +18 -15
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +136 -58
- package/dist/replica.d.ts +54 -36
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +279 -189
- package/dist/room-draft.d.ts +21 -3
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +102 -28
- package/dist/rows.d.ts +38 -30
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +173 -96
- package/dist/save.d.ts +52 -14
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +513 -185
- package/dist/search.d.ts +23 -42
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +28 -182
- package/dist/seed.d.ts.map +1 -1
- package/dist/seed.js +18 -4
- package/dist/shared.d.ts +7 -3
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +119 -66
- package/dist/status.d.ts +4 -3
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +3 -1
- package/dist/store.d.ts +83 -98
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +300 -685
- package/dist/stored.d.ts +130 -0
- package/dist/stored.d.ts.map +1 -0
- package/dist/stored.js +560 -0
- package/dist/tables.d.ts +325 -0
- package/dist/tables.d.ts.map +1 -0
- package/dist/tables.js +578 -0
- package/dist/transfer.d.ts +0 -5
- package/dist/transfer.d.ts.map +1 -1
- package/dist/turn.d.ts +5 -3
- package/dist/turn.d.ts.map +1 -1
- package/dist/turn.js +51 -21
- package/dist/validation.d.ts +29 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +101 -0
- package/dist/windows.d.ts +22 -0
- package/dist/windows.d.ts.map +1 -0
- package/dist/windows.js +89 -0
- package/dist/wire.d.ts +25 -5
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +7 -2
- package/dist/words.d.ts +19 -0
- package/dist/words.d.ts.map +1 -0
- package/dist/words.js +156 -0
- package/package.json +18 -19
- package/AGENTS.md +0 -307
- package/dist/acquisition.d.ts +0 -14
- package/dist/acquisition.d.ts.map +0 -1
- package/dist/acquisition.js +0 -24
- package/dist/chat/acts.d.ts +0 -33
- package/dist/chat/acts.d.ts.map +0 -1
- package/dist/chat/acts.js +0 -36
- package/dist/chat/context.d.ts +0 -3
- package/dist/chat/context.d.ts.map +0 -1
- package/dist/chat/context.js +0 -10
- package/dist/chat/input.d.ts +0 -9
- package/dist/chat/input.d.ts.map +0 -1
- package/dist/chat/input.js +0 -99
- package/dist/chat/kit.d.ts +0 -61
- package/dist/chat/kit.d.ts.map +0 -1
- package/dist/chat/kit.js +0 -123
- package/dist/chat/lasting.d.ts +0 -6
- package/dist/chat/lasting.d.ts.map +0 -1
- package/dist/chat/lasting.js +0 -19
- package/dist/chat/list.d.ts +0 -27
- package/dist/chat/list.d.ts.map +0 -1
- package/dist/chat/list.js +0 -63
- package/dist/chat/markdown.d.ts +0 -20
- package/dist/chat/markdown.d.ts.map +0 -1
- package/dist/chat/markdown.js +0 -188
- package/dist/chat/message.d.ts +0 -6
- package/dist/chat/message.d.ts.map +0 -1
- package/dist/chat/message.js +0 -192
- package/dist/chat/messages.d.ts +0 -22
- package/dist/chat/messages.d.ts.map +0 -1
- package/dist/chat/messages.js +0 -155
- package/dist/chat/name.d.ts +0 -6
- package/dist/chat/name.d.ts.map +0 -1
- package/dist/chat/name.js +0 -37
- package/dist/chat/opened.d.ts +0 -11
- package/dist/chat/opened.d.ts.map +0 -1
- package/dist/chat/opened.js +0 -17
- package/dist/chat/overlay.d.ts +0 -31
- package/dist/chat/overlay.d.ts.map +0 -1
- package/dist/chat/overlay.js +0 -97
- package/dist/chat/parts.d.ts +0 -57
- package/dist/chat/parts.d.ts.map +0 -1
- package/dist/chat/parts.js +0 -119
- package/dist/chat/process.d.ts +0 -20
- package/dist/chat/process.d.ts.map +0 -1
- package/dist/chat/process.js +0 -32
- package/dist/chat/provider.d.ts +0 -63
- package/dist/chat/provider.d.ts.map +0 -1
- package/dist/chat/provider.js +0 -629
- package/dist/chat/question.d.ts +0 -9
- package/dist/chat/question.d.ts.map +0 -1
- package/dist/chat/question.js +0 -69
- package/dist/chat/questions.d.ts +0 -31
- package/dist/chat/questions.d.ts.map +0 -1
- package/dist/chat/questions.js +0 -111
- package/dist/chat/read.d.ts +0 -5
- package/dist/chat/read.d.ts.map +0 -1
- package/dist/chat/read.js +0 -13
- package/dist/chat/ready.d.ts +0 -34
- package/dist/chat/ready.d.ts.map +0 -1
- package/dist/chat/ready.js +0 -91
- package/dist/chat/style.d.ts +0 -4
- package/dist/chat/style.d.ts.map +0 -1
- package/dist/chat/style.js +0 -302
- package/dist/chat.d.ts +0 -14
- package/dist/chat.d.ts.map +0 -1
- package/dist/chat.js +0 -8
- package/dist/read-side.d.ts +0 -25
- package/dist/read-side.d.ts.map +0 -1
- package/dist/read-side.js +0 -642
- package/dist/reads.d.ts +0 -11
- package/dist/reads.d.ts.map +0 -1
- package/dist/reads.js +0 -98
package/dist/store.js
CHANGED
|
@@ -1,760 +1,375 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const ROOMS = 'rooms';
|
|
7
|
-
const DOCUMENTS = 'documents';
|
|
8
|
-
const BINARY_FILES = 'binaryFiles';
|
|
9
|
-
const TRANSFERS = 'transfers';
|
|
10
|
-
const RECEIVED = 'received';
|
|
11
|
-
const RECEIVED_PARTS = 'receivedParts';
|
|
12
|
-
const PENDING_CHANGES = 'pendingChanges';
|
|
13
|
-
const DECLARATIONS = 'declarations';
|
|
14
|
-
const ROOM_DRAFTS = 'roomDrafts';
|
|
15
|
-
const PRESENTATION = 'presentation';
|
|
16
|
-
const CONVERSATIONS = 'conversations';
|
|
17
|
-
const MESSAGES = 'messages';
|
|
18
|
-
const CHAT_DRAFTS = 'chatDrafts';
|
|
19
|
-
const CALLS = 'calls';
|
|
20
|
-
const QUEUES = 'queues';
|
|
21
|
-
const FACTS = 'facts';
|
|
22
|
-
const CURRENT = 'current';
|
|
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
|
-
];
|
|
58
|
-
function settled(request) {
|
|
59
|
-
return new Promise((resolve, reject) => {
|
|
60
|
-
request.onsuccess = () => resolve(request.result);
|
|
61
|
-
request.onerror = () => reject(request.error);
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
function completed(transaction) {
|
|
65
|
-
return new Promise((resolve, reject) => {
|
|
66
|
-
transaction.oncomplete = () => resolve();
|
|
67
|
-
transaction.onerror = () => reject(transaction.error);
|
|
68
|
-
transaction.onabort = () => reject(transaction.error);
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
const EMPTY_DECLARATIONS = {
|
|
72
|
-
lastCompleted: null,
|
|
73
|
-
providerLimits: [],
|
|
74
|
-
capabilities: [],
|
|
75
|
-
};
|
|
76
|
-
function declaration(database) {
|
|
77
|
-
const transaction = database.transaction(DECLARATIONS, 'readonly');
|
|
78
|
-
return settled(transaction.objectStore(DECLARATIONS).get(CURRENT)).then((record) => record ?? EMPTY_DECLARATIONS);
|
|
79
|
-
}
|
|
80
|
-
function updateDeclaration(database, update) {
|
|
81
|
-
const transaction = database.transaction(DECLARATIONS, 'readwrite');
|
|
82
|
-
const store = transaction.objectStore(DECLARATIONS);
|
|
83
|
-
const prior = store.get(CURRENT);
|
|
84
|
-
prior.onsuccess = () => {
|
|
85
|
-
store.put(update(prior.result ?? EMPTY_DECLARATIONS), CURRENT);
|
|
86
|
-
};
|
|
87
|
-
return completed(transaction);
|
|
88
|
-
}
|
|
89
|
-
function amendPending(database, locator, id, amendment) {
|
|
90
|
-
const transaction = database.transaction(PENDING_CHANGES, 'readwrite');
|
|
91
|
-
const pending = transaction.objectStore(PENDING_CHANGES);
|
|
92
|
-
const request = pending.get([locator, id]);
|
|
93
|
-
request.onsuccess = () => {
|
|
94
|
-
const record = request.result;
|
|
95
|
-
if (record !== undefined)
|
|
96
|
-
pending.put({ ...record, ...amendment });
|
|
97
|
-
};
|
|
98
|
-
return completed(transaction);
|
|
99
|
-
}
|
|
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 ?? '' };
|
|
1
|
+
import { composedOf } from './collection.js';
|
|
2
|
+
import { binaryLocalId } from './identity.js';
|
|
3
|
+
const FILE = '\u0000file';
|
|
4
|
+
function batchFiles(batch) {
|
|
5
|
+
return batch.changes.flatMap((change) => (change.kind === 'binary' ? [change.localId] : []));
|
|
109
6
|
}
|
|
110
|
-
function
|
|
111
|
-
return
|
|
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
|
-
}
|
|
7
|
+
function draftFiles(drafts) {
|
|
8
|
+
return drafts.flatMap(({ parts }) => (parts.file === undefined ? [] : [parts.file.localId]));
|
|
128
9
|
}
|
|
129
|
-
function
|
|
130
|
-
return {
|
|
10
|
+
function roomDraftRow(record) {
|
|
11
|
+
return { path: record.path, record: JSON.stringify(record), files: draftFiles([record]) };
|
|
131
12
|
}
|
|
132
|
-
|
|
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) {
|
|
13
|
+
function authoredRows({ locator, queue, founded, saved, roomDrafts, consumedRoomDrafts }, held) {
|
|
155
14
|
return {
|
|
156
15
|
locator,
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
16
|
+
...(queue === undefined
|
|
17
|
+
? {}
|
|
18
|
+
: {
|
|
19
|
+
order: queue.map(({ id }) => id),
|
|
20
|
+
added: queue
|
|
21
|
+
.filter(({ id }) => held?.has(id) !== true)
|
|
22
|
+
.map((batch) => {
|
|
23
|
+
const drafts = saved?.get(batch.id) ?? [];
|
|
24
|
+
return {
|
|
25
|
+
id: batch.id,
|
|
26
|
+
batch: JSON.stringify(batch),
|
|
27
|
+
state: JSON.stringify({
|
|
28
|
+
drafts,
|
|
29
|
+
submission: { state: 'waiting' },
|
|
30
|
+
lost: false,
|
|
31
|
+
}),
|
|
32
|
+
files: [...batchFiles(batch), ...draftFiles(drafts)],
|
|
33
|
+
};
|
|
34
|
+
}),
|
|
35
|
+
}),
|
|
36
|
+
...(founded === undefined
|
|
37
|
+
? {}
|
|
38
|
+
: {
|
|
39
|
+
founded: founded.map((batch) => ({
|
|
40
|
+
id: batch.id,
|
|
41
|
+
batch: JSON.stringify(batch),
|
|
42
|
+
files: batchFiles(batch),
|
|
43
|
+
})),
|
|
44
|
+
}),
|
|
45
|
+
...(roomDrafts === undefined ? {} : { roomDrafts: roomDrafts.map(roomDraftRow) }),
|
|
46
|
+
...(consumedRoomDrafts === undefined ? {} : { consumedRoomDrafts }),
|
|
175
47
|
};
|
|
176
48
|
}
|
|
177
|
-
function
|
|
178
|
-
|
|
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
|
-
}
|
|
49
|
+
function marked(value) {
|
|
50
|
+
return typeof value === 'object' && value !== null && FILE in value;
|
|
204
51
|
}
|
|
205
|
-
function
|
|
206
|
-
const
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
52
|
+
function keyed(keys, value) {
|
|
53
|
+
const stored = [];
|
|
54
|
+
const files = [];
|
|
55
|
+
const record = JSON.stringify(value, (_name, held) => {
|
|
56
|
+
if (!(held instanceof Blob))
|
|
57
|
+
return held;
|
|
58
|
+
let key = keys.get(held);
|
|
59
|
+
if (key === undefined) {
|
|
60
|
+
key = binaryLocalId();
|
|
61
|
+
keys.set(held, key);
|
|
211
62
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
return new Promise((resolve, reject) => {
|
|
216
|
-
const removal = indexedDB.deleteDatabase(name);
|
|
217
|
-
removal.onsuccess = () => resolve();
|
|
218
|
-
removal.onerror = () => reject(removal.error);
|
|
63
|
+
stored.push({ key, blob: held });
|
|
64
|
+
files.push(key);
|
|
65
|
+
return { [FILE]: key, type: held.type };
|
|
219
66
|
});
|
|
67
|
+
return { record, files, stored };
|
|
220
68
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
return
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
|
|
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
|
-
}
|
|
69
|
+
async function filed(value, read) {
|
|
70
|
+
if (Array.isArray(value))
|
|
71
|
+
return Promise.all(value.map((item) => filed(item, read)));
|
|
72
|
+
if (marked(value))
|
|
73
|
+
return read(value[FILE], value.type);
|
|
74
|
+
if (typeof value !== 'object' || value === null)
|
|
75
|
+
return value;
|
|
76
|
+
const restored = {};
|
|
77
|
+
for (const [name, item] of Object.entries(value))
|
|
78
|
+
restored[name] = await filed(item, read);
|
|
79
|
+
return restored;
|
|
264
80
|
}
|
|
265
|
-
|
|
266
|
-
|
|
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
|
-
}
|
|
81
|
+
function json(value, fallback) {
|
|
82
|
+
return value === undefined ? fallback : JSON.parse(value);
|
|
275
83
|
}
|
|
276
|
-
export
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
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);
|
|
84
|
+
export function openRuntimeStore(fenced, authored) {
|
|
85
|
+
const keys = new WeakMap();
|
|
86
|
+
const read = async (key, type) => {
|
|
87
|
+
const blob = await authored.ask({ kind: 'authored.file', key, type });
|
|
88
|
+
keys.set(blob, key);
|
|
89
|
+
return blob;
|
|
90
|
+
};
|
|
91
|
+
const restored = async (records) => Promise.all(records.map(async (record) => (await filed(JSON.parse(record), read))));
|
|
92
|
+
const amend = async (locator, id, amendment) => {
|
|
93
|
+
if (fenced(locator))
|
|
94
|
+
return;
|
|
95
|
+
await authored.ask({
|
|
96
|
+
kind: 'authored.amend',
|
|
97
|
+
locator,
|
|
98
|
+
id,
|
|
99
|
+
amendment: JSON.stringify(amendment),
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
const fact = (name) => authored.ask({ kind: 'authored.fact', name });
|
|
103
|
+
const queued = new Map();
|
|
104
|
+
const changed = new Map();
|
|
105
|
+
const change = (key) => changed.set(key, (changed.get(key) ?? 0) + 1);
|
|
106
|
+
const rows = (writes) => writes.map((write) => authoredRows(write, queued.get(write.locator)));
|
|
107
|
+
const noted = (writes) => {
|
|
108
|
+
for (const { locator, queue } of writes) {
|
|
109
|
+
if (queue !== undefined)
|
|
110
|
+
queued.set(locator, new Set(queue.map(({ id }) => id)));
|
|
324
111
|
}
|
|
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
|
-
});
|
|
112
|
+
};
|
|
333
113
|
return {
|
|
334
|
-
async
|
|
335
|
-
const
|
|
336
|
-
const
|
|
337
|
-
const
|
|
338
|
-
for (const
|
|
339
|
-
|
|
114
|
+
async opened() {
|
|
115
|
+
const held = await authored.ask({ kind: 'replica.open' });
|
|
116
|
+
const unindexed = new Set(held.keys.flatMap(({ key, rederiving, searching }) => rederiving === null && searching === null ? [] : [key]));
|
|
117
|
+
const overlaid = new Map();
|
|
118
|
+
for (const { key, path } of held.overlaid) {
|
|
119
|
+
const paths = overlaid.get(key) ?? new Set();
|
|
120
|
+
paths.add(path);
|
|
121
|
+
overlaid.set(key, paths);
|
|
340
122
|
}
|
|
341
|
-
return
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
pending.put({
|
|
364
|
-
locator,
|
|
365
|
-
id: batch.id,
|
|
366
|
-
batch,
|
|
367
|
-
drafts: saved?.get(batch.id) ?? [],
|
|
368
|
-
submission: { state: 'waiting' },
|
|
369
|
-
lost: false,
|
|
370
|
-
});
|
|
371
|
-
}
|
|
372
|
-
}
|
|
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
|
-
}
|
|
123
|
+
return {
|
|
124
|
+
rooms: new Map(held.rooms
|
|
125
|
+
.filter(({ locator }) => !fenced(locator))
|
|
126
|
+
.map(({ locator, key, record }) => [
|
|
127
|
+
locator,
|
|
128
|
+
{ ...JSON.parse(record), key },
|
|
129
|
+
])),
|
|
130
|
+
unindexed,
|
|
131
|
+
overlaid,
|
|
132
|
+
erasing: held.erasing.length > 0,
|
|
133
|
+
orphans: held.orphans,
|
|
134
|
+
lastCompleted: json(held.facts.lastCompleted, null),
|
|
135
|
+
providerLimits: json(held.facts.providerLimits, []),
|
|
136
|
+
capabilities: json(held.facts.capabilities, []),
|
|
137
|
+
member: json(held.facts.member, null),
|
|
138
|
+
targets: json(held.facts.targets, {}),
|
|
139
|
+
presentation: held.presentation.map(({ app, value }) => ({
|
|
140
|
+
app,
|
|
141
|
+
value: JSON.parse(value),
|
|
142
|
+
})),
|
|
143
|
+
conversationList: json(held.facts.conversationList, null),
|
|
144
|
+
conversationCursor: json(held.facts.conversationCursor, null),
|
|
383
145
|
};
|
|
384
|
-
const draftStore = transaction.objectStore(ROOM_DRAFTS);
|
|
385
|
-
for (const path of consumedRoomDrafts ?? [])
|
|
386
|
-
draftStore.delete([locator, path]);
|
|
387
|
-
for (const record of roomDrafts ?? [])
|
|
388
|
-
draftStore.put(record);
|
|
389
|
-
await completed(transaction);
|
|
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
|
-
},
|
|
407
|
-
async revokeModels(records) {
|
|
408
|
-
const transaction = database.transaction(ROOMS, 'readwrite');
|
|
409
|
-
const rooms = transaction.objectStore(ROOMS);
|
|
410
|
-
const revoked = { status: 'unavailable', reason: 'revoked' };
|
|
411
|
-
for (const record of records) {
|
|
412
|
-
if (fenced(record.locator))
|
|
413
|
-
continue;
|
|
414
|
-
rooms.put(roomRecord({ ...record, model: revoked, selectionModel: revoked }));
|
|
415
|
-
}
|
|
416
|
-
await completed(transaction);
|
|
417
146
|
},
|
|
418
|
-
|
|
419
|
-
return (
|
|
147
|
+
counts(rooms) {
|
|
148
|
+
return authored.ask({ kind: 'replica.counts', rooms });
|
|
420
149
|
},
|
|
421
|
-
|
|
422
|
-
return
|
|
150
|
+
query(plan, rooms, budget) {
|
|
151
|
+
return authored.ask({ kind: 'replica.query', plan, rooms, budget });
|
|
423
152
|
},
|
|
424
|
-
|
|
425
|
-
return (
|
|
153
|
+
changes(keys) {
|
|
154
|
+
return keys.reduce((total, key) => total + (changed.get(key) ?? 0), 0);
|
|
426
155
|
},
|
|
427
|
-
|
|
428
|
-
return (
|
|
156
|
+
entries(key, view, paths) {
|
|
157
|
+
return authored.ask({ kind: 'replica.entries', key, view, paths });
|
|
429
158
|
},
|
|
430
|
-
|
|
431
|
-
return
|
|
432
|
-
...record,
|
|
433
|
-
providerLimits: limits,
|
|
434
|
-
capabilities,
|
|
435
|
-
}));
|
|
159
|
+
typed(key, view, paths) {
|
|
160
|
+
return authored.ask({ kind: 'replica.typed', key, view, paths });
|
|
436
161
|
},
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
const held = await settled(transaction.objectStore(DECLARATIONS).get(MEMBER));
|
|
440
|
-
return held ?? null;
|
|
162
|
+
referrers(key, view, targets, kinds) {
|
|
163
|
+
return authored.ask({ kind: 'replica.referrers', key, view, targets, kinds });
|
|
441
164
|
},
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
transaction.objectStore(DECLARATIONS).put(member, MEMBER);
|
|
445
|
-
await completed(transaction);
|
|
165
|
+
files(key, view, after, limit) {
|
|
166
|
+
return authored.ask({ kind: 'replica.files', key, view, after, limit });
|
|
446
167
|
},
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
168
|
+
folders(key, view, paths) {
|
|
169
|
+
return authored.ask({ kind: 'replica.folders', key, view, paths });
|
|
170
|
+
},
|
|
171
|
+
typedWithin(key, view, roots, within) {
|
|
172
|
+
return authored.ask({ kind: 'replica.typedWithin', key, view, roots, within });
|
|
173
|
+
},
|
|
174
|
+
search(ask) {
|
|
175
|
+
return authored.ask({ kind: 'replica.search', ...ask });
|
|
176
|
+
},
|
|
177
|
+
async stage(record, composition, page) {
|
|
178
|
+
if (fenced(record.locator))
|
|
179
|
+
return null;
|
|
180
|
+
return authored.ask({
|
|
181
|
+
kind: 'replica.stage',
|
|
182
|
+
locator: record.locator,
|
|
183
|
+
transfer: record.transfer,
|
|
184
|
+
key: record.key,
|
|
185
|
+
fingerprint: composedOf(composition).fingerprint,
|
|
186
|
+
entries: page.entries,
|
|
187
|
+
folders: page.folders,
|
|
188
|
+
});
|
|
189
|
+
},
|
|
190
|
+
async refresh(key, composition, page) {
|
|
191
|
+
await authored.ask({
|
|
192
|
+
kind: 'replica.refresh',
|
|
193
|
+
key,
|
|
194
|
+
composed: composedOf(composition),
|
|
195
|
+
entries: page.entries,
|
|
196
|
+
removals: page.removals,
|
|
197
|
+
folders: page.folders,
|
|
198
|
+
folderRemovals: page.folderRemovals,
|
|
199
|
+
});
|
|
451
200
|
},
|
|
452
|
-
async
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
201
|
+
async unrefresh(key) {
|
|
202
|
+
await authored.ask({ kind: 'replica.unrefresh', key });
|
|
203
|
+
},
|
|
204
|
+
async commit({ rooms, writes = [], files = [], whole = false, discards = [] }) {
|
|
205
|
+
const kept = whole ? writes : writes.filter(({ locator }) => !fenced(locator));
|
|
206
|
+
const refused = whole ? kept.find(({ locator }) => fenced(locator)) : undefined;
|
|
207
|
+
if (refused !== undefined) {
|
|
208
|
+
return { fenced: refused.locator, counts: {}, changed: {}, indexed: {} };
|
|
209
|
+
}
|
|
210
|
+
for (const room of rooms)
|
|
211
|
+
change(room.key);
|
|
212
|
+
const committed = await authored.ask({
|
|
213
|
+
kind: 'replica.commit',
|
|
214
|
+
...(kept.length === 0 && files.length === 0 && discards.length === 0
|
|
215
|
+
? {}
|
|
216
|
+
: { authored: { writes: rows(kept), files, whole, discards } }),
|
|
217
|
+
rooms: rooms.filter(({ locator }) => !fenced(locator)),
|
|
218
|
+
});
|
|
219
|
+
if (committed.fenced === null || !whole)
|
|
220
|
+
noted(kept);
|
|
221
|
+
for (const locator of discards)
|
|
222
|
+
queued.delete(locator);
|
|
223
|
+
return committed;
|
|
224
|
+
},
|
|
225
|
+
async fence(locators) {
|
|
226
|
+
await authored.ask({ kind: 'replica.fence', locators });
|
|
227
|
+
for (const locator of locators)
|
|
228
|
+
queued.delete(locator);
|
|
229
|
+
},
|
|
230
|
+
async erase(locator, limit) {
|
|
231
|
+
return (await authored.ask({ kind: 'replica.erase', locator, limit })).remaining;
|
|
232
|
+
},
|
|
233
|
+
async abandon(locator) {
|
|
234
|
+
if (fenced(locator))
|
|
235
|
+
return;
|
|
236
|
+
await authored.ask({ kind: 'replica.abandon', locator });
|
|
237
|
+
},
|
|
238
|
+
async release(key) {
|
|
239
|
+
await authored.ask({ kind: 'replica.release', key });
|
|
240
|
+
},
|
|
241
|
+
async rederive(key, composition, bytes, ms, floor) {
|
|
242
|
+
change(key);
|
|
243
|
+
return authored.ask({
|
|
244
|
+
kind: 'replica.rederive',
|
|
245
|
+
key,
|
|
246
|
+
composed: composedOf(composition),
|
|
247
|
+
bytes,
|
|
248
|
+
ms,
|
|
249
|
+
floor,
|
|
250
|
+
});
|
|
251
|
+
},
|
|
252
|
+
async fact(name, value) {
|
|
253
|
+
await authored.ask({ kind: 'replica.fact', name, value: JSON.stringify(value) });
|
|
456
254
|
},
|
|
457
255
|
async queues() {
|
|
458
|
-
const
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
]);
|
|
256
|
+
const { queues, pending } = await authored.ask({ kind: 'authored.queues' });
|
|
257
|
+
queued.clear();
|
|
258
|
+
for (const queue of queues)
|
|
259
|
+
queued.set(queue.locator, new Set(queue.ids));
|
|
463
260
|
const byId = new Map(pending.map((record) => [`${record.locator}\0${record.id}`, record]));
|
|
464
261
|
return queues
|
|
465
262
|
.filter((queue) => !fenced(queue.locator))
|
|
466
263
|
.map((queue) => ({
|
|
467
264
|
locator: queue.locator,
|
|
468
|
-
pending: queue.
|
|
265
|
+
pending: queue.ids.map((id) => {
|
|
469
266
|
const record = byId.get(`${queue.locator}\0${id}`);
|
|
470
267
|
if (record === undefined)
|
|
471
268
|
throw new Error(`Missing Pending Change ${queue.locator}/${id}`);
|
|
472
|
-
|
|
473
|
-
|
|
269
|
+
return {
|
|
270
|
+
batch: JSON.parse(record.batch),
|
|
271
|
+
state: JSON.parse(record.state),
|
|
272
|
+
};
|
|
474
273
|
}),
|
|
475
274
|
}));
|
|
476
275
|
},
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
await amendPending(authored, locator, id, { submission, lost });
|
|
480
|
-
},
|
|
481
|
-
async accept(locator, id, roomVersion) {
|
|
482
|
-
if (!fenced(locator))
|
|
483
|
-
await amendPending(authored, locator, id, { accepted: roomVersion });
|
|
276
|
+
submit(locator, id, submission, lost) {
|
|
277
|
+
return amend(locator, id, { submission, lost });
|
|
484
278
|
},
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
await amendPending(authored, locator, id, { upload });
|
|
279
|
+
keepUpload(locator, id, upload) {
|
|
280
|
+
return amend(locator, id, { upload });
|
|
488
281
|
},
|
|
489
282
|
async roomDrafts() {
|
|
490
|
-
const
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
async presentation() {
|
|
495
|
-
const transaction = database.transaction(PRESENTATION, 'readonly');
|
|
496
|
-
return settled(transaction.objectStore(PRESENTATION).getAll());
|
|
283
|
+
const held = await authored.ask({ kind: 'authored.roomDrafts' });
|
|
284
|
+
return held
|
|
285
|
+
.map((record) => JSON.parse(record))
|
|
286
|
+
.filter((record) => !fenced(record.locator));
|
|
497
287
|
},
|
|
498
288
|
async present(record) {
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
async conversations() {
|
|
504
|
-
const transaction = database.transaction([CONVERSATIONS, MESSAGES], 'readonly');
|
|
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
|
-
};
|
|
289
|
+
await authored.ask({
|
|
290
|
+
kind: 'replica.present',
|
|
291
|
+
app: record.app,
|
|
292
|
+
value: JSON.stringify(record.value ?? null),
|
|
526
293
|
});
|
|
527
|
-
return [...held.values()];
|
|
528
294
|
},
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
},
|
|
544
|
-
async messageWindow(conversation, limit) {
|
|
545
|
-
const transaction = database.transaction(MESSAGES, 'readonly');
|
|
546
|
-
const index = transaction.objectStore(MESSAGES).index('position');
|
|
547
|
-
const range = IDBKeyRange.bound([conversation], [conversation, []]);
|
|
548
|
-
if (limit === undefined) {
|
|
549
|
-
return (await settled(index.getAll(range))).map(accepted);
|
|
550
|
-
}
|
|
551
|
-
const latest = [];
|
|
552
|
-
if (limit === 0)
|
|
553
|
-
return latest;
|
|
554
|
-
await new Promise((resolve, reject) => {
|
|
555
|
-
const cursor = index.openCursor(range, 'prev');
|
|
556
|
-
cursor.onerror = () => reject(cursor.error);
|
|
557
|
-
cursor.onsuccess = () => {
|
|
558
|
-
const at = cursor.result;
|
|
559
|
-
if (at === null) {
|
|
560
|
-
resolve();
|
|
561
|
-
return;
|
|
562
|
-
}
|
|
563
|
-
latest.push(accepted(at.value));
|
|
564
|
-
if (latest.length === limit)
|
|
565
|
-
resolve();
|
|
566
|
-
else
|
|
567
|
-
at.continue();
|
|
568
|
-
};
|
|
569
|
-
});
|
|
570
|
-
return latest.reverse();
|
|
571
|
-
},
|
|
572
|
-
async messagesAfter(conversation, identity) {
|
|
573
|
-
const transaction = database.transaction(MESSAGES, 'readonly');
|
|
574
|
-
const range = identity === null
|
|
575
|
-
? IDBKeyRange.bound([conversation], [conversation, []])
|
|
576
|
-
: IDBKeyRange.bound([conversation, identity], [conversation, []], true);
|
|
577
|
-
const held = await settled(transaction.objectStore(MESSAGES).getAll(range, READ_BATCH));
|
|
578
|
-
return held.map(accepted);
|
|
579
|
-
},
|
|
580
|
-
async messageIdentities(conversation) {
|
|
581
|
-
const transaction = database.transaction(MESSAGES, 'readonly');
|
|
582
|
-
const keys = await settled(transaction.objectStore(MESSAGES).index('conversation').getAllKeys(conversation));
|
|
583
|
-
return keys.map((key) => key[1]);
|
|
584
|
-
},
|
|
585
|
-
async turnArrived(conversation, turn) {
|
|
586
|
-
const transaction = database.transaction(MESSAGES, 'readonly');
|
|
587
|
-
const held = await settled(transaction.objectStore(MESSAGES).index('turn').count([conversation, turn]));
|
|
588
|
-
return held > 0;
|
|
295
|
+
conversations() {
|
|
296
|
+
return authored.ask({ kind: 'conversation.held' });
|
|
297
|
+
},
|
|
298
|
+
async keepMessages(write) {
|
|
299
|
+
await authored.ask({ kind: 'conversation.keep', ...write });
|
|
300
|
+
},
|
|
301
|
+
messageWindow(conversation, limit, before) {
|
|
302
|
+
return authored.ask({ kind: 'conversation.window', conversation, limit, before });
|
|
303
|
+
},
|
|
304
|
+
messageCount(conversation) {
|
|
305
|
+
return authored.ask({ kind: 'conversation.count', conversation });
|
|
306
|
+
},
|
|
307
|
+
turnArrived(conversation, turn) {
|
|
308
|
+
return authored.ask({ kind: 'conversation.turn', conversation, turn });
|
|
589
309
|
},
|
|
590
310
|
async dropConversations(conversations) {
|
|
591
|
-
|
|
592
|
-
for (const conversation of conversations)
|
|
593
|
-
drafts.objectStore(CHAT_DRAFTS).delete(conversation);
|
|
594
|
-
await completed(drafts);
|
|
595
|
-
const transaction = database.transaction([CONVERSATIONS, MESSAGES], 'readwrite');
|
|
596
|
-
const messages = transaction.objectStore(MESSAGES);
|
|
597
|
-
for (const conversation of conversations) {
|
|
598
|
-
transaction.objectStore(CONVERSATIONS).delete(conversation);
|
|
599
|
-
const keys = messages.index('conversation').getAllKeys(conversation);
|
|
600
|
-
keys.onsuccess = () => {
|
|
601
|
-
for (const key of keys.result)
|
|
602
|
-
messages.delete(key);
|
|
603
|
-
};
|
|
604
|
-
}
|
|
605
|
-
await completed(transaction);
|
|
311
|
+
await authored.ask({ kind: 'conversation.drop', conversations });
|
|
606
312
|
},
|
|
607
313
|
async chatDrafts() {
|
|
608
|
-
const
|
|
609
|
-
|
|
610
|
-
return held.map(chatDraftRecord);
|
|
314
|
+
const records = await authored.ask({ kind: 'authored.chatDrafts' });
|
|
315
|
+
return records.map((record) => JSON.parse(record));
|
|
611
316
|
},
|
|
612
317
|
async keepChatDrafts(records) {
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
318
|
+
await authored.ask({
|
|
319
|
+
kind: 'authored.chatDraft',
|
|
320
|
+
kept: records.map((record) => ({
|
|
321
|
+
conversation: record.conversation,
|
|
322
|
+
record: JSON.stringify(record),
|
|
323
|
+
})),
|
|
324
|
+
dropped: [],
|
|
325
|
+
});
|
|
617
326
|
},
|
|
618
327
|
async dropChatDraft(conversation) {
|
|
619
|
-
|
|
620
|
-
transaction.objectStore(CHAT_DRAFTS).delete(conversation);
|
|
621
|
-
await completed(transaction);
|
|
328
|
+
await authored.ask({ kind: 'authored.chatDraft', kept: [], dropped: [conversation] });
|
|
622
329
|
},
|
|
623
330
|
async calls() {
|
|
624
|
-
|
|
625
|
-
return settled(transaction.objectStore(CALLS).getAll());
|
|
331
|
+
return restored(await authored.ask({ kind: 'authored.calls' }));
|
|
626
332
|
},
|
|
627
333
|
async keepCall(record) {
|
|
628
|
-
const
|
|
629
|
-
|
|
630
|
-
|
|
334
|
+
const { stored, ...kept } = keyed(keys, record);
|
|
335
|
+
await authored.ask({
|
|
336
|
+
kind: 'authored.call',
|
|
337
|
+
operation: record.operation,
|
|
338
|
+
kept,
|
|
339
|
+
files: stored,
|
|
340
|
+
});
|
|
631
341
|
},
|
|
632
342
|
async dropCall(operation) {
|
|
633
|
-
|
|
634
|
-
transaction.objectStore(CALLS).delete(operation);
|
|
635
|
-
await completed(transaction);
|
|
343
|
+
await authored.ask({ kind: 'authored.call', operation, kept: null, files: [] });
|
|
636
344
|
},
|
|
637
|
-
async keepRoomDraft(record) {
|
|
345
|
+
async keepRoomDraft(record, files) {
|
|
638
346
|
if (fenced(record.locator))
|
|
639
347
|
return;
|
|
640
|
-
const
|
|
641
|
-
|
|
642
|
-
|
|
348
|
+
const { path, ...kept } = roomDraftRow(record);
|
|
349
|
+
await authored.ask({
|
|
350
|
+
kind: 'authored.roomDraft',
|
|
351
|
+
locator: record.locator,
|
|
352
|
+
path,
|
|
353
|
+
kept,
|
|
354
|
+
files,
|
|
355
|
+
});
|
|
643
356
|
},
|
|
644
357
|
async discardRoomDraft(locator, path) {
|
|
645
358
|
if (fenced(locator))
|
|
646
359
|
return;
|
|
647
|
-
|
|
648
|
-
transaction.objectStore(ROOM_DRAFTS).delete([locator, path]);
|
|
649
|
-
await completed(transaction);
|
|
650
|
-
},
|
|
651
|
-
async fences() {
|
|
652
|
-
const transaction = authored.transaction(FENCES, 'readonly');
|
|
653
|
-
const held = await settled(transaction.objectStore(FENCES).getAll());
|
|
654
|
-
return held.map((fence) => fence.locator);
|
|
655
|
-
},
|
|
656
|
-
async erase(locators) {
|
|
657
|
-
const work = authored.transaction([FENCES, QUEUES, PENDING_CHANGES, ROOM_DRAFTS], 'readwrite');
|
|
658
|
-
for (const locator of locators) {
|
|
659
|
-
work.objectStore(FENCES).put({ locator });
|
|
660
|
-
work.objectStore(QUEUES).delete(locator);
|
|
661
|
-
for (const name of [PENDING_CHANGES, ROOM_DRAFTS]) {
|
|
662
|
-
removeRoomEntries(work.objectStore(name), locator);
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
await completed(work);
|
|
666
|
-
const transaction = database.transaction([ROOMS, DOCUMENTS, BINARY_FILES, TRANSFERS], 'readwrite');
|
|
667
|
-
for (const locator of locators) {
|
|
668
|
-
transaction.objectStore(ROOMS).delete(locator);
|
|
669
|
-
transaction.objectStore(TRANSFERS).delete(locator);
|
|
670
|
-
transaction.objectStore(DOCUMENTS).delete(underRoom(locator));
|
|
671
|
-
transaction.objectStore(BINARY_FILES).delete(underRoom(locator));
|
|
672
|
-
}
|
|
673
|
-
await completed(transaction);
|
|
674
|
-
const fences = authored.transaction(FENCES, 'readwrite');
|
|
675
|
-
for (const locator of locators)
|
|
676
|
-
fences.objectStore(FENCES).delete(locator);
|
|
677
|
-
await completed(fences);
|
|
678
|
-
},
|
|
679
|
-
async discard(locator) {
|
|
680
|
-
const transaction = authored.transaction([QUEUES, PENDING_CHANGES], 'readwrite');
|
|
681
|
-
transaction.objectStore(QUEUES).delete(locator);
|
|
682
|
-
removeRoomEntries(transaction.objectStore(PENDING_CHANGES), locator);
|
|
683
|
-
await completed(transaction);
|
|
684
|
-
},
|
|
685
|
-
async transfers() {
|
|
686
|
-
const transaction = database.transaction(TRANSFERS, 'readonly');
|
|
687
|
-
const held = await settled(transaction.objectStore(TRANSFERS).getAll());
|
|
688
|
-
return held.filter((record) => !fenced(record.locator));
|
|
360
|
+
await authored.ask({ kind: 'authored.roomDraft', locator, path, kept: null, files: [] });
|
|
689
361
|
},
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
return readLayer(transaction, record.locator, record.transfer);
|
|
693
|
-
},
|
|
694
|
-
async stage(record, page) {
|
|
695
|
-
if (fenced(record.locator))
|
|
696
|
-
return;
|
|
697
|
-
const transaction = database.transaction([TRANSFERS, DOCUMENTS, BINARY_FILES], 'readwrite');
|
|
698
|
-
writeLayer(transaction, record.locator, record.transfer, page, null);
|
|
699
|
-
transaction.objectStore(TRANSFERS).put(record);
|
|
700
|
-
await completed(transaction);
|
|
701
|
-
},
|
|
702
|
-
async abandon(locator) {
|
|
703
|
-
if (fenced(locator))
|
|
704
|
-
return;
|
|
705
|
-
const transaction = database.transaction([TRANSFERS, DOCUMENTS, BINARY_FILES], 'readwrite');
|
|
706
|
-
const transfers = transaction.objectStore(TRANSFERS);
|
|
707
|
-
const held = transfers.get(locator);
|
|
708
|
-
held.onsuccess = () => {
|
|
709
|
-
const record = held.result;
|
|
710
|
-
if (record === undefined)
|
|
711
|
-
return;
|
|
712
|
-
transfers.delete(locator);
|
|
713
|
-
const range = underTransfer(locator, record.transfer);
|
|
714
|
-
transaction.objectStore(DOCUMENTS).delete(range);
|
|
715
|
-
transaction.objectStore(BINARY_FILES).delete(range);
|
|
716
|
-
};
|
|
717
|
-
await completed(transaction);
|
|
718
|
-
},
|
|
719
|
-
async receivedHeads() {
|
|
720
|
-
const transaction = database.transaction(RECEIVED, 'readonly');
|
|
721
|
-
return settled(transaction.objectStore(RECEIVED).getAll());
|
|
722
|
-
},
|
|
723
|
-
async receivedParts(key, count) {
|
|
724
|
-
const transaction = database.transaction(RECEIVED_PARTS, 'readonly');
|
|
725
|
-
const kept = await settled(transaction
|
|
726
|
-
.objectStore(RECEIVED_PARTS)
|
|
727
|
-
.getAll(IDBKeyRange.bound([key, 0], [key, count], false, true)));
|
|
728
|
-
return kept.map((part) => part.value);
|
|
729
|
-
},
|
|
730
|
-
async keepReceived(key, head, part) {
|
|
731
|
-
const transaction = database.transaction([RECEIVED, RECEIVED_PARTS], 'readwrite');
|
|
732
|
-
transaction.objectStore(RECEIVED).put({ key, head });
|
|
733
|
-
if (part !== undefined)
|
|
734
|
-
transaction.objectStore(RECEIVED_PARTS).put({ key, ...part });
|
|
735
|
-
await completed(transaction);
|
|
736
|
-
},
|
|
737
|
-
async dropReceived(keys) {
|
|
738
|
-
const transaction = database.transaction([RECEIVED, RECEIVED_PARTS], 'readwrite');
|
|
739
|
-
for (const key of keys) {
|
|
740
|
-
transaction.objectStore(RECEIVED).delete(key);
|
|
741
|
-
transaction.objectStore(RECEIVED_PARTS).delete(IDBKeyRange.bound([key], [key, []]));
|
|
742
|
-
}
|
|
743
|
-
await completed(transaction);
|
|
362
|
+
fences() {
|
|
363
|
+
return authored.ask({ kind: 'authored.fences' });
|
|
744
364
|
},
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
const held = await settled(transaction.objectStore(FACTS).get(GENERATION));
|
|
748
|
-
return held ?? null;
|
|
365
|
+
generation() {
|
|
366
|
+
return fact('generation');
|
|
749
367
|
},
|
|
750
368
|
async rememberGeneration(value) {
|
|
751
|
-
|
|
752
|
-
transaction.objectStore(FACTS).put(value, GENERATION);
|
|
753
|
-
await completed(transaction);
|
|
369
|
+
await authored.ask({ kind: 'authored.remember', name: 'generation', value });
|
|
754
370
|
},
|
|
755
|
-
|
|
756
|
-
authored.
|
|
757
|
-
database.close();
|
|
371
|
+
file(key, type) {
|
|
372
|
+
return authored.ask({ kind: 'authored.file', key, type });
|
|
758
373
|
},
|
|
759
374
|
};
|
|
760
375
|
}
|