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