@agentshouse/mdruntime 0.1.0-c009335e08279 → 0.1.0-c0136de3edfbe
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 +25 -12
- package/dist/acceptance.d.ts +6 -0
- package/dist/acceptance.d.ts.map +1 -0
- package/dist/acceptance.js +389 -0
- package/dist/actions.d.ts +20 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/actions.js +248 -0
- package/dist/adapter.d.ts +246 -80
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +1 -9
- package/dist/attached-model.d.ts +8 -0
- package/dist/attached-model.d.ts.map +1 -0
- package/dist/attached-model.js +46 -0
- package/dist/attempt.d.ts +16 -0
- package/dist/attempt.d.ts.map +1 -0
- package/dist/attempt.js +70 -0
- package/dist/binary.d.ts +18 -0
- package/dist/binary.d.ts.map +1 -0
- package/dist/binary.js +8 -0
- package/dist/bootstrap.d.ts +40 -6
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +336 -51
- package/dist/cell.d.ts +6 -0
- package/dist/cell.d.ts.map +1 -0
- package/dist/cell.js +9 -0
- package/dist/chain.d.ts +12 -0
- package/dist/chain.d.ts.map +1 -0
- package/dist/chain.js +64 -0
- package/dist/change-set.d.ts +9 -14
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +11 -56
- package/dist/change.d.ts +46 -0
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +34 -0
- package/dist/chat-types.d.ts +3 -0
- package/dist/chat-types.d.ts.map +1 -0
- package/dist/chat-types.js +2 -0
- package/dist/client.d.ts +53 -18
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +162 -30
- package/dist/collection.d.ts +13 -14
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +52 -56
- package/dist/compile.d.ts +16 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +717 -0
- package/dist/connection.d.ts +8 -106
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +102 -2094
- package/dist/context.d.ts +2 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +7 -0
- package/dist/conversation-row.d.ts +33 -0
- package/dist/conversation-row.d.ts.map +1 -0
- package/dist/conversation-row.js +47 -0
- package/dist/conversation.d.ts +76 -27
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +1052 -273
- package/dist/digest.d.ts +7 -0
- package/dist/digest.d.ts.map +1 -0
- package/dist/digest.js +113 -0
- package/dist/discovery.d.ts +10 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +175 -0
- package/dist/durability.d.ts +10 -1
- package/dist/durability.d.ts.map +1 -1
- package/dist/durability.js +5 -0
- package/dist/entry.d.ts +20 -0
- package/dist/entry.d.ts.map +1 -0
- package/dist/entry.js +161 -0
- package/dist/expectation.d.ts +21 -0
- package/dist/expectation.d.ts.map +1 -0
- package/dist/expectation.js +70 -0
- package/dist/failure.d.ts +13 -0
- package/dist/failure.d.ts.map +1 -0
- package/dist/failure.js +19 -0
- package/dist/figures.d.ts +48 -0
- package/dist/figures.d.ts.map +1 -0
- package/dist/figures.js +197 -0
- package/dist/file.d.ts +2 -19
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +7 -122
- package/dist/host.d.ts +18 -2
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +165 -51
- package/dist/identity.d.ts +3 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +13 -1
- package/dist/index.d.ts +35 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -0
- package/dist/install.d.ts +56 -0
- package/dist/install.d.ts.map +1 -0
- package/dist/install.js +560 -0
- package/dist/job.d.ts +33 -0
- package/dist/job.d.ts.map +1 -0
- package/dist/job.js +54 -0
- package/dist/lifecycle.d.ts +7 -0
- package/dist/lifecycle.d.ts.map +1 -0
- package/dist/lifecycle.js +267 -0
- package/dist/listing.d.ts +53 -0
- package/dist/listing.d.ts.map +1 -0
- package/dist/listing.js +175 -0
- package/dist/live.d.ts +23 -0
- package/dist/live.d.ts.map +1 -0
- package/dist/live.js +165 -0
- package/dist/locks.d.ts +9 -0
- package/dist/locks.d.ts.map +1 -0
- package/dist/locks.js +61 -0
- package/dist/log.d.ts +1 -0
- package/dist/log.d.ts.map +1 -1
- package/dist/lost.d.ts +5 -0
- package/dist/lost.d.ts.map +1 -0
- package/dist/lost.js +61 -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 +30 -9
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +337 -86
- package/dist/messages.d.ts +52 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +168 -0
- package/dist/model.d.ts +17 -16
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +9 -9
- package/dist/models.d.ts +14 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +193 -0
- 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 +22 -2
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +133 -17
- package/dist/open.d.ts +2 -0
- package/dist/open.d.ts.map +1 -0
- package/dist/open.js +1 -0
- package/dist/opened.d.ts +22 -0
- package/dist/opened.d.ts.map +1 -0
- package/dist/opened.js +107 -0
- package/dist/opening.d.ts +4 -0
- package/dist/opening.d.ts.map +1 -0
- package/dist/opening.js +56 -0
- package/dist/outcome.d.ts +73 -5
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +84 -1
- package/dist/overlay.d.ts +16 -5
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +147 -180
- package/dist/parts.d.ts +59 -0
- package/dist/parts.d.ts.map +1 -0
- package/dist/parts.js +118 -0
- package/dist/paths.d.ts +1 -1
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +4 -16
- package/dist/preparation.d.ts +20 -0
- package/dist/preparation.d.ts.map +1 -0
- package/dist/preparation.js +226 -0
- package/dist/queries.d.ts +16 -0
- package/dist/queries.d.ts.map +1 -0
- package/dist/queries.js +272 -0
- package/dist/query.d.ts +56 -180
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +3 -422
- package/dist/questions.d.ts +32 -0
- package/dist/questions.d.ts.map +1 -0
- package/dist/questions.js +118 -0
- package/dist/reach.d.ts +56 -0
- package/dist/reach.d.ts.map +1 -0
- package/dist/reach.js +494 -0
- package/dist/react.d.ts +47 -0
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +54 -0
- package/dist/read.d.ts +7 -0
- package/dist/read.d.ts.map +1 -0
- package/dist/read.js +17 -0
- package/dist/readiness.d.ts +50 -0
- package/dist/readiness.d.ts.map +1 -0
- package/dist/readiness.js +27 -0
- package/dist/reads.d.ts +36 -0
- package/dist/reads.d.ts.map +1 -0
- package/dist/reads.js +136 -0
- package/dist/recovery.d.ts +26 -0
- package/dist/recovery.d.ts.map +1 -0
- package/dist/recovery.js +232 -0
- package/dist/refresh.d.ts +21 -7
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +155 -55
- package/dist/refusal.d.ts +5 -0
- package/dist/refusal.d.ts.map +1 -0
- package/dist/refusal.js +56 -0
- package/dist/replica.d.ts +58 -33
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +355 -158
- package/dist/room-draft.d.ts +62 -11
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +281 -83
- package/dist/room-drafts.d.ts +24 -0
- package/dist/room-drafts.d.ts.map +1 -0
- package/dist/room-drafts.js +216 -0
- package/dist/rows.d.ts +32 -34
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +65 -87
- package/dist/runtime.d.ts +42 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +1 -0
- package/dist/save.d.ts +74 -6
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +902 -182
- package/dist/saves.d.ts +70 -0
- package/dist/saves.d.ts.map +1 -0
- package/dist/saves.js +1004 -0
- package/dist/search.d.ts +15 -54
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +10 -121
- package/dist/seed.d.ts +25 -0
- package/dist/seed.d.ts.map +1 -0
- package/dist/seed.js +146 -0
- package/dist/shared.d.ts +15 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +421 -0
- package/dist/shown.d.ts +31 -0
- package/dist/shown.d.ts.map +1 -0
- package/dist/shown.js +294 -0
- package/dist/socket/admission.d.ts +7 -0
- package/dist/socket/admission.d.ts.map +1 -0
- package/dist/socket/admission.js +39 -0
- package/dist/socket/answers.d.ts +6 -0
- package/dist/socket/answers.d.ts.map +1 -0
- package/dist/socket/answers.js +20 -0
- package/dist/socket/lanes.d.ts +20 -0
- package/dist/socket/lanes.d.ts.map +1 -0
- package/dist/socket/lanes.js +77 -0
- package/dist/socket/reads.d.ts +10 -0
- package/dist/socket/reads.d.ts.map +1 -0
- package/dist/socket/reads.js +31 -0
- package/dist/socket/rooms.d.ts +43 -0
- package/dist/socket/rooms.d.ts.map +1 -0
- package/dist/socket/rooms.js +90 -0
- package/dist/socket/session.d.ts +39 -0
- package/dist/socket/session.d.ts.map +1 -0
- package/dist/socket/session.js +23 -0
- package/dist/socket.d.ts +70 -0
- package/dist/socket.d.ts.map +1 -0
- package/dist/socket.js +175 -0
- package/dist/source.d.ts +61 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +81 -0
- package/dist/status.d.ts +7 -4
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +3 -15
- package/dist/store/fences.d.ts +16 -0
- package/dist/store/fences.d.ts.map +1 -0
- package/dist/store/fences.js +26 -0
- package/dist/store/installs.d.ts +30 -0
- package/dist/store/installs.d.ts.map +1 -0
- package/dist/store/installs.js +34 -0
- package/dist/store/readiness.d.ts +10 -0
- package/dist/store/readiness.d.ts.map +1 -0
- package/dist/store/readiness.js +52 -0
- package/dist/store/records.d.ts +212 -0
- package/dist/store/records.d.ts.map +1 -0
- package/dist/store/records.js +408 -0
- package/dist/store/room-held.d.ts +8 -0
- package/dist/store/room-held.d.ts.map +1 -0
- package/dist/store/room-held.js +17 -0
- package/dist/store/space.d.ts +11 -0
- package/dist/store/space.d.ts.map +1 -0
- package/dist/store/space.js +43 -0
- package/dist/store/watchers.d.ts +17 -0
- package/dist/store/watchers.d.ts.map +1 -0
- package/dist/store/watchers.js +57 -0
- package/dist/store/writes.d.ts +24 -0
- package/dist/store/writes.d.ts.map +1 -0
- package/dist/store/writes.js +125 -0
- package/dist/store.d.ts +106 -93
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +142 -385
- package/dist/stored.d.ts +156 -0
- package/dist/stored.d.ts.map +1 -0
- package/dist/stored.js +637 -0
- package/dist/surface.d.ts +86 -0
- package/dist/surface.d.ts.map +1 -0
- package/dist/surface.js +217 -0
- package/dist/sync.d.ts +20 -0
- package/dist/sync.d.ts.map +1 -0
- package/dist/sync.js +531 -0
- package/dist/tables.d.ts +407 -0
- package/dist/tables.d.ts.map +1 -0
- package/dist/tables.js +724 -0
- package/dist/tallies.d.ts +18 -0
- package/dist/tallies.d.ts.map +1 -0
- package/dist/tallies.js +129 -0
- package/dist/transfer.d.ts +0 -5
- package/dist/transfer.d.ts.map +1 -1
- package/dist/turn.d.ts +9 -0
- package/dist/turn.d.ts.map +1 -0
- package/dist/turn.js +75 -0
- package/dist/validation.d.ts +29 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +104 -0
- package/dist/windows.d.ts +11 -0
- package/dist/windows.d.ts.map +1 -0
- package/dist/windows.js +16 -0
- package/dist/wire.d.ts +206 -20
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +95 -61
- package/package.json +33 -10
- package/AGENTS.md +0 -208
- package/dist/attachment.d.ts +0 -3
- package/dist/attachment.d.ts.map +0 -1
- package/dist/attachment.js +0 -8
- package/dist/read-side.d.ts +0 -14
- package/dist/read-side.d.ts.map +0 -1
- package/dist/read-side.js +0 -371
package/dist/connection.js
CHANGED
|
@@ -1,2105 +1,113 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { changeSet, mergeChangeSets } from './change-set.js';
|
|
10
|
-
import { createConversationStore, } from './conversation.js';
|
|
11
|
-
import { MD_MODEL, parseCollection, validRoot } from './collection.js';
|
|
1
|
+
import { submitLater, unpair } from './acceptance.js';
|
|
2
|
+
import { invokeAction, called, readConversationHistory, resendLater } from './actions.js';
|
|
3
|
+
import { builtInModel, roomTypesOf } from './attached-model.js';
|
|
4
|
+
import { createConversationStore } from './conversation.js';
|
|
5
|
+
import { refusedForSpace } from './durability.js';
|
|
6
|
+
import { createFigures } from './figures.js';
|
|
7
|
+
import { scheduleCurrency, spaceFreed, start } from './lifecycle.js';
|
|
8
|
+
import { createLiveQueries } from './live.js';
|
|
12
9
|
import { createLog } from './log.js';
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const
|
|
26
|
-
const knownCapabilities = new Set(VERSION_1_CAPABILITIES);
|
|
27
|
-
function within(root, path) {
|
|
28
|
-
return root === '' || beneath(root, path);
|
|
29
|
-
}
|
|
30
|
-
function declaredIdentity(model) {
|
|
31
|
-
if (model === undefined)
|
|
32
|
-
return false;
|
|
33
|
-
if (model === null)
|
|
34
|
-
return true;
|
|
35
|
-
return (typeof model.identity === 'string' &&
|
|
36
|
-
model.identity.length > 0 &&
|
|
37
|
-
typeof model.version === 'string' &&
|
|
38
|
-
model.version.length > 0);
|
|
39
|
-
}
|
|
40
|
-
function declaredSelection(room) {
|
|
41
|
-
const selection = room.typedSelection;
|
|
42
|
-
if (selection === undefined)
|
|
43
|
-
return true;
|
|
44
|
-
if (!Array.isArray(selection.roots) || selection.roots.length === 0)
|
|
45
|
-
return false;
|
|
46
|
-
if (!declaredIdentity(selection.attachedModel))
|
|
47
|
-
return false;
|
|
48
|
-
return selection.roots.every((root, index) => validRoot(root) &&
|
|
49
|
-
root !== '' &&
|
|
50
|
-
selection.roots.every((other, at) => at === index || !within(other, root)));
|
|
51
|
-
}
|
|
10
|
+
import { scoped } from './models.js';
|
|
11
|
+
import { open } from './opening.js';
|
|
12
|
+
import { deliverOwedReadiness, filled } from './preparation.js';
|
|
13
|
+
import { answeredPlan, figuresCover, roomTallies } from './queries.js';
|
|
14
|
+
import { readAnswers } from './reads.js';
|
|
15
|
+
import { createRecovery } from './recovery.js';
|
|
16
|
+
import { settle } from './saves.js';
|
|
17
|
+
import { publishChangeSet, show } from './shown.js';
|
|
18
|
+
import { createSocket } from './socket.js';
|
|
19
|
+
import { createStore } from './store.js';
|
|
20
|
+
import { composedRuntime } from './surface.js';
|
|
21
|
+
import { advanced, withhold } from './sync.js';
|
|
22
|
+
const ROOM_ATTEMPTS = 16;
|
|
52
23
|
export function composeRuntime(options) {
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const replicas = new Map();
|
|
58
|
-
const pendings = new Map();
|
|
59
|
-
const roomDrafts = new Map();
|
|
60
|
-
const presentations = new Map();
|
|
61
|
-
const wanted = new Map();
|
|
62
|
-
let limits = [];
|
|
63
|
-
let capabilities = [];
|
|
64
|
-
function rewriting() {
|
|
65
|
-
return capabilities.some((capability) => capability.name === 'move-reference-rewrites')
|
|
66
|
-
? 'provider'
|
|
67
|
-
: 'runtime';
|
|
68
|
-
}
|
|
69
|
-
const rooms = new Map();
|
|
70
|
-
const unreadable = new Set();
|
|
71
|
-
const models = new Map();
|
|
72
|
-
const liveModels = new Map();
|
|
73
|
-
const invalidModels = new Set();
|
|
74
|
-
const modelEpoch = new Map();
|
|
75
|
-
const discovered = new Map();
|
|
76
|
-
let roomsListed = false;
|
|
77
|
-
const scopedRefusals = new Map();
|
|
78
|
-
const sourceRefusals = new Map();
|
|
79
|
-
const declarationRefusals = new Set();
|
|
80
|
-
function modelKey(locator, attached) {
|
|
81
|
-
return `${attached} ${locator}`;
|
|
82
|
-
}
|
|
83
|
-
function modelOf(locator, attached = 'collection') {
|
|
84
|
-
return models.get(modelKey(locator, attached)) ?? NO_MODEL;
|
|
85
|
-
}
|
|
86
|
-
function savedModel(locator, attached) {
|
|
87
|
-
return invalidModels.has(modelKey(locator, attached))
|
|
88
|
-
? { status: 'unavailable', reason: 'revoked' }
|
|
89
|
-
: modelOf(locator, attached);
|
|
90
|
-
}
|
|
91
|
-
function compositionFor(record) {
|
|
92
|
-
return compositionOf({
|
|
93
|
-
...record,
|
|
94
|
-
model: savedModel(record.locator, 'collection'),
|
|
95
|
-
selectionModel: savedModel(record.locator, 'typed-selection'),
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
let refusal = null;
|
|
99
|
-
let storageLoss = null;
|
|
100
|
-
let status = STARTING;
|
|
101
|
-
let shown = STARTING;
|
|
102
|
-
let discoveryPaused = false;
|
|
103
|
-
let timer;
|
|
104
|
-
let closed = false;
|
|
105
|
-
let stopping = false;
|
|
106
|
-
let withdrawNotifications;
|
|
107
|
-
let handed;
|
|
108
|
-
const notices = new Map();
|
|
109
|
-
let chain = Promise.resolve();
|
|
110
|
-
let mutations = Promise.resolve();
|
|
111
|
-
let inflight = false;
|
|
112
|
-
let queued = false;
|
|
113
|
-
const roomListeners = new Set();
|
|
114
|
-
const refusalListeners = new Set();
|
|
115
|
-
const storageLossListeners = new Set();
|
|
116
|
-
const statusListeners = new Set();
|
|
117
|
-
const queueSubscriptions = new Set();
|
|
118
|
-
const roomDraftListeners = new Set();
|
|
119
|
-
const presentationListeners = new Set();
|
|
120
|
-
const readSide = createReadSide();
|
|
121
|
-
let withdrawConversations;
|
|
122
|
-
let currencyQueued = false;
|
|
123
|
-
let serviceMode = 'open';
|
|
124
|
-
let haltedRefusal = false;
|
|
125
|
-
let openSignals = 0;
|
|
126
|
-
const serviceModeListeners = new Set();
|
|
127
|
-
const conversations = createConversationStore({
|
|
128
|
-
cards: declaredCards,
|
|
129
|
-
documents: (locator) => readSide.query(planOf({ rooms: [locator], select: 'all' }, roomFacts())),
|
|
130
|
-
call: (name, input) => invokeAction(name, input),
|
|
131
|
-
durably,
|
|
132
|
-
stream: () => options.adapter.conversations,
|
|
133
|
-
publish: publishChangeSet,
|
|
134
|
-
closed: () => closed,
|
|
135
|
-
});
|
|
136
|
-
const searchIndex = createSearchIndex({
|
|
137
|
-
documents: () => readSide.documents(),
|
|
138
|
-
document: (room, path) => readSide.document(room, path),
|
|
139
|
-
messages: () => conversations.messages(),
|
|
140
|
-
message: (conversation, identity) => conversations.message(conversation, identity),
|
|
141
|
-
});
|
|
24
|
+
const { adapter, shell } = options;
|
|
25
|
+
const roomTypes = roomTypesOf(adapter.roomTypes, adapter.nameableRoomTypes);
|
|
26
|
+
const builtIn = builtInModel(roomTypes);
|
|
27
|
+
const typedPaths = adapter.typedPaths ?? [];
|
|
142
28
|
const journal = createLog();
|
|
143
|
-
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
if (room === undefined)
|
|
155
|
-
return;
|
|
156
|
-
room.delete(path);
|
|
157
|
-
if (room.size === 0)
|
|
158
|
-
roomDrafts.delete(locator);
|
|
159
|
-
}
|
|
160
|
-
function roomDraftOf(record) {
|
|
161
|
-
return structuredClone({
|
|
162
|
-
room: record.locator,
|
|
163
|
-
path: record.path,
|
|
164
|
-
revision: record.revision,
|
|
165
|
-
parts: record.parts,
|
|
166
|
-
changedUnderneath: underneath(record.parts, record.path, replicas.get(record.locator)?.visible),
|
|
167
|
-
returned: record.returned,
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
function snapshotRoomDrafts() {
|
|
171
|
-
const records = [...roomDrafts.values()].flatMap((room) => [...room.values()]);
|
|
172
|
-
records.sort((left, right) => {
|
|
173
|
-
if (left.locator !== right.locator)
|
|
174
|
-
return left.locator < right.locator ? -1 : 1;
|
|
175
|
-
if (left.path !== right.path)
|
|
176
|
-
return left.path < right.path ? -1 : 1;
|
|
177
|
-
return 0;
|
|
178
|
-
});
|
|
179
|
-
return records.map(roomDraftOf);
|
|
180
|
-
}
|
|
181
|
-
function publishRoomDrafts(waitingOnly) {
|
|
182
|
-
if (closed)
|
|
183
|
-
return;
|
|
184
|
-
const value = snapshotRoomDrafts();
|
|
185
|
-
for (const listener of roomDraftListeners) {
|
|
186
|
-
if (waitingOnly && listener.ready)
|
|
187
|
-
continue;
|
|
188
|
-
listener.ready = true;
|
|
189
|
-
listener.notify(value);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
function presentationOf(app) {
|
|
193
|
-
const value = presentations.get(app);
|
|
194
|
-
return value === undefined ? null : structuredClone(value);
|
|
195
|
-
}
|
|
196
|
-
function emitPresentation(app) {
|
|
197
|
-
if (closed)
|
|
198
|
-
return;
|
|
199
|
-
for (const listener of presentationListeners) {
|
|
200
|
-
if (listener.app === app && listener.ready)
|
|
201
|
-
listener.notify(presentationOf(app));
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
function pending() {
|
|
205
|
-
return [...replicas.values()].some((replica) => replica.queue.length > 0);
|
|
206
|
-
}
|
|
207
|
-
function attention() {
|
|
208
|
-
const attentionRequired = [];
|
|
209
|
-
for (const record of [...roomDrafts.values()].flatMap((room) => [...room.values()])) {
|
|
210
|
-
if (record.returned === null)
|
|
211
|
-
continue;
|
|
212
|
-
attentionRequired.push({
|
|
213
|
-
room: record.locator,
|
|
214
|
-
path: record.path,
|
|
215
|
-
returned: structuredClone(record.returned),
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
attentionRequired.sort((left, right) => {
|
|
219
|
-
if (left.room !== right.room)
|
|
220
|
-
return left.room < right.room ? -1 : 1;
|
|
221
|
-
if (left.path !== right.path)
|
|
222
|
-
return left.path < right.path ? -1 : 1;
|
|
223
|
-
return 0;
|
|
224
|
-
});
|
|
225
|
-
let reset = null;
|
|
226
|
-
for (const { submission } of pendings.values()) {
|
|
227
|
-
if (submission.state !== 'reset')
|
|
228
|
-
continue;
|
|
229
|
-
if (reset === null || submission.reset < reset)
|
|
230
|
-
reset = submission.reset;
|
|
231
|
-
}
|
|
232
|
-
return { attentionRequired, reset };
|
|
233
|
-
}
|
|
234
|
-
function listed() {
|
|
235
|
-
return structuredClone([...rooms.values()]);
|
|
236
|
-
}
|
|
237
|
-
function roomFacts() {
|
|
238
|
-
const held = [...rooms.values()].map((room) => {
|
|
239
|
-
const attached = [
|
|
240
|
-
modelOf(room.locator),
|
|
241
|
-
...(room.typedSelection === null ? [] : [modelOf(room.locator, 'typed-selection')]),
|
|
242
|
-
];
|
|
243
|
-
return {
|
|
244
|
-
locator: room.locator,
|
|
245
|
-
available: attached.every((model) => model.status !== 'unavailable'),
|
|
246
|
-
types: attached.flatMap((model) => model.status === 'unavailable' ? [] : Object.values(model.types)),
|
|
247
|
-
};
|
|
248
|
-
});
|
|
249
|
-
return { listed: roomsListed, rooms: held };
|
|
250
|
-
}
|
|
251
|
-
function emitRooms() {
|
|
252
|
-
const value = listed();
|
|
253
|
-
for (const listener of roomListeners)
|
|
254
|
-
listener(value);
|
|
255
|
-
}
|
|
256
|
-
function emitRefusal() {
|
|
257
|
-
for (const listener of refusalListeners)
|
|
258
|
-
listener(refusal);
|
|
259
|
-
}
|
|
260
|
-
function emitStorageLoss() {
|
|
261
|
-
for (const listener of storageLossListeners)
|
|
262
|
-
listener(storageLoss);
|
|
263
|
-
}
|
|
264
|
-
function moveTo(next) {
|
|
265
|
-
if (closed)
|
|
266
|
-
return;
|
|
267
|
-
status = next;
|
|
268
|
-
show();
|
|
269
|
-
}
|
|
270
|
-
function show() {
|
|
271
|
-
if (closed)
|
|
272
|
-
return;
|
|
273
|
-
const next = status.state === 'synced' && !conversations.current()
|
|
274
|
-
? { ...status, state: 'syncing' }
|
|
275
|
-
: status;
|
|
276
|
-
if (same(shown, next))
|
|
277
|
-
return;
|
|
278
|
-
shown = next;
|
|
279
|
-
for (const listener of statusListeners)
|
|
280
|
-
listener(shown);
|
|
281
|
-
}
|
|
282
|
-
function entryOf(batch) {
|
|
283
|
-
const state = pendings.get(batch.id);
|
|
284
|
-
if (state?.accepted !== undefined) {
|
|
285
|
-
return Object.freeze({ batch, state: 'accepted', roomVersion: state.accepted });
|
|
286
|
-
}
|
|
287
|
-
return Object.freeze({ batch, ...(state?.submission ?? { state: 'waiting' }) });
|
|
288
|
-
}
|
|
289
|
-
function entriesOf(locator) {
|
|
290
|
-
const replica = replicas.get(locator);
|
|
291
|
-
if (replica === undefined)
|
|
292
|
-
return [];
|
|
293
|
-
return replica.queue.map(entryOf);
|
|
294
|
-
}
|
|
295
|
-
function emitQueue(locator) {
|
|
296
|
-
const entries = entriesOf(locator);
|
|
297
|
-
for (const subscription of queueSubscriptions) {
|
|
298
|
-
if (subscription.locator === locator)
|
|
299
|
-
subscription.notify(entries);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
function applyChangeSet(set) {
|
|
303
|
-
readSide.apply(set);
|
|
304
|
-
searchIndex.changed(set);
|
|
305
|
-
}
|
|
306
|
-
function publishChangeSet(set) {
|
|
307
|
-
applyChangeSet(set);
|
|
308
|
-
if (set.rooms.length > 0)
|
|
309
|
-
emitRooms();
|
|
310
|
-
for (const locator of set.queues)
|
|
311
|
-
emitQueue(locator);
|
|
312
|
-
if (set.drafts.length > 0 ||
|
|
313
|
-
set.documents.some((change) => roomDrafts.get(change.locator)?.has(change.path) === true)) {
|
|
314
|
-
publishRoomDrafts(false);
|
|
315
|
-
}
|
|
316
|
-
for (const app of set.presentations)
|
|
317
|
-
emitPresentation(app);
|
|
318
|
-
conversations.changed(set);
|
|
319
|
-
const cards = declaredCards();
|
|
320
|
-
if (cards !== undefined && set.documents.some((change) => change.locator === cards.locator)) {
|
|
321
|
-
scheduleCurrency();
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
function stopTimer() {
|
|
325
|
-
if (timer === undefined)
|
|
326
|
-
return;
|
|
327
|
-
clearInterval(timer);
|
|
328
|
-
timer = undefined;
|
|
329
|
-
}
|
|
330
|
-
function startTimer() {
|
|
331
|
-
if (timer !== undefined)
|
|
332
|
-
return;
|
|
333
|
-
timer = setInterval(() => {
|
|
334
|
-
void enqueue();
|
|
335
|
-
}, period);
|
|
336
|
-
}
|
|
337
|
-
async function refuse(reason, provider = null) {
|
|
338
|
-
stopTimer();
|
|
339
|
-
discoveryPaused = true;
|
|
340
|
-
try {
|
|
341
|
-
if (reason === 'revoked-authorization') {
|
|
342
|
-
await exclusive(async () => {
|
|
343
|
-
const revoked = { status: 'unavailable', reason: 'revoked' };
|
|
344
|
-
try {
|
|
345
|
-
const local = await openLocal();
|
|
346
|
-
await local.revokeModels([...replicas.values()].map((replica) => ({
|
|
347
|
-
record: replica.confirmed.record,
|
|
348
|
-
queue: replica.queue,
|
|
349
|
-
})));
|
|
350
|
-
}
|
|
351
|
-
catch {
|
|
352
|
-
journal.record('model revocation persistence failed');
|
|
353
|
-
}
|
|
354
|
-
const keys = new Set(models.keys());
|
|
355
|
-
for (const locator of rooms.keys()) {
|
|
356
|
-
keys.add(modelKey(locator, 'collection'));
|
|
357
|
-
keys.add(modelKey(locator, 'typed-selection'));
|
|
358
|
-
}
|
|
359
|
-
for (const key of keys) {
|
|
360
|
-
invalidModels.add(key);
|
|
361
|
-
modelEpoch.set(key, (modelEpoch.get(key) ?? 0) + 1);
|
|
362
|
-
models.set(key, revoked);
|
|
363
|
-
liveModels.delete(key);
|
|
364
|
-
}
|
|
365
|
-
const changed = [];
|
|
366
|
-
for (const [locator, room] of rooms) {
|
|
367
|
-
const before = replicas.get(locator) ?? null;
|
|
368
|
-
if (before !== null) {
|
|
369
|
-
const record = {
|
|
370
|
-
...before.confirmed.record,
|
|
371
|
-
model: revoked,
|
|
372
|
-
selectionModel: revoked,
|
|
373
|
-
};
|
|
374
|
-
const read = readConfirmed(record, before.confirmed);
|
|
375
|
-
replicas.set(locator, read.kind === 'staged'
|
|
376
|
-
? replicaOf(read.confirmed, before.queue, rewriting(), before)
|
|
377
|
-
: { ...before, confirmed: { ...before.confirmed, record } });
|
|
378
|
-
}
|
|
379
|
-
rooms.set(locator, {
|
|
380
|
-
...room,
|
|
381
|
-
model: modelTypes(revoked),
|
|
382
|
-
typedSelection: room.typedSelection === null
|
|
383
|
-
? null
|
|
384
|
-
: { ...room.typedSelection, model: modelTypes(revoked) },
|
|
385
|
-
});
|
|
386
|
-
changed.push(changeSet(locator, before, replicas.get(locator) ?? null, { room: true }));
|
|
387
|
-
}
|
|
388
|
-
publishChangeSet(mergeChangeSets(changed));
|
|
389
|
-
});
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
finally {
|
|
393
|
-
refusal = {
|
|
394
|
-
reason,
|
|
395
|
-
reauthorizationOffered: reason === 'revoked-authorization',
|
|
396
|
-
refusal: provider,
|
|
397
|
-
};
|
|
398
|
-
journal.record(`connection refused ${reason}`);
|
|
399
|
-
emitRefusal();
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
function list(room, synchronizationAvailable) {
|
|
403
|
-
const selection = room.typedSelection ?? null;
|
|
404
|
-
rooms.set(room.locator, {
|
|
405
|
-
name: room.name,
|
|
406
|
-
locator: room.locator,
|
|
407
|
-
operations: room.operations,
|
|
408
|
-
replica: room.replica,
|
|
409
|
-
attachedModel: room.attachedModel,
|
|
410
|
-
model: modelTypes(modelOf(room.locator)),
|
|
411
|
-
typedSelection: selection === null
|
|
412
|
-
? null
|
|
413
|
-
: {
|
|
414
|
-
roots: selection.roots,
|
|
415
|
-
attachedModel: selection.attachedModel,
|
|
416
|
-
model: modelTypes(modelOf(room.locator, 'typed-selection')),
|
|
417
|
-
},
|
|
418
|
-
providerOwnedPaths: room.providerOwnedPaths,
|
|
419
|
-
roomVersion: replicas.get(room.locator)?.confirmed.record.roomVersion ?? null,
|
|
420
|
-
attachments: replicas.get(room.locator)?.confirmed.record.attachments ?? [],
|
|
421
|
-
synchronizationAvailable,
|
|
422
|
-
refusal: scopedRefusals.get(room.locator) ?? sourceRefusals.get(room.locator) ?? null,
|
|
423
|
-
});
|
|
424
|
-
}
|
|
425
|
-
async function applyDiscovery(listed, local) {
|
|
426
|
-
const listedLocators = new Set(listed.map((room) => room.locator));
|
|
427
|
-
for (const locator of listedLocators)
|
|
428
|
-
scopedRefusals.delete(locator);
|
|
429
|
-
discovered.clear();
|
|
430
|
-
for (const room of listed)
|
|
431
|
-
discovered.set(room.locator, room);
|
|
432
|
-
for (const locator of wanted.keys())
|
|
433
|
-
if (!listedLocators.has(locator))
|
|
434
|
-
wanted.delete(locator);
|
|
435
|
-
await dropUnlisted(listedLocators, local);
|
|
436
|
-
for (const room of listed) {
|
|
437
|
-
const held = replicas.get(room.locator)?.confirmed.record;
|
|
438
|
-
if (held !== undefined && held.replica.selection !== room.replica.selection) {
|
|
439
|
-
list(held, false);
|
|
440
|
-
continue;
|
|
441
|
-
}
|
|
442
|
-
list(room, room.replica.admission === 'admitted' && !unreadable.has(room.locator));
|
|
443
|
-
}
|
|
444
|
-
roomsListed = true;
|
|
445
|
-
emitRooms();
|
|
446
|
-
}
|
|
447
|
-
function unlistedHeld(listed) {
|
|
448
|
-
return [...new Set([...rooms.keys(), ...roomDrafts.keys()])].filter((locator) => !listed.has(locator));
|
|
449
|
-
}
|
|
450
|
-
async function dropUnlisted(listed, local) {
|
|
451
|
-
if (unlistedHeld(listed).length === 0)
|
|
452
|
-
return;
|
|
453
|
-
await exclusive(async () => {
|
|
454
|
-
const unlisted = unlistedHeld(listed);
|
|
455
|
-
await local.dropRooms(unlisted);
|
|
456
|
-
const removed = unlisted.map((locator) => changeSet(locator, replicas.get(locator) ?? null, null, {
|
|
457
|
-
room: true,
|
|
458
|
-
queue: true,
|
|
459
|
-
drafts: [...(roomDrafts.get(locator)?.keys() ?? [])],
|
|
460
|
-
}));
|
|
461
|
-
for (const locator of unlisted) {
|
|
462
|
-
for (const batch of replicas.get(locator)?.queue ?? [])
|
|
463
|
-
pendings.delete(batch.id);
|
|
464
|
-
replicas.delete(locator);
|
|
465
|
-
rooms.delete(locator);
|
|
466
|
-
roomDrafts.delete(locator);
|
|
467
|
-
unreadable.delete(locator);
|
|
468
|
-
scopedRefusals.delete(locator);
|
|
469
|
-
sourceRefusals.delete(locator);
|
|
470
|
-
declarationRefusals.delete(locator);
|
|
471
|
-
for (const attached of ['collection', 'typed-selection']) {
|
|
472
|
-
const key = modelKey(locator, attached);
|
|
473
|
-
models.delete(key);
|
|
474
|
-
liveModels.delete(key);
|
|
475
|
-
invalidModels.delete(key);
|
|
476
|
-
modelEpoch.delete(key);
|
|
29
|
+
const socket = createSocket(adapter, journal, options.authorization);
|
|
30
|
+
const store = createStore({
|
|
31
|
+
database: {
|
|
32
|
+
async ask(request) {
|
|
33
|
+
try {
|
|
34
|
+
return await options.database.ask(request);
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
if (refusedForSpace(error))
|
|
38
|
+
filled(runtime);
|
|
39
|
+
throw error;
|
|
477
40
|
}
|
|
478
|
-
journal.record(`room unlisted ${locator}`);
|
|
479
|
-
}
|
|
480
|
-
publishChangeSet(mergeChangeSets(removed));
|
|
481
|
-
moveTo({ ...status, ...attention() });
|
|
482
|
-
});
|
|
483
|
-
}
|
|
484
|
-
function restored(key, persisted) {
|
|
485
|
-
const prior = models.get(key);
|
|
486
|
-
if (prior !== undefined &&
|
|
487
|
-
prior.status !== 'unavailable' &&
|
|
488
|
-
(persisted.status === 'unavailable' || !sameModel(prior.snapshot, persisted.snapshot))) {
|
|
489
|
-
modelEpoch.set(key, (modelEpoch.get(key) ?? 0) + 1);
|
|
490
|
-
invalidModels.add(key);
|
|
491
|
-
}
|
|
492
|
-
const model = persisted.status === 'unavailable' ||
|
|
493
|
-
liveModels.get(key) === JSON.stringify(persisted.snapshot)
|
|
494
|
-
? persisted
|
|
495
|
-
: cached(persisted);
|
|
496
|
-
models.set(key, model);
|
|
497
|
-
if (model.status === 'unavailable') {
|
|
498
|
-
liveModels.delete(key);
|
|
499
|
-
invalidModels.delete(key);
|
|
500
|
-
}
|
|
501
|
-
return model;
|
|
502
|
-
}
|
|
503
|
-
async function hydrate(opened) {
|
|
504
|
-
const [records, queues, kept, presented, held, chatDrafts, lastCompleted, declared, named] = await Promise.all([
|
|
505
|
-
opened.all(),
|
|
506
|
-
opened.queues(),
|
|
507
|
-
opened.roomDrafts(),
|
|
508
|
-
opened.presentation(),
|
|
509
|
-
opened.conversations(),
|
|
510
|
-
opened.chatDrafts(),
|
|
511
|
-
opened.lastCompleted(),
|
|
512
|
-
opened.providerLimits(),
|
|
513
|
-
opened.capabilities(),
|
|
514
|
-
]);
|
|
515
|
-
limits = declared;
|
|
516
|
-
capabilities = named;
|
|
517
|
-
roomDrafts.clear();
|
|
518
|
-
for (const record of kept)
|
|
519
|
-
rememberRoomDraft(record);
|
|
520
|
-
presentations.clear();
|
|
521
|
-
for (const record of presented)
|
|
522
|
-
presentations.set(record.app, record.value);
|
|
523
|
-
conversations.hydrate(held, chatDrafts);
|
|
524
|
-
const queuedBatches = new Map(queues.map((queue) => [queue.locator, queue.pending.map(({ batch }) => batch)]));
|
|
525
|
-
pendings.clear();
|
|
526
|
-
for (const queue of queues) {
|
|
527
|
-
for (const { batch, state } of queue.pending)
|
|
528
|
-
pendings.set(batch.id, state);
|
|
529
|
-
}
|
|
530
|
-
const previousReplicas = new Map(replicas);
|
|
531
|
-
replicas.clear();
|
|
532
|
-
for (const record of records) {
|
|
533
|
-
const model = restored(modelKey(record.locator, 'collection'), record.model);
|
|
534
|
-
const selectionModel = restored(modelKey(record.locator, 'typed-selection'), record.selectionModel);
|
|
535
|
-
const prior = previousReplicas.get(record.locator);
|
|
536
|
-
const retyped = { ...record, model, selectionModel };
|
|
537
|
-
const read = readConfirmed(retyped, prior?.confirmed);
|
|
538
|
-
const confirmed = read.kind === 'staged'
|
|
539
|
-
? read.confirmed
|
|
540
|
-
: {
|
|
541
|
-
record: retyped,
|
|
542
|
-
collection: parseCollection(retyped.documents, compositionOf(retyped)),
|
|
543
|
-
};
|
|
544
|
-
replicas.set(record.locator, replicaOf(confirmed, queuedBatches.get(record.locator) ?? [], rewriting(), prior));
|
|
545
|
-
const existing = rooms.get(record.locator);
|
|
546
|
-
if (existing === undefined)
|
|
547
|
-
list(record, false);
|
|
548
|
-
else {
|
|
549
|
-
rooms.set(record.locator, {
|
|
550
|
-
...existing,
|
|
551
|
-
roomVersion: record.roomVersion,
|
|
552
|
-
attachments: record.attachments,
|
|
553
|
-
operations: record.operations,
|
|
554
|
-
replica: record.replica,
|
|
555
|
-
attachedModel: record.attachedModel,
|
|
556
|
-
model: modelTypes(model),
|
|
557
|
-
typedSelection: record.typedSelection === null
|
|
558
|
-
? null
|
|
559
|
-
: {
|
|
560
|
-
roots: record.typedSelection.roots,
|
|
561
|
-
attachedModel: record.typedSelection.attachedModel,
|
|
562
|
-
model: modelTypes(selectionModel),
|
|
563
|
-
},
|
|
564
|
-
});
|
|
565
|
-
}
|
|
566
|
-
if (read.kind === 'refused') {
|
|
567
|
-
refusedSource(record, read);
|
|
568
|
-
declarationRefusals.add(record.locator);
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
return {
|
|
572
|
-
lastCompleted,
|
|
573
|
-
locators: new Set([...queuedBatches.keys(), ...replicas.keys()]),
|
|
574
|
-
read: mergeChangeSets([...new Set([...previousReplicas.keys(), ...replicas.keys()])].map((locator) => changeSet(locator, previousReplicas.get(locator) ?? null, replicas.get(locator) ?? null))),
|
|
575
|
-
};
|
|
576
|
-
}
|
|
577
|
-
async function readState(opened) {
|
|
578
|
-
const { lastCompleted, locators, read } = await hydrate(opened);
|
|
579
|
-
applyChangeSet(read);
|
|
580
|
-
if (replicas.size > 0)
|
|
581
|
-
emitRooms();
|
|
582
|
-
for (const locator of locators)
|
|
583
|
-
emitQueue(locator);
|
|
584
|
-
publishRoomDrafts(false);
|
|
585
|
-
return lastCompleted;
|
|
586
|
-
}
|
|
587
|
-
async function open() {
|
|
588
|
-
const opened = await openRuntimeStore();
|
|
589
|
-
store = opened;
|
|
590
|
-
const kept = options.shell.generation;
|
|
591
|
-
const storedGeneration = await opened.generation();
|
|
592
|
-
const loss = detectStorageLoss(storedGeneration, kept);
|
|
593
|
-
const generation = nextGeneration(storedGeneration, kept);
|
|
594
|
-
if (generation !== storedGeneration)
|
|
595
|
-
await opened.rememberGeneration(generation);
|
|
596
|
-
if (generation !== kept)
|
|
597
|
-
await options.shell.keepGeneration(generation);
|
|
598
|
-
if (loss !== null) {
|
|
599
|
-
storageLoss = loss;
|
|
600
|
-
emitStorageLoss();
|
|
601
|
-
}
|
|
602
|
-
const lastCompleted = await readState(opened);
|
|
603
|
-
moveTo({ ...status, lastCompleted, ...attention() });
|
|
604
|
-
return opened;
|
|
605
|
-
}
|
|
606
|
-
function durably(work) {
|
|
607
|
-
return exclusive(async () => {
|
|
608
|
-
const local = await openLocal();
|
|
609
|
-
return work(local);
|
|
610
|
-
});
|
|
611
|
-
}
|
|
612
|
-
function openLocal() {
|
|
613
|
-
opening ??= open().catch((error) => {
|
|
614
|
-
opening = undefined;
|
|
615
|
-
throw error;
|
|
616
|
-
});
|
|
617
|
-
return opening;
|
|
618
|
-
}
|
|
619
|
-
function exclusive(work) {
|
|
620
|
-
const next = mutations.then(work, work);
|
|
621
|
-
mutations = next.then(() => undefined, () => undefined);
|
|
622
|
-
return next;
|
|
623
|
-
}
|
|
624
|
-
function withdrawable(batch) {
|
|
625
|
-
const state = pendings.get(batch.id);
|
|
626
|
-
return state !== undefined && state.submission.state !== 'submitted' && !state.lost;
|
|
627
|
-
}
|
|
628
|
-
async function settle(local, locator, before, confirmed, queue, settling = {}) {
|
|
629
|
-
const withdrawn = withdrawals(confirmed, queue, withdrawable, rewriting());
|
|
630
|
-
const kept = queue.filter((batch) => !withdrawn.includes(batch));
|
|
631
|
-
const returning = [
|
|
632
|
-
...withdrawn.map((batch) => ({ batch, returned: { kind: 'withdrawn' } })).reverse(),
|
|
633
|
-
...(settling.leaving === undefined ? [] : [settling.leaving]),
|
|
634
|
-
];
|
|
635
|
-
const written = new Map();
|
|
636
|
-
for (const { batch, returned } of returning) {
|
|
637
|
-
if (returned === null)
|
|
638
|
-
continue;
|
|
639
|
-
for (const saved of pendings.get(batch.id)?.drafts ?? []) {
|
|
640
|
-
const existing = written.get(saved.path) ?? roomDrafts.get(locator)?.get(saved.path);
|
|
641
|
-
written.set(saved.path, {
|
|
642
|
-
locator,
|
|
643
|
-
path: saved.path,
|
|
644
|
-
revision: roomDraftRevision(),
|
|
645
|
-
parts: returnedInto(existing?.parts, saved.parts),
|
|
646
|
-
returned,
|
|
647
|
-
});
|
|
648
|
-
}
|
|
649
|
-
}
|
|
650
|
-
await local.write({
|
|
651
|
-
next: confirmed.record,
|
|
652
|
-
previous: before?.confirmed.record ?? null,
|
|
653
|
-
queue: kept,
|
|
654
|
-
previousQueue: before?.queue ?? [],
|
|
655
|
-
roomDrafts: [...written.values()],
|
|
656
|
-
});
|
|
657
|
-
for (const batch of before?.queue ?? []) {
|
|
658
|
-
if (!kept.includes(batch))
|
|
659
|
-
pendings.delete(batch.id);
|
|
660
|
-
}
|
|
661
|
-
for (const record of written.values())
|
|
662
|
-
rememberRoomDraft(record);
|
|
663
|
-
for (const batch of withdrawn)
|
|
664
|
-
journal.record(`outcome ${locator} ${batch.id} withdrawn`);
|
|
665
|
-
replicas.set(locator, replicaOf(confirmed, kept, rewriting(), before));
|
|
666
|
-
return changeSet(locator, before ?? null, replicas.get(locator) ?? null, {
|
|
667
|
-
room: settling.room,
|
|
668
|
-
queue: kept.length !== (before?.queue.length ?? 0),
|
|
669
|
-
drafts: [...written.keys()],
|
|
670
|
-
});
|
|
671
|
-
}
|
|
672
|
-
function publish(local, room, confirmed) {
|
|
673
|
-
return exclusive(async () => {
|
|
674
|
-
if (!continuing())
|
|
675
|
-
return;
|
|
676
|
-
const prior = replicas.get(room.locator);
|
|
677
|
-
const retained = readConfirmed({
|
|
678
|
-
...confirmed.record,
|
|
679
|
-
model: modelOf(room.locator),
|
|
680
|
-
selectionModel: modelOf(room.locator, 'typed-selection'),
|
|
681
|
-
}, prior?.confirmed);
|
|
682
|
-
if (retained.kind !== 'staged')
|
|
683
|
-
throw new Error(`Cannot retype staged Room ${room.locator}`);
|
|
684
|
-
const queue = prior?.queue ?? [];
|
|
685
|
-
const roomVersion = retained.confirmed.record.roomVersion;
|
|
686
|
-
const accepted = queue.filter((batch) => {
|
|
687
|
-
const accepted = pendings.get(batch.id)?.accepted;
|
|
688
|
-
return (accepted !== undefined && options.adapter.reached(room.locator, roomVersion, accepted));
|
|
689
|
-
});
|
|
690
|
-
const set = await settle(local, room.locator, prior, retained.confirmed, queue.filter((batch) => !accepted.includes(batch)), { room: true });
|
|
691
|
-
const target = wanted.get(room.locator);
|
|
692
|
-
if (target !== undefined && options.adapter.reached(room.locator, roomVersion, target)) {
|
|
693
|
-
wanted.delete(room.locator);
|
|
694
|
-
}
|
|
695
|
-
unreadable.delete(room.locator);
|
|
696
|
-
sourceRefusals.delete(room.locator);
|
|
697
|
-
declarationRefusals.delete(room.locator);
|
|
698
|
-
list(room, true);
|
|
699
|
-
publishChangeSet(set);
|
|
700
|
-
moveTo({ ...status, ...attention() });
|
|
701
|
-
});
|
|
702
|
-
}
|
|
703
|
-
async function saveRoomDrafts(locator, named, writtenBy) {
|
|
704
|
-
const local = await openLocal();
|
|
705
|
-
const replica = replicas.get(locator);
|
|
706
|
-
const room = rooms.get(locator);
|
|
707
|
-
if (replica === undefined || room === undefined) {
|
|
708
|
-
throw new Error(`No Local Replica for ${locator}`);
|
|
709
|
-
}
|
|
710
|
-
const saved = [];
|
|
711
|
-
for (const { path, revision } of named) {
|
|
712
|
-
const record = roomDrafts.get(locator)?.get(path);
|
|
713
|
-
if (record === undefined || record.revision !== revision) {
|
|
714
|
-
return { outcome: 'refused', rule: 'stale', file: path };
|
|
715
|
-
}
|
|
716
|
-
if (!saved.some((draft) => draft.path === path))
|
|
717
|
-
saved.push({ path, parts: record.parts });
|
|
718
|
-
}
|
|
719
|
-
const changed = saved.flatMap(({ path, parts }) => changedParts(path, underneath(parts, path, replica.visible)));
|
|
720
|
-
if (changed.length > 0)
|
|
721
|
-
return { outcome: 'refused', rule: 'changed-underneath', parts: changed };
|
|
722
|
-
const parts = saved.map(({ path, parts }) => changesOf(path, parts));
|
|
723
|
-
const built = buildBatch({
|
|
724
|
-
baseRoomVersion: replica.confirmed.record.roomVersion,
|
|
725
|
-
composition: compositionFor(replica.confirmed.record),
|
|
726
|
-
operations: room.operations,
|
|
727
|
-
providerOwnedPaths: room.providerOwnedPaths,
|
|
728
|
-
providerLimits: limits,
|
|
729
|
-
rewriting: rewriting(),
|
|
730
|
-
visible: replica.visible,
|
|
731
|
-
changes: parts.flatMap(({ changes }) => changes),
|
|
732
|
-
bases: parts.flatMap(({ bases }) => bases),
|
|
733
|
-
writtenBy,
|
|
734
|
-
});
|
|
735
|
-
if (built.kind === 'refused')
|
|
736
|
-
return { outcome: 'refused', ...built.refusal };
|
|
737
|
-
const batches = [...replica.queue, built.batch];
|
|
738
|
-
const consumed = saved.map(({ path }) => path);
|
|
739
|
-
try {
|
|
740
|
-
await local.write({
|
|
741
|
-
next: replica.confirmed.record,
|
|
742
|
-
previous: replica.confirmed.record,
|
|
743
|
-
queue: batches,
|
|
744
|
-
previousQueue: replica.queue,
|
|
745
|
-
saved,
|
|
746
|
-
consumedRoomDrafts: consumed,
|
|
747
|
-
});
|
|
748
|
-
}
|
|
749
|
-
catch {
|
|
750
|
-
return { outcome: 'refused', rule: 'durable-write' };
|
|
751
|
-
}
|
|
752
|
-
pendings.set(built.batch.id, { drafts: saved, submission: { state: 'waiting' }, lost: false });
|
|
753
|
-
for (const path of consumed)
|
|
754
|
-
forgetRoomDraft(locator, path);
|
|
755
|
-
replicas.set(locator, replicaOf(replica.confirmed, batches, rewriting(), replica));
|
|
756
|
-
publishChangeSet(changeSet(locator, replica, replicas.get(locator) ?? null, {
|
|
757
|
-
queue: true,
|
|
758
|
-
drafts: consumed,
|
|
759
|
-
}));
|
|
760
|
-
moveTo({ ...saving(status), ...attention() });
|
|
761
|
-
void enqueue();
|
|
762
|
-
return { outcome: 'saved', identifier: built.batch.id };
|
|
763
|
-
}
|
|
764
|
-
async function mark(local, locator, identifier, submission, lost) {
|
|
765
|
-
const state = pendings.get(identifier);
|
|
766
|
-
if (!continuing() ||
|
|
767
|
-
state === undefined ||
|
|
768
|
-
(JSON.stringify(state.submission) === JSON.stringify(submission) && state.lost === lost)) {
|
|
769
|
-
return;
|
|
770
|
-
}
|
|
771
|
-
await local.submit(locator, identifier, submission, lost);
|
|
772
|
-
pendings.set(identifier, { ...state, submission, lost });
|
|
773
|
-
const replica = replicas.get(locator);
|
|
774
|
-
if (replica !== undefined)
|
|
775
|
-
publishChangeSet(changeSet(locator, replica, replica, { queue: true }));
|
|
776
|
-
moveTo({ ...status, ...attention() });
|
|
777
|
-
}
|
|
778
|
-
async function keepUpload(local, locator, identifier, upload) {
|
|
779
|
-
if (!continuing())
|
|
780
|
-
throw new Error('connection refused');
|
|
781
|
-
await local.keepUpload(locator, identifier, upload);
|
|
782
|
-
pendings.set(identifier, { ...pendings.get(identifier), upload });
|
|
783
|
-
if (!continuing())
|
|
784
|
-
throw new Error('connection refused');
|
|
785
|
-
}
|
|
786
|
-
async function readHistory(locator, path, query = {}) {
|
|
787
|
-
if (closed || refusal !== null)
|
|
788
|
-
return { kind: 'transport-uncertainty' };
|
|
789
|
-
try {
|
|
790
|
-
await ensureAuthorization();
|
|
791
|
-
}
|
|
792
|
-
catch {
|
|
793
|
-
return { kind: 'transport-uncertainty' };
|
|
794
|
-
}
|
|
795
|
-
if (closed || authorization === undefined || refusal !== null) {
|
|
796
|
-
return { kind: 'transport-uncertainty' };
|
|
797
|
-
}
|
|
798
|
-
const transfer = ['transfer', 'history', locator, path];
|
|
799
|
-
if (query.revision !== undefined)
|
|
800
|
-
transfer.push(query.revision);
|
|
801
|
-
if (query.pageToken !== undefined)
|
|
802
|
-
transfer.push(query.pageToken);
|
|
803
|
-
journal.record(transfer.join(' '));
|
|
804
|
-
try {
|
|
805
|
-
const result = await options.adapter.history(authorization, {
|
|
806
|
-
locator,
|
|
807
|
-
path,
|
|
808
|
-
...(query.revision === undefined ? {} : { revision: query.revision }),
|
|
809
|
-
...(query.pageToken === undefined ? {} : { pageToken: query.pageToken }),
|
|
810
|
-
});
|
|
811
|
-
journal.record(`outcome history ${result.kind}`);
|
|
812
|
-
if (result.kind === 'scoped-refusal') {
|
|
813
|
-
const room = replicas.get(result.locator)?.confirmed.record;
|
|
814
|
-
if (room !== undefined)
|
|
815
|
-
await scoped(result, room, await openLocal());
|
|
816
|
-
}
|
|
817
|
-
if (result.kind === 'revoked-authorization')
|
|
818
|
-
await refuse('revoked-authorization');
|
|
819
|
-
return result;
|
|
820
|
-
}
|
|
821
|
-
catch {
|
|
822
|
-
journal.record('outcome history transport-uncertainty');
|
|
823
|
-
return { kind: 'transport-uncertainty' };
|
|
824
|
-
}
|
|
825
|
-
}
|
|
826
|
-
function declaredCards() {
|
|
827
|
-
for (const capability of capabilities) {
|
|
828
|
-
if (capability.conversationCards !== undefined)
|
|
829
|
-
return capability.conversationCards;
|
|
830
|
-
}
|
|
831
|
-
return undefined;
|
|
832
|
-
}
|
|
833
|
-
function declaredAction(name) {
|
|
834
|
-
for (const capability of capabilities) {
|
|
835
|
-
for (const action of capability.actions ?? []) {
|
|
836
|
-
if (action.name === name)
|
|
837
|
-
return action;
|
|
838
|
-
}
|
|
839
|
-
}
|
|
840
|
-
return undefined;
|
|
841
|
-
}
|
|
842
|
-
async function invokeAction(name, input) {
|
|
843
|
-
await ready;
|
|
844
|
-
if (closed || refusal !== null)
|
|
845
|
-
return { kind: 'transport-uncertainty' };
|
|
846
|
-
if (declaredAction(name) === undefined)
|
|
847
|
-
return { kind: 'undeclared' };
|
|
848
|
-
try {
|
|
849
|
-
await ensureAuthorization();
|
|
850
|
-
}
|
|
851
|
-
catch {
|
|
852
|
-
return { kind: 'transport-uncertainty' };
|
|
853
|
-
}
|
|
854
|
-
if (closed || authorization === undefined || refusal !== null) {
|
|
855
|
-
return { kind: 'transport-uncertainty' };
|
|
856
|
-
}
|
|
857
|
-
journal.record(`transfer action ${name}`);
|
|
858
|
-
try {
|
|
859
|
-
const result = await options.adapter.action(authorization, { name, input });
|
|
860
|
-
if (result.kind === 'action' && result.at !== undefined)
|
|
861
|
-
want(result.at);
|
|
862
|
-
if (result.kind === 'scoped-refusal') {
|
|
863
|
-
const room = replicas.get(result.locator)?.confirmed.record;
|
|
864
|
-
if (room !== undefined)
|
|
865
|
-
await scoped(result, room, await openLocal());
|
|
866
|
-
}
|
|
867
|
-
if (result.kind === 'revoked-authorization')
|
|
868
|
-
await refuse('revoked-authorization');
|
|
869
|
-
journal.record(`outcome action ${result.kind}`);
|
|
870
|
-
if (result.kind === 'rejected')
|
|
871
|
-
journal.record(`refusal rejected ${result.code}`);
|
|
872
|
-
return result;
|
|
873
|
-
}
|
|
874
|
-
catch {
|
|
875
|
-
journal.record('outcome action transport-uncertainty');
|
|
876
|
-
return { kind: 'transport-uncertainty' };
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
function continuing() {
|
|
880
|
-
return !closed && !discoveryPaused;
|
|
881
|
-
}
|
|
882
|
-
function reachedBy(locator, roomVersion) {
|
|
883
|
-
const held = replicas.get(locator);
|
|
884
|
-
return (held !== undefined &&
|
|
885
|
-
options.adapter.reached(locator, held.confirmed.record.roomVersion, roomVersion));
|
|
886
|
-
}
|
|
887
|
-
function writtenAfter(locator, roomVersion, path) {
|
|
888
|
-
const written = replicas.get(locator)?.confirmed.record.written[path];
|
|
889
|
-
return (written !== undefined &&
|
|
890
|
-
options.adapter.reached(locator, written, roomVersion) &&
|
|
891
|
-
!options.adapter.reached(locator, roomVersion, written));
|
|
892
|
-
}
|
|
893
|
-
function want({ locator, roomVersion }) {
|
|
894
|
-
if (reachedBy(locator, roomVersion))
|
|
895
|
-
return;
|
|
896
|
-
const held = wanted.get(locator);
|
|
897
|
-
if (held === undefined || options.adapter.reached(locator, roomVersion, held)) {
|
|
898
|
-
wanted.set(locator, roomVersion);
|
|
899
|
-
}
|
|
900
|
-
}
|
|
901
|
-
function reachOf(locator) {
|
|
902
|
-
let target = wanted.get(locator);
|
|
903
|
-
const versions = (replicas.get(locator)?.queue ?? []).flatMap((batch) => {
|
|
904
|
-
const accepted = pendings.get(batch.id)?.accepted;
|
|
905
|
-
return accepted === undefined ? [] : [accepted];
|
|
906
|
-
});
|
|
907
|
-
for (const version of versions) {
|
|
908
|
-
if (target === undefined || options.adapter.reached(locator, version, target)) {
|
|
909
|
-
target = version;
|
|
910
|
-
}
|
|
911
|
-
}
|
|
912
|
-
return target === undefined || reachedBy(locator, target) ? undefined : target;
|
|
913
|
-
}
|
|
914
|
-
async function pullRefresh(authorized, confirmed, minPosition) {
|
|
915
|
-
if (!continuing())
|
|
916
|
-
return unconfirmed;
|
|
917
|
-
try {
|
|
918
|
-
const result = await refreshRoom(options.adapter, authorized, confirmed, continuing, minPosition);
|
|
919
|
-
const detail = result.kind === 'advanced' ? result.confirmed.record.roomVersion : result.kind;
|
|
920
|
-
journal.record(`transfer refresh ${confirmed.record.locator} ${detail}`);
|
|
921
|
-
return result;
|
|
922
|
-
}
|
|
923
|
-
catch (error) {
|
|
924
|
-
journal.record(`transfer refresh ${confirmed.record.locator} transport-uncertainty`);
|
|
925
|
-
throw error;
|
|
926
|
-
}
|
|
927
|
-
}
|
|
928
|
-
async function pullBootstrap(authorized, room) {
|
|
929
|
-
if (!continuing())
|
|
930
|
-
return unconfirmed;
|
|
931
|
-
try {
|
|
932
|
-
const result = await bootstrapRoom(options.adapter, authorized, room, {
|
|
933
|
-
collection: modelOf(room.locator),
|
|
934
|
-
selection: modelOf(room.locator, 'typed-selection'),
|
|
935
|
-
}, continuing);
|
|
936
|
-
const detail = result.kind === 'staged' ? result.confirmed.record.roomVersion : result.kind;
|
|
937
|
-
journal.record(`transfer bootstrap ${room.locator} ${detail}`);
|
|
938
|
-
return result;
|
|
939
|
-
}
|
|
940
|
-
catch (error) {
|
|
941
|
-
journal.record(`transfer bootstrap ${room.locator} transport-uncertainty`);
|
|
942
|
-
throw error;
|
|
943
|
-
}
|
|
944
|
-
}
|
|
945
|
-
async function advance(room, local, authorized) {
|
|
946
|
-
const held = replicas.get(room.locator);
|
|
947
|
-
if (held !== undefined) {
|
|
948
|
-
const refreshed = await pullRefresh(authorized, held.confirmed, reachOf(room.locator));
|
|
949
|
-
if (!continuing())
|
|
950
|
-
return unconfirmed;
|
|
951
|
-
if (refreshed.kind === 'still-current') {
|
|
952
|
-
if (sourceRefusals.has(room.locator))
|
|
953
|
-
return unconfirmed;
|
|
954
|
-
if (unreadable.delete(room.locator)) {
|
|
955
|
-
list(room, true);
|
|
956
|
-
emitRooms();
|
|
957
|
-
}
|
|
958
|
-
return null;
|
|
959
|
-
}
|
|
960
|
-
if (refreshed.kind === 'advanced') {
|
|
961
|
-
await publish(local, room, refreshed.confirmed);
|
|
962
|
-
return null;
|
|
963
|
-
}
|
|
964
|
-
if (refreshed.kind !== 'beyond-horizon')
|
|
965
|
-
return refreshed;
|
|
966
|
-
}
|
|
967
|
-
const bootstrapped = await pullBootstrap(authorized, room);
|
|
968
|
-
if (!continuing())
|
|
969
|
-
return unconfirmed;
|
|
970
|
-
if (bootstrapped.kind !== 'staged')
|
|
971
|
-
return bootstrapped;
|
|
972
|
-
await publish(local, room, bootstrapped.confirmed);
|
|
973
|
-
return null;
|
|
974
|
-
}
|
|
975
|
-
async function withhold(withheld, room, local, progress) {
|
|
976
|
-
progress.complete = false;
|
|
977
|
-
if (withheld.kind === 'scoped-refusal') {
|
|
978
|
-
await scoped(withheld, room, local);
|
|
979
|
-
}
|
|
980
|
-
else if (withheld.kind === 'transport-uncertainty') {
|
|
981
|
-
progress.reachable = false;
|
|
982
|
-
}
|
|
983
|
-
else if (withheld.kind === 'retryable-refusal') {
|
|
984
|
-
journal.record(`refusal ${withheld.kind} ${withheld.code}`);
|
|
985
|
-
}
|
|
986
|
-
else if (withheld.kind === 'room-not-readable') {
|
|
987
|
-
unreadable.add(room.locator);
|
|
988
|
-
list(room, false);
|
|
989
|
-
emitRooms();
|
|
990
|
-
}
|
|
991
|
-
else if (withheld.kind === 'refused') {
|
|
992
|
-
refusedSource(room, withheld);
|
|
993
|
-
}
|
|
994
|
-
else if (withheld.kind === 'revoked-authorization') {
|
|
995
|
-
await refuse('revoked-authorization');
|
|
996
|
-
}
|
|
997
|
-
}
|
|
998
|
-
function refusedSource(room, { refusal }) {
|
|
999
|
-
sourceRefusals.set(room.locator, {
|
|
1000
|
-
code: refusal.rule,
|
|
1001
|
-
fields: {
|
|
1002
|
-
file: refusal.file,
|
|
1003
|
-
...(refusal.location === undefined ? {} : { location: refusal.location }),
|
|
1004
|
-
...(refusal.referrers === undefined ? {} : { referrers: refusal.referrers }),
|
|
1005
41
|
},
|
|
1006
|
-
});
|
|
1007
|
-
declarationRefusals.delete(room.locator);
|
|
1008
|
-
unreadable.add(room.locator);
|
|
1009
|
-
list(room, false);
|
|
1010
|
-
emitRooms();
|
|
1011
|
-
journal.record(`source refused ${room.locator} ${refusal.rule} ${refusal.file}`);
|
|
1012
|
-
}
|
|
1013
|
-
function leaving(batch, result) {
|
|
1014
|
-
if (result.kind === 'expired')
|
|
1015
|
-
return { batch, returned: { kind: 'expired' } };
|
|
1016
|
-
return { batch, returned: { kind: result.kind, code: result.code, fields: result.fields } };
|
|
1017
|
-
}
|
|
1018
|
-
function logAccepted(locator, submitted, accepted) {
|
|
1019
|
-
journal.record(`outcome ${locator} ${submitted.id} accepted ${accepted.roomVersion}` +
|
|
1020
|
-
(accepted.author === undefined ? '' : ` author ${accepted.author}`) +
|
|
1021
|
-
(accepted.time === undefined ? '' : ` time ${accepted.time}`));
|
|
1022
|
-
}
|
|
1023
|
-
async function recordAccepted(room, local, submitted, accepted) {
|
|
1024
|
-
await exclusive(async () => {
|
|
1025
|
-
if (!continuing())
|
|
1026
|
-
return;
|
|
1027
|
-
const current = replicas.get(room.locator);
|
|
1028
|
-
if (current === undefined || !current.queue.some((batch) => batch.id === submitted.id)) {
|
|
1029
|
-
return;
|
|
1030
|
-
}
|
|
1031
|
-
const state = pendings.get(submitted.id);
|
|
1032
|
-
if (state === undefined)
|
|
1033
|
-
return;
|
|
1034
|
-
await local.accept(room.locator, submitted.id, accepted.roomVersion);
|
|
1035
|
-
pendings.set(submitted.id, { ...state, accepted: accepted.roomVersion });
|
|
1036
|
-
logAccepted(room.locator, submitted, accepted);
|
|
1037
|
-
emitQueue(room.locator);
|
|
1038
|
-
});
|
|
1039
|
-
}
|
|
1040
|
-
async function commitAccept(room, local, submitted, accepted, base, overlay) {
|
|
1041
|
-
if (!continuing())
|
|
1042
|
-
return unconfirmed;
|
|
1043
|
-
const current = replicas.get(room.locator);
|
|
1044
|
-
if (current === undefined || !current.queue.some((batch) => batch.id === submitted.id)) {
|
|
1045
|
-
return null;
|
|
1046
|
-
}
|
|
1047
|
-
const remaining = current.queue.filter((batch) => batch.id !== submitted.id);
|
|
1048
|
-
const retainedBase = readConfirmed({
|
|
1049
|
-
...base.record,
|
|
1050
|
-
model: modelOf(room.locator),
|
|
1051
|
-
selectionModel: modelOf(room.locator, 'typed-selection'),
|
|
1052
|
-
}, base);
|
|
1053
|
-
const applied = overlay && retainedBase.kind === 'staged'
|
|
1054
|
-
? applyReturned(retainedBase.confirmed, accepted.documents, accepted.revisions, accepted.removals, accepted.roomVersion)
|
|
1055
|
-
: retainedBase;
|
|
1056
|
-
if (applied.kind !== 'staged')
|
|
1057
|
-
return applied;
|
|
1058
|
-
const set = await settle(local, room.locator, current, applied.confirmed, remaining, {
|
|
1059
|
-
room: true,
|
|
1060
|
-
});
|
|
1061
|
-
unreadable.delete(room.locator);
|
|
1062
|
-
sourceRefusals.delete(room.locator);
|
|
1063
|
-
declarationRefusals.delete(room.locator);
|
|
1064
|
-
list(room, true);
|
|
1065
|
-
publishChangeSet(set);
|
|
1066
|
-
moveTo({ ...status, ...attention() });
|
|
1067
|
-
return null;
|
|
1068
|
-
}
|
|
1069
|
-
async function accepting(room, local, submitted, accepted, pulled, fresh) {
|
|
1070
|
-
const roomVersion = pulled.record.roomVersion;
|
|
1071
|
-
if (options.adapter.reached(room.locator, roomVersion, accepted.roomVersion)) {
|
|
1072
|
-
return committed(room, local, submitted, accepted, pulled, roomVersion === accepted.roomVersion);
|
|
1073
|
-
}
|
|
1074
|
-
if (fresh && continuing())
|
|
1075
|
-
await publish(local, room, pulled);
|
|
1076
|
-
return false;
|
|
1077
|
-
}
|
|
1078
|
-
async function committed(room, local, submitted, accepted, base, overlay) {
|
|
1079
|
-
const failed = await exclusive(() => commitAccept(room, local, submitted, accepted, base, overlay));
|
|
1080
|
-
if (failed === null)
|
|
1081
|
-
return true;
|
|
1082
|
-
if (failed.kind === 'refused' && continuing())
|
|
1083
|
-
refusedSource(room, failed);
|
|
1084
|
-
return false;
|
|
1085
|
-
}
|
|
1086
|
-
async function submitRoom(room, local, authorized, progress) {
|
|
1087
|
-
while (continuing() && authorization !== undefined) {
|
|
1088
|
-
const replica = replicas.get(room.locator);
|
|
1089
|
-
const head = replica?.queue[0];
|
|
1090
|
-
if (replica === undefined || head === undefined)
|
|
1091
|
-
return;
|
|
1092
|
-
if (pendings.get(head.id)?.accepted !== undefined) {
|
|
1093
|
-
progress.complete = false;
|
|
1094
|
-
return;
|
|
1095
|
-
}
|
|
1096
|
-
const state = pendings.get(head.id);
|
|
1097
|
-
const { submission, lost: earlier } = state ?? {
|
|
1098
|
-
submission: { state: 'waiting' },
|
|
1099
|
-
lost: false,
|
|
1100
|
-
};
|
|
1101
|
-
if (submission.state === 'reset' && Date.now() < Date.parse(submission.reset)) {
|
|
1102
|
-
progress.complete = false;
|
|
1103
|
-
return;
|
|
1104
|
-
}
|
|
1105
|
-
if (writesHeld()) {
|
|
1106
|
-
progress.complete = false;
|
|
1107
|
-
journal.record(`submission ${room.locator} ${head.id} held`);
|
|
1108
|
-
return;
|
|
1109
|
-
}
|
|
1110
|
-
const lost = earlier || submission.state === 'submitted';
|
|
1111
|
-
let result;
|
|
1112
|
-
journal.record(`submission ${room.locator} ${head.id}`);
|
|
1113
|
-
await exclusive(() => mark(local, room.locator, head.id, { state: 'submitted' }, lost));
|
|
1114
|
-
if (discoveryPaused)
|
|
1115
|
-
return;
|
|
1116
|
-
if (writesHeld()) {
|
|
1117
|
-
await exclusive(() => mark(local, room.locator, head.id, submission, earlier));
|
|
1118
|
-
progress.complete = false;
|
|
1119
|
-
journal.record(`submission ${room.locator} ${head.id} held`);
|
|
1120
|
-
return;
|
|
1121
|
-
}
|
|
1122
|
-
const opensBefore = openSignals;
|
|
1123
|
-
try {
|
|
1124
|
-
result = await options.adapter.mutate(authorized, {
|
|
1125
|
-
locator: room.locator,
|
|
1126
|
-
changeBatch: head,
|
|
1127
|
-
...(state?.upload === undefined ? {} : { upload: state.upload }),
|
|
1128
|
-
}, (upload) => exclusive(() => keepUpload(local, room.locator, head.id, upload)));
|
|
1129
|
-
}
|
|
1130
|
-
catch {
|
|
1131
|
-
progress.complete = false;
|
|
1132
|
-
if (continuing())
|
|
1133
|
-
progress.reachable = false;
|
|
1134
|
-
return;
|
|
1135
|
-
}
|
|
1136
|
-
if (!continuing())
|
|
1137
|
-
return;
|
|
1138
|
-
if (result.kind === 'transport-uncertainty') {
|
|
1139
|
-
progress.complete = false;
|
|
1140
|
-
progress.reachable = false;
|
|
1141
|
-
return;
|
|
1142
|
-
}
|
|
1143
|
-
if (result.kind === 'room-not-readable') {
|
|
1144
|
-
progress.complete = false;
|
|
1145
|
-
unreadable.add(room.locator);
|
|
1146
|
-
list(room, false);
|
|
1147
|
-
emitRooms();
|
|
1148
|
-
return;
|
|
1149
|
-
}
|
|
1150
|
-
if (result.kind === 'revoked-authorization') {
|
|
1151
|
-
await refuse('revoked-authorization');
|
|
1152
|
-
return;
|
|
1153
|
-
}
|
|
1154
|
-
if (result.kind === 'scoped-refusal') {
|
|
1155
|
-
await scoped(result, room, local);
|
|
1156
|
-
progress.complete = false;
|
|
1157
|
-
return;
|
|
1158
|
-
}
|
|
1159
|
-
if (result.kind === 'write-halted') {
|
|
1160
|
-
journal.record(`outcome ${room.locator} ${head.id} write-halted`);
|
|
1161
|
-
await exclusive(() => mark(local, room.locator, head.id, { state: 'waiting' }, lost));
|
|
1162
|
-
if (serviceMode === 'open' && openSignals !== opensBefore)
|
|
1163
|
-
continue;
|
|
1164
|
-
haltedRefusal = true;
|
|
1165
|
-
progress.complete = false;
|
|
1166
|
-
return;
|
|
1167
|
-
}
|
|
1168
|
-
if (result.kind === 'conflicted' ||
|
|
1169
|
-
result.kind === 'rejected' ||
|
|
1170
|
-
result.kind === 'expired' ||
|
|
1171
|
-
result.kind === 'retryable-refusal') {
|
|
1172
|
-
if (result.kind === 'expired') {
|
|
1173
|
-
const withheld = await advance(room, local, authorized);
|
|
1174
|
-
if (withheld !== null)
|
|
1175
|
-
return withhold(withheld, room, local, progress);
|
|
1176
|
-
}
|
|
1177
|
-
journal.record(`outcome ${room.locator} ${head.id} ${result.kind}` +
|
|
1178
|
-
(result.kind === 'conflicted' && result.roomVersion !== undefined
|
|
1179
|
-
? ` ${result.roomVersion}`
|
|
1180
|
-
: ''));
|
|
1181
|
-
if (result.kind !== 'expired')
|
|
1182
|
-
journal.record(`refusal ${result.kind} ${result.code}`);
|
|
1183
|
-
}
|
|
1184
|
-
if (result.kind === 'retryable-refusal') {
|
|
1185
|
-
const reset = result.reset;
|
|
1186
|
-
progress.complete = false;
|
|
1187
|
-
await exclusive(() => mark(local, room.locator, head.id, reset === undefined ? { state: 'waiting' } : { state: 'reset', reset }, lost));
|
|
1188
|
-
return;
|
|
1189
|
-
}
|
|
1190
|
-
if (result.kind === 'conflicted' || result.kind === 'rejected' || result.kind === 'expired') {
|
|
1191
|
-
const left = leaving(head, result);
|
|
1192
|
-
await exclusive(async () => {
|
|
1193
|
-
if (!continuing())
|
|
1194
|
-
return;
|
|
1195
|
-
const current = replicas.get(room.locator);
|
|
1196
|
-
if (current === undefined || !current.queue.some((batch) => batch.id === head.id)) {
|
|
1197
|
-
return;
|
|
1198
|
-
}
|
|
1199
|
-
const set = await settle(local, room.locator, current, current.confirmed, current.queue.filter((batch) => batch.id !== head.id), {
|
|
1200
|
-
leaving: result.kind === 'expired' && landed(head, current.confirmed)
|
|
1201
|
-
? { ...left, returned: null }
|
|
1202
|
-
: left,
|
|
1203
|
-
});
|
|
1204
|
-
publishChangeSet(set);
|
|
1205
|
-
moveTo({ ...status, ...attention() });
|
|
1206
|
-
});
|
|
1207
|
-
if (result.kind === 'expired')
|
|
1208
|
-
continue;
|
|
1209
|
-
const withheld = await advance(room, local, authorized);
|
|
1210
|
-
if (withheld !== null)
|
|
1211
|
-
return withhold(withheld, room, local, progress);
|
|
1212
|
-
continue;
|
|
1213
|
-
}
|
|
1214
|
-
await recordAccepted(room, local, head, result);
|
|
1215
|
-
const from = replicas.get(room.locator)?.confirmed;
|
|
1216
|
-
if (from === undefined)
|
|
1217
|
-
return;
|
|
1218
|
-
const pulled = await pullRefresh(authorized, from, reachOf(room.locator));
|
|
1219
|
-
if (!continuing())
|
|
1220
|
-
return;
|
|
1221
|
-
if (pulled.kind === 'revoked-authorization') {
|
|
1222
|
-
await refuse('revoked-authorization');
|
|
1223
|
-
return;
|
|
1224
|
-
}
|
|
1225
|
-
if (pulled.kind === 'scoped-refusal') {
|
|
1226
|
-
await scoped(pulled, room, local);
|
|
1227
|
-
progress.complete = false;
|
|
1228
|
-
return;
|
|
1229
|
-
}
|
|
1230
|
-
if (pulled.kind === 'still-current' || pulled.kind === 'advanced') {
|
|
1231
|
-
const fresh = pulled.kind === 'advanced';
|
|
1232
|
-
if (!(await accepting(room, local, head, result, fresh ? pulled.confirmed : from, fresh))) {
|
|
1233
|
-
progress.complete = false;
|
|
1234
|
-
return;
|
|
1235
|
-
}
|
|
1236
|
-
continue;
|
|
1237
|
-
}
|
|
1238
|
-
if (pulled.kind === 'beyond-horizon') {
|
|
1239
|
-
const bootstrapped = await pullBootstrap(authorized, room);
|
|
1240
|
-
if (!continuing())
|
|
1241
|
-
return;
|
|
1242
|
-
if (bootstrapped.kind === 'revoked-authorization') {
|
|
1243
|
-
await refuse('revoked-authorization');
|
|
1244
|
-
return;
|
|
1245
|
-
}
|
|
1246
|
-
if (bootstrapped.kind === 'scoped-refusal') {
|
|
1247
|
-
await scoped(bootstrapped, room, local);
|
|
1248
|
-
progress.complete = false;
|
|
1249
|
-
return;
|
|
1250
|
-
}
|
|
1251
|
-
if (bootstrapped.kind === 'staged') {
|
|
1252
|
-
if (!(await accepting(room, local, head, result, bootstrapped.confirmed, true))) {
|
|
1253
|
-
progress.complete = false;
|
|
1254
|
-
return;
|
|
1255
|
-
}
|
|
1256
|
-
continue;
|
|
1257
|
-
}
|
|
1258
|
-
progress.complete = false;
|
|
1259
|
-
if (bootstrapped.kind === 'transport-uncertainty') {
|
|
1260
|
-
progress.reachable = false;
|
|
1261
|
-
}
|
|
1262
|
-
else if (bootstrapped.kind === 'room-not-readable') {
|
|
1263
|
-
unreadable.add(room.locator);
|
|
1264
|
-
list(room, false);
|
|
1265
|
-
emitRooms();
|
|
1266
|
-
}
|
|
1267
|
-
else if (bootstrapped.kind === 'refused') {
|
|
1268
|
-
refusedSource(room, bootstrapped);
|
|
1269
|
-
}
|
|
1270
|
-
return;
|
|
1271
|
-
}
|
|
1272
|
-
progress.complete = false;
|
|
1273
|
-
if (pulled.kind === 'transport-uncertainty') {
|
|
1274
|
-
progress.reachable = false;
|
|
1275
|
-
}
|
|
1276
|
-
else if (pulled.kind === 'room-not-readable') {
|
|
1277
|
-
unreadable.add(room.locator);
|
|
1278
|
-
list(room, false);
|
|
1279
|
-
emitRooms();
|
|
1280
|
-
}
|
|
1281
|
-
else if (pulled.kind === 'retryable-refusal') {
|
|
1282
|
-
journal.record(`refusal ${pulled.kind} ${pulled.code}`);
|
|
1283
|
-
}
|
|
1284
|
-
else if (pulled.kind === 'refused') {
|
|
1285
|
-
refusedSource(room, pulled);
|
|
1286
|
-
}
|
|
1287
|
-
return;
|
|
1288
|
-
}
|
|
1289
|
-
return;
|
|
1290
|
-
}
|
|
1291
|
-
async function retainModel(local, room, attached, next) {
|
|
1292
|
-
const key = modelKey(room.locator, attached);
|
|
1293
|
-
if (next.status === 'unavailable')
|
|
1294
|
-
modelEpoch.set(key, (modelEpoch.get(key) ?? 0) + 1);
|
|
1295
|
-
const epoch = modelEpoch.get(key) ?? 0;
|
|
1296
|
-
const previous = modelOf(room.locator, attached);
|
|
1297
|
-
const heldBefore = replicas.get(room.locator)?.confirmed.record;
|
|
1298
|
-
if (JSON.stringify(previous) === JSON.stringify(next) &&
|
|
1299
|
-
(heldBefore === undefined ||
|
|
1300
|
-
(JSON.stringify(heldBefore.operations) === JSON.stringify(room.operations) &&
|
|
1301
|
-
JSON.stringify(heldBefore.replica) === JSON.stringify(room.replica) &&
|
|
1302
|
-
JSON.stringify(heldBefore.attachedModel) === JSON.stringify(room.attachedModel) &&
|
|
1303
|
-
JSON.stringify(heldBefore.typedSelection) ===
|
|
1304
|
-
JSON.stringify(room.typedSelection ?? null))))
|
|
1305
|
-
return;
|
|
1306
|
-
if (next.status === 'unavailable') {
|
|
1307
|
-
invalidModels.add(key);
|
|
1308
|
-
liveModels.delete(key);
|
|
1309
|
-
}
|
|
1310
|
-
await exclusive(async () => {
|
|
1311
|
-
if (!continuing())
|
|
1312
|
-
return;
|
|
1313
|
-
if (next.status !== 'unavailable' && epoch !== (modelEpoch.get(key) ?? 0))
|
|
1314
|
-
return;
|
|
1315
|
-
const held = replicas.get(room.locator);
|
|
1316
|
-
if (held !== undefined) {
|
|
1317
|
-
const declared = {
|
|
1318
|
-
...held.confirmed.record,
|
|
1319
|
-
operations: room.operations,
|
|
1320
|
-
replica: room.replica,
|
|
1321
|
-
attachedModel: room.attachedModel,
|
|
1322
|
-
typedSelection: room.typedSelection ?? null,
|
|
1323
|
-
...(attached === 'collection' ? { model: next } : { selectionModel: next }),
|
|
1324
|
-
};
|
|
1325
|
-
const reclassified = sameMembership(declared, held.confirmed.record)
|
|
1326
|
-
? readConfirmed(declared, held.confirmed)
|
|
1327
|
-
: readReclassified(declared, held.confirmed);
|
|
1328
|
-
const record = reclassified.kind === 'staged'
|
|
1329
|
-
? declared
|
|
1330
|
-
: {
|
|
1331
|
-
...declared,
|
|
1332
|
-
typedSelection: held.confirmed.record.typedSelection,
|
|
1333
|
-
};
|
|
1334
|
-
const read = reclassified.kind === 'deferred' ? readConfirmed(record, held.confirmed) : reclassified;
|
|
1335
|
-
await local.write({
|
|
1336
|
-
next: record,
|
|
1337
|
-
previous: held.confirmed.record,
|
|
1338
|
-
queue: held.queue,
|
|
1339
|
-
});
|
|
1340
|
-
if (read.kind === 'staged') {
|
|
1341
|
-
replicas.set(room.locator, replicaOf(read.confirmed, held.queue, rewriting(), held));
|
|
1342
|
-
if (declarationRefusals.delete(room.locator)) {
|
|
1343
|
-
sourceRefusals.delete(room.locator);
|
|
1344
|
-
unreadable.delete(room.locator);
|
|
1345
|
-
}
|
|
1346
|
-
}
|
|
1347
|
-
else if (read.kind === 'refused') {
|
|
1348
|
-
replicas.set(room.locator, { ...held, confirmed: { ...held.confirmed, record } });
|
|
1349
|
-
refusedSource(room, read);
|
|
1350
|
-
declarationRefusals.add(room.locator);
|
|
1351
|
-
}
|
|
1352
|
-
}
|
|
1353
|
-
models.set(key, next);
|
|
1354
|
-
if (next.status === 'current') {
|
|
1355
|
-
liveModels.set(key, JSON.stringify(next.snapshot));
|
|
1356
|
-
invalidModels.delete(key);
|
|
1357
|
-
}
|
|
1358
|
-
list(room, room.replica.admission === 'admitted' && !unreadable.has(room.locator));
|
|
1359
|
-
if (held === undefined)
|
|
1360
|
-
emitRooms();
|
|
1361
|
-
else
|
|
1362
|
-
publishChangeSet(changeSet(room.locator, held, replicas.get(room.locator) ?? null, { room: true }));
|
|
1363
|
-
});
|
|
1364
|
-
}
|
|
1365
|
-
async function acquireModel(local, room, authorized, selection) {
|
|
1366
|
-
const attached = selection === null ? 'collection' : 'typed-selection';
|
|
1367
|
-
const key = modelKey(room.locator, attached);
|
|
1368
|
-
const declared = selection === null ? room.attachedModel : selection.attachedModel;
|
|
1369
|
-
let previous = modelOf(room.locator, attached);
|
|
1370
|
-
if (previous.status !== 'unavailable' &&
|
|
1371
|
-
(declared === null || !sameModel(previous.snapshot, declared))) {
|
|
1372
|
-
previous = { status: 'unavailable', reason: 'version-changed' };
|
|
1373
|
-
await retainModel(local, room, attached, previous);
|
|
1374
|
-
if (discoveryPaused)
|
|
1375
|
-
return;
|
|
1376
|
-
}
|
|
1377
|
-
const epoch = modelEpoch.get(key) ?? 0;
|
|
1378
|
-
let answer;
|
|
1379
|
-
try {
|
|
1380
|
-
answer = await options.adapter.model(authorized, selection === null
|
|
1381
|
-
? { locator: room.locator, attached: 'collection', model: declared }
|
|
1382
|
-
: {
|
|
1383
|
-
locator: room.locator,
|
|
1384
|
-
attached: 'typed-selection',
|
|
1385
|
-
roots: selection.roots,
|
|
1386
|
-
model: declared,
|
|
1387
|
-
});
|
|
1388
|
-
}
|
|
1389
|
-
catch {
|
|
1390
|
-
answer = { kind: 'transport-uncertainty' };
|
|
1391
|
-
}
|
|
1392
|
-
if (discoveryPaused || epoch !== (modelEpoch.get(key) ?? 0))
|
|
1393
|
-
return;
|
|
1394
|
-
if (answer.kind === 'revoked-authorization') {
|
|
1395
|
-
await refuse('revoked-authorization');
|
|
1396
|
-
return;
|
|
1397
|
-
}
|
|
1398
|
-
if (answer.kind === 'transport-uncertainty') {
|
|
1399
|
-
await retainModel(local, room, attached, cached(previous));
|
|
1400
|
-
return;
|
|
1401
|
-
}
|
|
1402
|
-
if (answer.kind === 'model-unavailable') {
|
|
1403
|
-
await retainModel(local, room, attached, {
|
|
1404
|
-
status: 'unavailable',
|
|
1405
|
-
reason: answer.reason,
|
|
1406
|
-
});
|
|
1407
|
-
return;
|
|
1408
|
-
}
|
|
1409
|
-
if (declared === null || !sameModel(answer.snapshot, declared)) {
|
|
1410
|
-
await retainModel(local, room, attached, {
|
|
1411
|
-
status: 'unavailable',
|
|
1412
|
-
reason: 'version-changed',
|
|
1413
|
-
});
|
|
1414
|
-
return;
|
|
1415
|
-
}
|
|
1416
|
-
if (previous.status !== 'unavailable' &&
|
|
1417
|
-
sameModel(previous.snapshot, answer.snapshot) &&
|
|
1418
|
-
previous.snapshot.schema !== answer.snapshot.schema) {
|
|
1419
|
-
await retainModel(local, room, attached, { status: 'unavailable', reason: 'invalid' });
|
|
1420
|
-
return;
|
|
1421
|
-
}
|
|
1422
|
-
const verified = answer.snapshot.schema === null ? EMPTY_SCHEMA : verify(answer.snapshot.schema);
|
|
1423
|
-
if (verified.verdict === 'refuse') {
|
|
1424
|
-
const where = verified.location === undefined ? '' : ` ${verified.location}`;
|
|
1425
|
-
journal.record(`model invalid ${room.locator} ${attached} ${answer.snapshot.identity} ${verified.rule}${where}`);
|
|
1426
|
-
}
|
|
1427
|
-
await retainModel(local, room, attached, verified.verdict === 'accept'
|
|
1428
|
-
? {
|
|
1429
|
-
status: 'current',
|
|
1430
|
-
snapshot: structuredClone(answer.snapshot),
|
|
1431
|
-
verifiedWith: MD_MODEL,
|
|
1432
|
-
types: verified.model.types,
|
|
1433
|
-
}
|
|
1434
|
-
: { status: 'unavailable', reason: 'invalid' });
|
|
1435
|
-
}
|
|
1436
|
-
async function scoped(result, room, local) {
|
|
1437
|
-
scopedRefusals.set(room.locator, { code: result.code, fields: result.fields });
|
|
1438
|
-
let scopedAway = room;
|
|
1439
|
-
if (result.scope === 'model')
|
|
1440
|
-
await retainModel(local, room, 'collection', {
|
|
1441
|
-
status: 'unavailable',
|
|
1442
|
-
reason: 'not-readable',
|
|
1443
|
-
});
|
|
1444
|
-
else if (result.scope !== 'operation') {
|
|
1445
|
-
unreadable.add(room.locator);
|
|
1446
|
-
scopedAway = {
|
|
1447
|
-
...room,
|
|
1448
|
-
replica: { ...room.replica, admission: 'unavailable' },
|
|
1449
|
-
operations: {
|
|
1450
|
-
...room.operations,
|
|
1451
|
-
...(result.scope === 'content' ? { read: [] } : {}),
|
|
1452
|
-
...(result.scope === 'sync' ? { sync: [] } : {}),
|
|
1453
|
-
},
|
|
1454
|
-
};
|
|
1455
|
-
await retainModel(local, scopedAway, 'collection', modelOf(room.locator));
|
|
1456
|
-
}
|
|
1457
|
-
if (!continuing())
|
|
1458
|
-
return;
|
|
1459
|
-
list(scopedAway, scopedAway.replica.admission === 'admitted' && !unreadable.has(room.locator));
|
|
1460
|
-
emitRooms();
|
|
1461
|
-
journal.record(`scoped refusal ${room.locator} ${result.scope} ${result.code}`);
|
|
1462
|
-
}
|
|
1463
|
-
async function synchronize(discovered, local, progress) {
|
|
1464
|
-
const prepared = await Promise.allSettled(discovered.map((room) => prepareRoom(room, local, progress)));
|
|
1465
|
-
const ready = discovered.filter((_, index) => {
|
|
1466
|
-
const outcome = prepared[index];
|
|
1467
|
-
return outcome?.status === 'fulfilled' && outcome.value;
|
|
1468
|
-
});
|
|
1469
|
-
const rooms = await Promise.allSettled(ready.map((room) => synchronizeRoom(room, local, progress)));
|
|
1470
|
-
const failed = [...prepared, ...rooms].find((room) => room.status === 'rejected');
|
|
1471
|
-
if (failed !== undefined)
|
|
1472
|
-
throw failed.reason;
|
|
1473
|
-
}
|
|
1474
|
-
async function prepareRoom(room, local, progress) {
|
|
1475
|
-
if (closed || authorization === undefined) {
|
|
1476
|
-
progress.complete = false;
|
|
1477
|
-
return false;
|
|
1478
|
-
}
|
|
1479
|
-
const prior = replicas.get(room.locator)?.confirmed.record;
|
|
1480
|
-
if (prior !== undefined && prior.replica.selection !== room.replica.selection) {
|
|
1481
|
-
list(prior, false);
|
|
1482
|
-
emitRooms();
|
|
1483
|
-
journal.record(`selection changed ${room.locator}`);
|
|
1484
|
-
progress.complete = false;
|
|
1485
|
-
return false;
|
|
1486
|
-
}
|
|
1487
|
-
const selection = room.typedSelection;
|
|
1488
|
-
await Promise.all([
|
|
1489
|
-
acquireModel(local, room, authorization, null),
|
|
1490
|
-
...(selection === undefined ? [] : [acquireModel(local, room, authorization, selection)]),
|
|
1491
|
-
]);
|
|
1492
|
-
if (discoveryPaused || authorization === undefined)
|
|
1493
|
-
return false;
|
|
1494
|
-
if (room.replica.admission !== 'admitted') {
|
|
1495
|
-
if (replicas.has(room.locator))
|
|
1496
|
-
progress.complete = false;
|
|
1497
|
-
return false;
|
|
1498
|
-
}
|
|
1499
|
-
const held = replicas.get(room.locator);
|
|
1500
|
-
if (held !== undefined && held.confirmed.record.replica.selection !== room.replica.selection) {
|
|
1501
|
-
progress.complete = false;
|
|
1502
|
-
return false;
|
|
1503
|
-
}
|
|
1504
|
-
return true;
|
|
1505
|
-
}
|
|
1506
|
-
async function synchronizeRoom(room, local, progress) {
|
|
1507
|
-
if (discoveryPaused || authorization === undefined)
|
|
1508
|
-
return;
|
|
1509
|
-
const withheld = await advance(room, local, authorization);
|
|
1510
|
-
if (withheld !== null) {
|
|
1511
|
-
await withhold(withheld, room, local, progress);
|
|
1512
|
-
return;
|
|
1513
|
-
}
|
|
1514
|
-
await submitRoom(room, local, authorization, progress);
|
|
1515
|
-
}
|
|
1516
|
-
async function cycle(local, progress) {
|
|
1517
|
-
if (closed || authorization === undefined) {
|
|
1518
|
-
notifyReady();
|
|
1519
|
-
return;
|
|
1520
|
-
}
|
|
1521
|
-
moveTo(transferring(status));
|
|
1522
|
-
let result;
|
|
1523
|
-
try {
|
|
1524
|
-
result = await options.adapter.discover(authorization);
|
|
1525
|
-
}
|
|
1526
|
-
catch {
|
|
1527
|
-
result = { kind: 'transport-uncertainty' };
|
|
1528
|
-
}
|
|
1529
|
-
if (closed) {
|
|
1530
|
-
notifyReady();
|
|
1531
|
-
return;
|
|
1532
|
-
}
|
|
1533
|
-
journal.record(`connection discover ${result.kind}`);
|
|
1534
|
-
if (result.kind === 'transport-uncertainty') {
|
|
1535
|
-
for (const room of rooms.values()) {
|
|
1536
|
-
await retainModel(local, room, 'collection', cached(modelOf(room.locator)));
|
|
1537
|
-
if (room.typedSelection !== null) {
|
|
1538
|
-
await retainModel(local, room, 'typed-selection', cached(modelOf(room.locator, 'typed-selection')));
|
|
1539
|
-
}
|
|
1540
|
-
}
|
|
1541
|
-
progress.reachable = false;
|
|
1542
|
-
notifyReady();
|
|
1543
|
-
return;
|
|
1544
|
-
}
|
|
1545
|
-
if (result.kind === 'contract-unavailable') {
|
|
1546
|
-
journal.record(`connection contract unavailable ${result.code}`);
|
|
1547
|
-
await refuse('missing-required-guarantee', { code: result.code, fields: result.fields });
|
|
1548
|
-
notifyReady();
|
|
1549
|
-
return;
|
|
1550
|
-
}
|
|
1551
|
-
if (result.kind === 'revoked-authorization') {
|
|
1552
|
-
await refuse('revoked-authorization');
|
|
1553
|
-
notifyReady();
|
|
1554
|
-
return;
|
|
1555
|
-
}
|
|
1556
|
-
if (!result.supportedVersions.includes(ROOM_CONTRACT_VERSION)) {
|
|
1557
|
-
await refuse('no-matching-version');
|
|
1558
|
-
notifyReady();
|
|
1559
|
-
return;
|
|
1560
|
-
}
|
|
1561
|
-
if (result.mdModel.identity !== MD_MODEL.identity ||
|
|
1562
|
-
result.mdModel.version !== MD_MODEL.version) {
|
|
1563
|
-
await refuse('md-model-version-mismatch');
|
|
1564
|
-
notifyReady();
|
|
1565
|
-
return;
|
|
1566
|
-
}
|
|
1567
|
-
for (const room of result.rooms) {
|
|
1568
|
-
if (room.operations === undefined ||
|
|
1569
|
-
!['read', 'sync', 'write', 'delete'].every((operation) => Array.isArray(room.operations[operation]) &&
|
|
1570
|
-
room.operations[operation].every((scope) => typeof scope === 'string')) ||
|
|
1571
|
-
room.replica === undefined ||
|
|
1572
|
-
typeof room.replica.selection !== 'string' ||
|
|
1573
|
-
room.replica.selection.length === 0 ||
|
|
1574
|
-
!['admitted', 'unavailable'].includes(room.replica.admission) ||
|
|
1575
|
-
!declaredIdentity(room.attachedModel)) {
|
|
1576
|
-
journal.record(`invalid model or replica declaration ${room.locator}`);
|
|
1577
|
-
await refuse('missing-required-guarantee');
|
|
1578
|
-
notifyReady();
|
|
1579
|
-
return;
|
|
1580
|
-
}
|
|
1581
|
-
if (!declaredSelection(room)) {
|
|
1582
|
-
journal.record(`invalid typed selection declaration ${room.locator}`);
|
|
1583
|
-
await refuse('missing-required-guarantee');
|
|
1584
|
-
notifyReady();
|
|
1585
|
-
return;
|
|
1586
|
-
}
|
|
1587
|
-
}
|
|
1588
|
-
const declared = new Set(result.capabilities.map((capability) => capability.name));
|
|
1589
|
-
for (const required of options.requiredGuarantees) {
|
|
1590
|
-
if (!knownCapabilities.has(required) || !declared.has(required)) {
|
|
1591
|
-
await refuse('missing-required-guarantee');
|
|
1592
|
-
notifyReady();
|
|
1593
|
-
return;
|
|
1594
|
-
}
|
|
1595
|
-
}
|
|
1596
|
-
if (refusal !== null) {
|
|
1597
|
-
refusal = null;
|
|
1598
|
-
emitRefusal();
|
|
1599
|
-
}
|
|
1600
|
-
const previousCapabilities = capabilities;
|
|
1601
|
-
capabilities = result.capabilities;
|
|
1602
|
-
notifyReady();
|
|
1603
|
-
if (declared.has('room-version-notifications'))
|
|
1604
|
-
observeRoomVersions();
|
|
1605
|
-
if (JSON.stringify(result.providerLimits) !== JSON.stringify(limits) ||
|
|
1606
|
-
JSON.stringify(result.capabilities) !== JSON.stringify(previousCapabilities)) {
|
|
1607
|
-
limits = result.providerLimits;
|
|
1608
|
-
await local.declare(limits, capabilities);
|
|
1609
|
-
}
|
|
1610
|
-
await applyDiscovery(result.rooms, local);
|
|
1611
|
-
handSelected(result.rooms
|
|
1612
|
-
.filter((room) => room.replica.admission === 'admitted')
|
|
1613
|
-
.map((room) => room.locator)
|
|
1614
|
-
.sort());
|
|
1615
|
-
progress.complete = true;
|
|
1616
|
-
await synchronize(result.rooms, local, progress);
|
|
1617
|
-
if (closed || discoveryPaused)
|
|
1618
|
-
return;
|
|
1619
|
-
await conversations.reconcile();
|
|
1620
|
-
if (status.providerUnreachable)
|
|
1621
|
-
conversations.reconnected();
|
|
1622
|
-
await conversations.catchUp(progress);
|
|
1623
|
-
}
|
|
1624
|
-
async function ensureAuthorization() {
|
|
1625
|
-
if (authorization !== undefined)
|
|
1626
|
-
return;
|
|
1627
|
-
try {
|
|
1628
|
-
authorization = await options.adapter.authorize();
|
|
1629
|
-
journal.record('connection authorize');
|
|
1630
|
-
}
|
|
1631
|
-
catch (error) {
|
|
1632
|
-
journal.record('connection authorize transport-uncertainty');
|
|
1633
|
-
throw error;
|
|
1634
|
-
}
|
|
1635
|
-
}
|
|
1636
|
-
async function connect() {
|
|
1637
|
-
if (closed || stopping || discoveryPaused) {
|
|
1638
|
-
notifyReady();
|
|
1639
|
-
return;
|
|
1640
|
-
}
|
|
1641
|
-
startTimer();
|
|
1642
|
-
const progress = { complete: false, reachable: true };
|
|
1643
|
-
let completedAt = new Date().toISOString();
|
|
1644
|
-
try {
|
|
1645
|
-
const local = await openLocal();
|
|
1646
|
-
if (closed)
|
|
1647
|
-
return;
|
|
1648
|
-
await ensureAuthorization();
|
|
1649
|
-
if (closed)
|
|
1650
|
-
return;
|
|
1651
|
-
await cycle(local, progress);
|
|
1652
|
-
if (closed)
|
|
1653
|
-
return;
|
|
1654
|
-
if (progress.complete) {
|
|
1655
|
-
completedAt = new Date().toISOString();
|
|
1656
|
-
await local.complete(completedAt);
|
|
1657
|
-
}
|
|
1658
|
-
}
|
|
1659
|
-
catch {
|
|
1660
|
-
progress.complete = false;
|
|
1661
|
-
notifyReady();
|
|
1662
|
-
return;
|
|
1663
|
-
}
|
|
1664
|
-
finally {
|
|
1665
|
-
notifyReady();
|
|
1666
|
-
moveTo(settled(status, progress, completedAt, pending(), attention()));
|
|
1667
|
-
}
|
|
1668
|
-
}
|
|
1669
|
-
async function run() {
|
|
1670
|
-
try {
|
|
1671
|
-
await connect();
|
|
1672
|
-
}
|
|
1673
|
-
finally {
|
|
1674
|
-
inflight = false;
|
|
1675
|
-
if (queued && !closed) {
|
|
1676
|
-
queued = false;
|
|
1677
|
-
void enqueue();
|
|
1678
|
-
}
|
|
1679
|
-
}
|
|
1680
|
-
}
|
|
1681
|
-
function observeRoomVersions() {
|
|
1682
|
-
const path = options.adapter.roomVersions;
|
|
1683
|
-
if (path === undefined || withdrawNotifications !== undefined)
|
|
1684
|
-
return;
|
|
1685
|
-
withdrawNotifications = path.observe((notification) => {
|
|
1686
|
-
if (closed || stopping || discoveryPaused)
|
|
1687
|
-
return;
|
|
1688
|
-
if (notification.modelChanged === true && handed?.includes(notification.locator) === true) {
|
|
1689
|
-
void enqueue();
|
|
1690
|
-
return;
|
|
1691
|
-
}
|
|
1692
|
-
notices.set(notification.locator, notification.roomVersion);
|
|
1693
|
-
chain = chain.then(runNotified, runNotified);
|
|
1694
|
-
});
|
|
1695
|
-
}
|
|
1696
|
-
function writesHeld() {
|
|
1697
|
-
return serviceMode === 'write-halted' || haltedRefusal;
|
|
1698
|
-
}
|
|
1699
|
-
function signalled(mode) {
|
|
1700
|
-
if (closed)
|
|
1701
|
-
return;
|
|
1702
|
-
if (mode === 'open')
|
|
1703
|
-
openSignals += 1;
|
|
1704
|
-
const releasing = mode === 'open' && (haltedRefusal || serviceMode === 'write-halted');
|
|
1705
|
-
if (mode === 'open')
|
|
1706
|
-
haltedRefusal = false;
|
|
1707
|
-
if (mode !== serviceMode) {
|
|
1708
|
-
serviceMode = mode;
|
|
1709
|
-
journal.record(`service mode ${mode}`);
|
|
1710
|
-
for (const listener of serviceModeListeners)
|
|
1711
|
-
listener(mode);
|
|
1712
|
-
}
|
|
1713
|
-
if (releasing)
|
|
1714
|
-
void enqueue();
|
|
1715
|
-
}
|
|
1716
|
-
function observeConversations() {
|
|
1717
|
-
const stream = options.adapter.conversations;
|
|
1718
|
-
if (stream === undefined || withdrawConversations !== undefined)
|
|
1719
|
-
return;
|
|
1720
|
-
withdrawConversations = stream.observe((event) => {
|
|
1721
|
-
conversations.receive(event);
|
|
1722
|
-
if (event.kind !== 'gap' || conversations.current())
|
|
1723
|
-
return;
|
|
1724
|
-
show();
|
|
1725
|
-
scheduleCurrency();
|
|
1726
|
-
});
|
|
1727
|
-
}
|
|
1728
|
-
function scheduleCurrency() {
|
|
1729
|
-
if (currencyQueued || closed || stopping)
|
|
1730
|
-
return;
|
|
1731
|
-
currencyQueued = true;
|
|
1732
|
-
chain = chain.then(runCurrency, runCurrency);
|
|
1733
|
-
}
|
|
1734
|
-
async function runCurrency() {
|
|
1735
|
-
currencyQueued = false;
|
|
1736
|
-
if (closed || stopping || discoveryPaused)
|
|
1737
|
-
return;
|
|
1738
|
-
await conversations.reconcile();
|
|
1739
|
-
if (conversations.current()) {
|
|
1740
|
-
show();
|
|
1741
|
-
return;
|
|
1742
|
-
}
|
|
1743
|
-
const resting = status;
|
|
1744
|
-
const progress = { complete: false, reachable: !resting.providerUnreachable };
|
|
1745
|
-
moveTo(transferring(resting));
|
|
1746
|
-
try {
|
|
1747
|
-
await conversations.catchUp(progress);
|
|
1748
|
-
}
|
|
1749
|
-
catch {
|
|
1750
|
-
progress.reachable = false;
|
|
1751
|
-
}
|
|
1752
|
-
finally {
|
|
1753
|
-
if (!closed) {
|
|
1754
|
-
moveTo(settled(resting, progress, resting.lastCompleted, pending(), attention()));
|
|
1755
|
-
}
|
|
1756
|
-
}
|
|
1757
|
-
}
|
|
1758
|
-
function handSelected(locators) {
|
|
1759
|
-
const path = options.adapter.roomVersions;
|
|
1760
|
-
if (path === undefined || withdrawNotifications === undefined)
|
|
1761
|
-
return;
|
|
1762
|
-
if (handed !== undefined && JSON.stringify(locators) === JSON.stringify(handed))
|
|
1763
|
-
return;
|
|
1764
|
-
handed = locators;
|
|
1765
|
-
path.select(locators);
|
|
1766
|
-
}
|
|
1767
|
-
async function runNotified() {
|
|
1768
|
-
const announced = [...notices];
|
|
1769
|
-
notices.clear();
|
|
1770
|
-
if (announced.length === 0 || closed || stopping || discoveryPaused)
|
|
1771
|
-
return;
|
|
1772
|
-
const resting = status;
|
|
1773
|
-
const progress = { complete: false, reachable: !resting.providerUnreachable };
|
|
1774
|
-
moveTo(transferring(resting));
|
|
1775
|
-
try {
|
|
1776
|
-
const local = await openLocal();
|
|
1777
|
-
for (const [locator, roomVersion] of announced) {
|
|
1778
|
-
if (closed || discoveryPaused || authorization === undefined)
|
|
1779
|
-
return;
|
|
1780
|
-
if (handed?.includes(locator) !== true)
|
|
1781
|
-
continue;
|
|
1782
|
-
const room = discovered.get(locator);
|
|
1783
|
-
const held = replicas.get(locator);
|
|
1784
|
-
if (room === undefined)
|
|
1785
|
-
continue;
|
|
1786
|
-
if (sourceRefusals.has(locator)) {
|
|
1787
|
-
void enqueue();
|
|
1788
|
-
continue;
|
|
1789
|
-
}
|
|
1790
|
-
if (held === undefined)
|
|
1791
|
-
continue;
|
|
1792
|
-
if (held.confirmed.record.roomVersion === roomVersion)
|
|
1793
|
-
continue;
|
|
1794
|
-
const withheld = await advance(room, local, authorization);
|
|
1795
|
-
if (withheld === null)
|
|
1796
|
-
continue;
|
|
1797
|
-
if (withheld.kind === 'refused') {
|
|
1798
|
-
void enqueue();
|
|
1799
|
-
continue;
|
|
1800
|
-
}
|
|
1801
|
-
if (withheld.kind === 'scoped-refusal') {
|
|
1802
|
-
await scoped(withheld, room, local);
|
|
1803
|
-
continue;
|
|
1804
|
-
}
|
|
1805
|
-
if (withheld.kind === 'transport-uncertainty') {
|
|
1806
|
-
progress.reachable = false;
|
|
1807
|
-
continue;
|
|
1808
|
-
}
|
|
1809
|
-
if (withheld.kind === 'room-not-readable') {
|
|
1810
|
-
unreadable.add(locator);
|
|
1811
|
-
list(room, false);
|
|
1812
|
-
emitRooms();
|
|
1813
|
-
continue;
|
|
1814
|
-
}
|
|
1815
|
-
if (withheld.kind === 'revoked-authorization') {
|
|
1816
|
-
await refuse('revoked-authorization');
|
|
1817
|
-
return;
|
|
1818
|
-
}
|
|
1819
|
-
}
|
|
1820
|
-
}
|
|
1821
|
-
catch {
|
|
1822
|
-
progress.reachable = false;
|
|
1823
|
-
}
|
|
1824
|
-
finally {
|
|
1825
|
-
if (!closed) {
|
|
1826
|
-
moveTo(settled(resting, progress, resting.lastCompleted, pending(), attention()));
|
|
1827
|
-
}
|
|
1828
|
-
}
|
|
1829
|
-
}
|
|
1830
|
-
function enqueue() {
|
|
1831
|
-
if (stopping)
|
|
1832
|
-
return chain;
|
|
1833
|
-
if (inflight) {
|
|
1834
|
-
queued = true;
|
|
1835
|
-
return chain;
|
|
1836
|
-
}
|
|
1837
|
-
inflight = true;
|
|
1838
|
-
chain = chain.then(run, run);
|
|
1839
|
-
return chain;
|
|
1840
|
-
}
|
|
1841
|
-
function close() {
|
|
1842
|
-
if (closed)
|
|
1843
|
-
return;
|
|
1844
|
-
closed = true;
|
|
1845
|
-
withdrawNotifications?.();
|
|
1846
|
-
withdrawNotifications = undefined;
|
|
1847
|
-
withdrawConversations?.();
|
|
1848
|
-
withdrawConversations = undefined;
|
|
1849
|
-
withdrawServiceMode?.();
|
|
1850
|
-
conversations.close();
|
|
1851
|
-
stopTimer();
|
|
1852
|
-
store?.close();
|
|
1853
|
-
void opening?.then((opened) => {
|
|
1854
|
-
opened.close();
|
|
1855
|
-
}, () => { });
|
|
1856
|
-
roomListeners.clear();
|
|
1857
|
-
refusalListeners.clear();
|
|
1858
|
-
storageLossListeners.clear();
|
|
1859
|
-
statusListeners.clear();
|
|
1860
|
-
serviceModeListeners.clear();
|
|
1861
|
-
readSide.close();
|
|
1862
|
-
queueSubscriptions.clear();
|
|
1863
|
-
roomDraftListeners.clear();
|
|
1864
|
-
presentationListeners.clear();
|
|
1865
|
-
}
|
|
1866
|
-
async function stop() {
|
|
1867
|
-
stopping = true;
|
|
1868
|
-
stopTimer();
|
|
1869
|
-
await chain.catch(() => undefined);
|
|
1870
|
-
await mutations;
|
|
1871
|
-
close();
|
|
1872
|
-
}
|
|
1873
|
-
const withdrawServiceMode = options.adapter.serviceMode?.observe(signalled);
|
|
1874
|
-
observeConversations();
|
|
1875
|
-
options.signal?.addEventListener('abort', close, { once: true });
|
|
1876
|
-
if (options.signal?.aborted)
|
|
1877
|
-
close();
|
|
1878
|
-
void openLocal().catch(() => {
|
|
1879
|
-
notifyReady();
|
|
1880
|
-
});
|
|
1881
|
-
void enqueue();
|
|
1882
|
-
function watch(topic, input, deliver) {
|
|
1883
|
-
if (topic === 'room-drafts') {
|
|
1884
|
-
const listener = { notify: deliver, ready: false };
|
|
1885
|
-
roomDraftListeners.add(listener);
|
|
1886
|
-
void exclusive(async () => {
|
|
1887
|
-
await openLocal();
|
|
1888
|
-
if (closed || !roomDraftListeners.has(listener))
|
|
1889
|
-
return;
|
|
1890
|
-
listener.ready = true;
|
|
1891
|
-
}).then(() => {
|
|
1892
|
-
if (closed || !listener.ready || !roomDraftListeners.has(listener))
|
|
1893
|
-
return;
|
|
1894
|
-
listener.notify(snapshotRoomDrafts());
|
|
1895
|
-
}, () => { });
|
|
1896
|
-
return () => {
|
|
1897
|
-
roomDraftListeners.delete(listener);
|
|
1898
|
-
};
|
|
1899
|
-
}
|
|
1900
|
-
if (topic === 'conversation') {
|
|
1901
|
-
return conversations.watch(input[0], input[1], deliver);
|
|
1902
|
-
}
|
|
1903
|
-
if (topic === 'presentation') {
|
|
1904
|
-
const listener = {
|
|
1905
|
-
app: input[0],
|
|
1906
|
-
notify: deliver,
|
|
1907
|
-
ready: false,
|
|
1908
|
-
};
|
|
1909
|
-
presentationListeners.add(listener);
|
|
1910
|
-
void exclusive(async () => {
|
|
1911
|
-
await openLocal();
|
|
1912
|
-
}).then(() => {
|
|
1913
|
-
if (closed || !presentationListeners.has(listener))
|
|
1914
|
-
return;
|
|
1915
|
-
listener.ready = true;
|
|
1916
|
-
listener.notify(presentationOf(listener.app));
|
|
1917
|
-
}, () => { });
|
|
1918
|
-
return () => {
|
|
1919
|
-
presentationListeners.delete(listener);
|
|
1920
|
-
};
|
|
1921
|
-
}
|
|
1922
|
-
if (topic === 'rooms') {
|
|
1923
|
-
roomListeners.add(deliver);
|
|
1924
|
-
deliver(listed());
|
|
1925
|
-
return () => {
|
|
1926
|
-
roomListeners.delete(deliver);
|
|
1927
|
-
};
|
|
1928
|
-
}
|
|
1929
|
-
if (topic === 'connection-refusal') {
|
|
1930
|
-
refusalListeners.add(deliver);
|
|
1931
|
-
deliver(refusal);
|
|
1932
|
-
return () => {
|
|
1933
|
-
refusalListeners.delete(deliver);
|
|
1934
|
-
};
|
|
1935
|
-
}
|
|
1936
|
-
if (topic === 'storage-loss') {
|
|
1937
|
-
storageLossListeners.add(deliver);
|
|
1938
|
-
deliver(storageLoss);
|
|
1939
|
-
return () => {
|
|
1940
|
-
storageLossListeners.delete(deliver);
|
|
1941
|
-
};
|
|
1942
|
-
}
|
|
1943
|
-
if (topic === 'sync-status') {
|
|
1944
|
-
statusListeners.add(deliver);
|
|
1945
|
-
deliver(shown);
|
|
1946
|
-
return () => {
|
|
1947
|
-
statusListeners.delete(deliver);
|
|
1948
|
-
};
|
|
1949
|
-
}
|
|
1950
|
-
if (topic === 'service-mode') {
|
|
1951
|
-
serviceModeListeners.add(deliver);
|
|
1952
|
-
deliver(serviceMode);
|
|
1953
|
-
return () => {
|
|
1954
|
-
serviceModeListeners.delete(deliver);
|
|
1955
|
-
};
|
|
1956
|
-
}
|
|
1957
|
-
if (topic === 'queue') {
|
|
1958
|
-
const queue = { locator: input[0], notify: deliver };
|
|
1959
|
-
queueSubscriptions.add(queue);
|
|
1960
|
-
queue.notify(entriesOf(queue.locator));
|
|
1961
|
-
return () => {
|
|
1962
|
-
queueSubscriptions.delete(queue);
|
|
1963
|
-
};
|
|
1964
|
-
}
|
|
1965
|
-
const plan = planOf(input[0], roomFacts());
|
|
1966
|
-
return readSide.watch(plan, identityOf(plan), deliver);
|
|
1967
|
-
}
|
|
1968
|
-
const md = {
|
|
1969
|
-
loaded() {
|
|
1970
|
-
return openLocal().then(() => undefined);
|
|
1971
|
-
},
|
|
1972
|
-
rooms() {
|
|
1973
|
-
return listed();
|
|
1974
42
|
},
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
return searchIndex.search(plan, selected);
|
|
1983
|
-
},
|
|
1984
|
-
types(locator) {
|
|
1985
|
-
return {
|
|
1986
|
-
collection: modelTypes(modelOf(locator)),
|
|
1987
|
-
typedSelection: rooms.get(locator)?.typedSelection === undefined ||
|
|
1988
|
-
rooms.get(locator)?.typedSelection === null
|
|
1989
|
-
? null
|
|
1990
|
-
: modelTypes(modelOf(locator, 'typed-selection')),
|
|
1991
|
-
};
|
|
1992
|
-
},
|
|
1993
|
-
roomDraft: {
|
|
1994
|
-
list() {
|
|
1995
|
-
return exclusive(async () => {
|
|
1996
|
-
await openLocal();
|
|
1997
|
-
return snapshotRoomDrafts();
|
|
1998
|
-
}).then((listed) => {
|
|
1999
|
-
publishRoomDrafts(true);
|
|
2000
|
-
return listed;
|
|
2001
|
-
});
|
|
2002
|
-
},
|
|
2003
|
-
read(room, path) {
|
|
2004
|
-
const record = roomDrafts.get(room)?.get(path);
|
|
2005
|
-
return record === undefined ? null : roomDraftOf(record);
|
|
2006
|
-
},
|
|
2007
|
-
write(room, path, revision, input) {
|
|
2008
|
-
return exclusive(async () => {
|
|
2009
|
-
const parts = readParts(input);
|
|
2010
|
-
if (path === '' || normalize(path) !== path)
|
|
2011
|
-
throw new Error('room-draft-malformed');
|
|
2012
|
-
const local = await openLocal();
|
|
2013
|
-
const current = roomDrafts.get(room)?.get(path);
|
|
2014
|
-
if ((current?.revision ?? null) !== revision) {
|
|
2015
|
-
return {
|
|
2016
|
-
outcome: 'stale',
|
|
2017
|
-
current: current === undefined ? null : roomDraftOf(current),
|
|
2018
|
-
};
|
|
2019
|
-
}
|
|
2020
|
-
const record = {
|
|
2021
|
-
locator: room,
|
|
2022
|
-
path,
|
|
2023
|
-
revision: roomDraftRevision(),
|
|
2024
|
-
parts: held(current?.parts, parts),
|
|
2025
|
-
returned: current?.returned ?? null,
|
|
2026
|
-
};
|
|
2027
|
-
await local.keepRoomDraft(record);
|
|
2028
|
-
rememberRoomDraft(record);
|
|
2029
|
-
publishChangeSet(changeSet(room, null, null, { drafts: [path] }));
|
|
2030
|
-
return { outcome: 'written', roomDraft: roomDraftOf(record) };
|
|
2031
|
-
});
|
|
2032
|
-
},
|
|
2033
|
-
discard(room, path, revision) {
|
|
2034
|
-
return exclusive(async () => {
|
|
2035
|
-
const local = await openLocal();
|
|
2036
|
-
const current = roomDrafts.get(room)?.get(path);
|
|
2037
|
-
if (current === undefined || current.revision !== revision) {
|
|
2038
|
-
return {
|
|
2039
|
-
outcome: 'stale',
|
|
2040
|
-
current: current === undefined ? null : roomDraftOf(current),
|
|
2041
|
-
};
|
|
2042
|
-
}
|
|
2043
|
-
await local.discardRoomDraft(room, path);
|
|
2044
|
-
forgetRoomDraft(room, path);
|
|
2045
|
-
publishChangeSet(changeSet(room, null, null, { drafts: [path] }));
|
|
2046
|
-
moveTo({ ...status, ...attention() });
|
|
2047
|
-
return { outcome: 'written', roomDraft: null };
|
|
2048
|
-
});
|
|
2049
|
-
},
|
|
43
|
+
roomTypes,
|
|
44
|
+
builtIn,
|
|
45
|
+
typedPaths,
|
|
46
|
+
persistence: shell.persistence,
|
|
47
|
+
opening: (local) => open(runtime, local),
|
|
48
|
+
unpublished(error) {
|
|
49
|
+
journal.record(`publication failed ${error instanceof Error ? error.name : 'unknown'}`);
|
|
2050
50
|
},
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
51
|
+
changed: (set) => publishChangeSet(runtime, set),
|
|
52
|
+
deliverReadiness: (owed) => deliverOwedReadiness(runtime, owed),
|
|
53
|
+
spaceFreed: () => spaceFreed(runtime),
|
|
54
|
+
});
|
|
55
|
+
const recovery = (options.recovery ?? createRecovery()).scope(ROOM_ATTEMPTS);
|
|
56
|
+
const runtime = {
|
|
57
|
+
store,
|
|
58
|
+
socket,
|
|
59
|
+
journal,
|
|
60
|
+
recovery,
|
|
61
|
+
shell,
|
|
62
|
+
localSaves: adapter.localSaves === true,
|
|
63
|
+
roomTypes,
|
|
64
|
+
builtIn,
|
|
65
|
+
typedPaths,
|
|
66
|
+
usableWhenHeld: options.usableWhenHeld ?? [],
|
|
67
|
+
build: options.build ?? '',
|
|
68
|
+
conversations: createConversationStore({
|
|
69
|
+
call: (name, input) => invokeAction(runtime, name, input),
|
|
70
|
+
history: (request) => readConversationHistory(runtime, request),
|
|
71
|
+
durably: store.durably,
|
|
72
|
+
local: store.open,
|
|
73
|
+
again: () => {
|
|
74
|
+
void socket.ready.then(() => scheduleCurrency(runtime));
|
|
2059
75
|
},
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
const local = await openLocal();
|
|
2063
|
-
const stored = structuredClone(value);
|
|
2064
|
-
await local.present({ app, value: stored });
|
|
2065
|
-
presentations.set(app, stored);
|
|
2066
|
-
publishChangeSet(changeSet('', null, null, { presentation: app }));
|
|
2067
|
-
});
|
|
76
|
+
settled: () => {
|
|
77
|
+
show(runtime);
|
|
2068
78
|
},
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
},
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
discoveryPaused = false;
|
|
2101
|
-
await enqueue();
|
|
2102
|
-
},
|
|
79
|
+
stream: socket.conversations,
|
|
80
|
+
publish: store.changed,
|
|
81
|
+
online: socket.reached,
|
|
82
|
+
full: store.space.full,
|
|
83
|
+
closed: socket.closed,
|
|
84
|
+
unanswered: () => resendLater(runtime),
|
|
85
|
+
}),
|
|
86
|
+
liveQueries: createLiveQueries((plan, cover) => answeredPlan(runtime, plan, cover), store.covering),
|
|
87
|
+
figures: createFigures({
|
|
88
|
+
cover: () => figuresCover(runtime),
|
|
89
|
+
rooms: (rooms, since) => roomTallies(runtime, rooms, since),
|
|
90
|
+
conversations: (conversations, since) => store.ask({ kind: 'conversation.tallies', conversations, since }),
|
|
91
|
+
}),
|
|
92
|
+
lastAnswers: readAnswers({
|
|
93
|
+
database: store,
|
|
94
|
+
build: options.build ?? '',
|
|
95
|
+
unkept: adapter.unkeptReads ?? [],
|
|
96
|
+
leading: adapter.leadingReads ?? [],
|
|
97
|
+
recovery: recovery.scope(),
|
|
98
|
+
call: (name, input) => called(runtime, name, input),
|
|
99
|
+
online: socket.reached,
|
|
100
|
+
closed: socket.closed,
|
|
101
|
+
full: store.space.full,
|
|
102
|
+
filled: () => filled(runtime),
|
|
103
|
+
}),
|
|
104
|
+
settle: (...held) => settle(runtime, ...held),
|
|
105
|
+
scoped: (...held) => scoped(runtime, ...held),
|
|
106
|
+
submitLater: (locator) => submitLater(runtime, locator),
|
|
107
|
+
unpair: (local) => unpair(runtime, local),
|
|
108
|
+
advanced: (...held) => advanced(runtime, ...held),
|
|
109
|
+
withhold: (...held) => withhold(runtime, ...held),
|
|
2103
110
|
};
|
|
2104
|
-
|
|
111
|
+
start(runtime, options.signal);
|
|
112
|
+
return composedRuntime(runtime);
|
|
2105
113
|
}
|