@agentshouse/mdruntime 0.1.0-ce6bc9239a3ee → 0.1.0-ce80727f9590f
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 +21 -19
- package/dist/acceptance.d.ts +2 -25
- package/dist/acceptance.d.ts.map +1 -1
- package/dist/acceptance.js +119 -204
- package/dist/act-lanes.d.ts +30 -0
- package/dist/act-lanes.d.ts.map +1 -0
- package/dist/act-lanes.js +225 -0
- package/dist/actions.d.ts +26 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/actions.js +321 -0
- package/dist/adapter.d.ts +114 -57
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attached-model.d.ts +0 -1
- package/dist/attached-model.d.ts.map +1 -1
- package/dist/attached-model.js +0 -5
- package/dist/authored.d.ts +88 -0
- package/dist/authored.d.ts.map +1 -0
- package/dist/authored.js +345 -0
- package/dist/bootstrap.d.ts +1 -1
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +6 -18
- package/dist/cell.d.ts +6 -0
- package/dist/cell.d.ts.map +1 -0
- package/dist/cell.js +9 -0
- package/dist/change-set.d.ts +4 -0
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +6 -0
- package/dist/change.d.ts +4 -0
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +7 -2
- package/dist/chat-types.d.ts +2 -2
- package/dist/chat-types.d.ts.map +1 -1
- package/dist/chat-types.js +11 -2
- package/dist/client.d.ts +25 -22
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +45 -58
- package/dist/collection.d.ts +26 -4
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +28 -10
- package/dist/column.d.ts +5 -0
- package/dist/column.d.ts.map +1 -0
- package/dist/column.js +9 -0
- package/dist/compile.d.ts +2 -0
- package/dist/compile.d.ts.map +1 -1
- package/dist/compile.js +31 -25
- package/dist/connection.d.ts +4 -116
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +103 -2994
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +1 -1
- package/dist/conversation-live.d.ts +150 -0
- package/dist/conversation-live.d.ts.map +1 -0
- package/dist/conversation-live.js +259 -0
- package/dist/conversation-row.d.ts +8 -1
- package/dist/conversation-row.d.ts.map +1 -1
- package/dist/conversation-row.js +22 -3
- package/dist/conversation.d.ts +57 -45
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +974 -1031
- package/dist/declaration.d.ts +10 -0
- package/dist/declaration.d.ts.map +1 -0
- package/dist/declaration.js +24 -0
- package/dist/discovery.d.ts +12 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +175 -0
- package/dist/download.d.ts +6 -0
- package/dist/download.d.ts.map +1 -0
- package/dist/download.js +42 -0
- package/dist/durability.d.ts +14 -0
- package/dist/durability.d.ts.map +1 -1
- package/dist/durability.js +7 -0
- package/dist/entry.d.ts +5 -0
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +23 -12
- package/dist/expectation.d.ts +26 -13
- package/dist/expectation.d.ts.map +1 -1
- package/dist/expectation.js +72 -19
- package/dist/failure.d.ts +3 -0
- package/dist/failure.d.ts.map +1 -1
- package/dist/failure.js +8 -0
- package/dist/figures.d.ts +1 -0
- package/dist/figures.d.ts.map +1 -1
- package/dist/figures.js +6 -4
- package/dist/file.d.ts +1 -1
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +3 -2
- package/dist/holdings.d.ts +29 -0
- package/dist/holdings.d.ts.map +1 -0
- package/dist/holdings.js +95 -0
- package/dist/host.d.ts +20 -2
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +83 -19
- package/dist/house-query.d.ts +21 -0
- package/dist/house-query.d.ts.map +1 -0
- package/dist/house-query.js +41 -0
- package/dist/index.d.ts +28 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -6
- package/dist/install.d.ts +22 -48
- package/dist/install.d.ts.map +1 -1
- package/dist/install.js +161 -161
- package/dist/lifecycle.d.ts +7 -0
- package/dist/lifecycle.d.ts.map +1 -0
- package/dist/lifecycle.js +290 -0
- package/dist/listing.d.ts +60 -0
- package/dist/listing.d.ts.map +1 -0
- package/dist/listing.js +168 -0
- package/dist/live.d.ts +5 -3
- package/dist/live.d.ts.map +1 -1
- package/dist/live.js +74 -33
- package/dist/loaded.d.ts +5 -0
- package/dist/loaded.d.ts.map +1 -0
- package/dist/loaded.js +40 -0
- package/dist/locks.d.ts +7 -0
- package/dist/locks.d.ts.map +1 -0
- package/dist/locks.js +34 -0
- package/dist/lost.d.ts +5 -0
- package/dist/lost.d.ts.map +1 -0
- package/dist/lost.js +49 -0
- package/dist/media.d.ts +19 -2
- package/dist/media.d.ts.map +1 -1
- package/dist/media.js +105 -27
- package/dist/memory.d.ts +9 -3
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +94 -32
- package/dist/messages.d.ts +12 -6
- package/dist/messages.d.ts.map +1 -1
- package/dist/messages.js +35 -39
- package/dist/modal.d.ts +2 -0
- package/dist/modal.d.ts.map +1 -0
- package/dist/modal.js +11 -0
- package/dist/model.d.ts +4 -3
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +6 -5
- package/dist/models.d.ts +10 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +153 -0
- package/dist/observation.d.ts +29 -12
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +217 -85
- package/dist/online.d.ts +7 -0
- package/dist/online.d.ts.map +1 -0
- package/dist/online.js +29 -0
- package/dist/opened.d.ts +1 -1
- package/dist/opened.d.ts.map +1 -1
- package/dist/opened.js +5 -5
- package/dist/opening.d.ts +4 -0
- package/dist/opening.d.ts.map +1 -0
- package/dist/opening.js +70 -0
- package/dist/outcome.d.ts +16 -2
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +32 -2
- package/dist/overlay.d.ts +6 -2
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +37 -24
- package/dist/page-metadata.d.ts +8 -0
- package/dist/page-metadata.d.ts.map +1 -0
- package/dist/page-metadata.js +19 -0
- package/dist/parts.d.ts +5 -58
- package/dist/parts.d.ts.map +1 -1
- package/dist/parts.js +28 -110
- package/dist/paths.d.ts +1 -0
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +22 -0
- package/dist/preparation.d.ts +41 -0
- package/dist/preparation.d.ts.map +1 -0
- package/dist/preparation.js +326 -0
- package/dist/presence.d.ts +58 -0
- package/dist/presence.d.ts.map +1 -0
- package/dist/presence.js +231 -0
- package/dist/queries.d.ts +16 -0
- package/dist/queries.d.ts.map +1 -0
- package/dist/queries.js +287 -0
- package/dist/query.d.ts +0 -1
- package/dist/query.d.ts.map +1 -1
- package/dist/questions.d.ts +3 -5
- package/dist/questions.d.ts.map +1 -1
- package/dist/questions.js +4 -6
- package/dist/queue.d.ts +40 -0
- package/dist/queue.d.ts.map +1 -0
- package/dist/queue.js +480 -0
- package/dist/reach.d.ts +15 -5
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +164 -33
- package/dist/react.d.ts +17 -2
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +33 -1
- package/dist/readiness.d.ts +9 -4
- package/dist/readiness.d.ts.map +1 -1
- package/dist/readiness.js +3 -5
- package/dist/reads.d.ts +5 -2
- package/dist/reads.d.ts.map +1 -1
- package/dist/reads.js +49 -7
- package/dist/recovery.d.ts +4 -1
- package/dist/recovery.d.ts.map +1 -1
- package/dist/recovery.js +31 -8
- package/dist/refresh.js +2 -2
- package/dist/refusal.d.ts +5 -0
- package/dist/refusal.d.ts.map +1 -0
- package/dist/refusal.js +55 -0
- package/dist/replica.d.ts +5 -10
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +17 -18
- package/dist/retried.d.ts +30 -0
- package/dist/retried.d.ts.map +1 -0
- package/dist/retried.js +72 -0
- package/dist/room-draft.d.ts +31 -8
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +273 -66
- package/dist/room-drafts.d.ts +7 -12
- package/dist/room-drafts.d.ts.map +1 -1
- package/dist/room-drafts.js +44 -40
- package/dist/room-list.d.ts +10 -0
- package/dist/room-list.d.ts.map +1 -0
- package/dist/room-list.js +45 -0
- package/dist/rows.d.ts +2 -1
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +9 -10
- package/dist/running.d.ts +5 -0
- package/dist/running.d.ts.map +1 -0
- package/dist/running.js +10 -0
- package/dist/runtime.d.ts +36 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +1 -0
- package/dist/save.d.ts +42 -86
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +326 -225
- package/dist/saves.d.ts +8 -70
- package/dist/saves.d.ts.map +1 -1
- package/dist/saves.js +199 -761
- package/dist/seed.d.ts +3 -8
- package/dist/seed.d.ts.map +1 -1
- package/dist/seed.js +21 -17
- package/dist/shared.d.ts +1 -6
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +13 -95
- package/dist/shown.d.ts +35 -0
- package/dist/shown.d.ts.map +1 -0
- package/dist/shown.js +326 -0
- package/dist/socket/lanes.d.ts +28 -0
- package/dist/socket/lanes.d.ts.map +1 -0
- package/dist/socket/lanes.js +103 -0
- package/dist/socket/session.d.ts +41 -0
- package/dist/socket/session.d.ts.map +1 -0
- package/dist/socket/session.js +26 -0
- package/dist/socket.d.ts +30 -5
- package/dist/socket.d.ts.map +1 -1
- package/dist/socket.js +75 -6
- package/dist/source.d.ts +5 -7
- package/dist/source.d.ts.map +1 -1
- package/dist/source.js +18 -6
- package/dist/store/installs.d.ts +2 -2
- package/dist/store/installs.d.ts.map +1 -1
- package/dist/store/installs.js +10 -4
- package/dist/store/readiness.d.ts +11 -0
- package/dist/store/readiness.d.ts.map +1 -0
- package/dist/store/readiness.js +68 -0
- package/dist/store/records.d.ts +22 -8
- package/dist/store/records.d.ts.map +1 -1
- package/dist/store/records.js +84 -24
- package/dist/store/rooms-shown.d.ts +8 -0
- package/dist/store/rooms-shown.d.ts.map +1 -0
- package/dist/store/rooms-shown.js +13 -0
- package/dist/store/space.d.ts +11 -0
- package/dist/store/space.d.ts.map +1 -0
- package/dist/store/space.js +41 -0
- package/dist/store/watchers.d.ts +2 -0
- package/dist/store/watchers.d.ts.map +1 -1
- package/dist/store/watchers.js +23 -3
- package/dist/store.d.ts +50 -9
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +78 -17
- package/dist/stored.d.ts +25 -14
- package/dist/stored.d.ts.map +1 -1
- package/dist/stored.js +99 -36
- package/dist/surface.d.ts +94 -0
- package/dist/surface.d.ts.map +1 -0
- package/dist/surface.js +255 -0
- package/dist/sync.d.ts +21 -0
- package/dist/sync.d.ts.map +1 -0
- package/dist/sync.js +557 -0
- package/dist/tables.d.ts +28 -59
- package/dist/tables.d.ts.map +1 -1
- package/dist/tables.js +106 -331
- package/dist/tallies.d.ts.map +1 -1
- package/dist/tallies.js +6 -7
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +4 -10
- package/dist/wire.d.ts +68 -8
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +26 -9
- package/package.json +3 -2
- package/dist/attempt.d.ts +0 -16
- package/dist/attempt.d.ts.map +0 -1
- package/dist/attempt.js +0 -70
- package/dist/job.d.ts +0 -33
- package/dist/job.d.ts.map +0 -1
- package/dist/job.js +0 -54
- package/dist/log.d.ts +0 -6
- package/dist/log.d.ts.map +0 -1
- package/dist/log.js +0 -14
- package/dist/store/fences.d.ts +0 -16
- package/dist/store/fences.d.ts.map +0 -1
- package/dist/store/fences.js +0 -26
package/dist/connection.js
CHANGED
|
@@ -1,3017 +1,126 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
1
|
+
import { notReadyWaitMs } from '@agentshouse/mdmodel';
|
|
2
|
+
import { submitLater } from './acceptance.js';
|
|
3
|
+
import { invokeAction, called, readConversationHistory, resendLater } from './actions.js';
|
|
4
|
+
import { builtInModel, NO_ROOM_TYPES } from './attached-model.js';
|
|
5
|
+
import { createLiveConversations } from './conversation-live.js';
|
|
6
|
+
import { writeRule } from './declaration.js';
|
|
7
|
+
import { refusedForSpace } from './durability.js';
|
|
8
|
+
import { uncaught } from './failure.js';
|
|
9
|
+
import { createFigures } from './figures.js';
|
|
10
|
+
import { scheduleCurrency, spaceFreed, start } from './lifecycle.js';
|
|
7
11
|
import { createLiveQueries } from './live.js';
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import { behind, catching, caughtUpTo, forgetInstall, installationOf, publish, pullBootstrap, pullRefresh, reachedBy, replaced, roomInFlight, standingAt, writtenAfter, } from './install.js';
|
|
13
|
-
import { changeSet, mergeChangeSets } from './change-set.js';
|
|
14
|
-
import { createConversationStore, } from './conversation.js';
|
|
15
|
-
import { composedOf, validRoot } from './collection.js';
|
|
16
|
-
import { createLog } from './log.js';
|
|
17
|
-
import { ABSENT_TARGET, calledResult, failure, historyAnswer, LOCKED_UNREADABLE, lockedRoom, OFFLINE, undeclared, UNCERTAIN, UNREADABLE, } from './outcome.js';
|
|
12
|
+
import { open } from './opening.js';
|
|
13
|
+
import { deliverOwedReadiness, filled, prepare } from './preparation.js';
|
|
14
|
+
import { createPresence } from './presence.js';
|
|
15
|
+
import { answeredPlan, figuresCover, roomTallies } from './queries.js';
|
|
18
16
|
import { readAnswers } from './reads.js';
|
|
19
17
|
import { createRecovery } from './recovery.js';
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
22
|
-
import { detectStorageLoss, nextGeneration, refusedForSpace, } from './durability.js';
|
|
23
|
-
import { readinessOf, } from './readiness.js';
|
|
24
|
-
import { yielded } from './turn.js';
|
|
25
|
-
import { beneath } from './paths.js';
|
|
26
|
-
import { createStore, } from './store.js';
|
|
27
|
-
import { emitPresentation, presentationSurface, publishRoomDrafts, roomDraftSurface, watchPresentation, watchRoomDrafts, } from './room-drafts.js';
|
|
18
|
+
import { createRetries } from './retried.js';
|
|
19
|
+
import { publishChangeSet, show } from './shown.js';
|
|
28
20
|
import { createSocket } from './socket.js';
|
|
29
|
-
import {
|
|
30
|
-
import {
|
|
31
|
-
import {
|
|
32
|
-
import { submitAll, submitLater, unpair } from './acceptance.js';
|
|
33
|
-
function within(root, path) {
|
|
34
|
-
return root === '' || beneath(root, path);
|
|
35
|
-
}
|
|
36
|
-
function declaredSelection(room) {
|
|
37
|
-
const selection = room.typedSelection;
|
|
38
|
-
if (selection === undefined)
|
|
39
|
-
return true;
|
|
40
|
-
if (!Array.isArray(selection.roots) || selection.roots.length === 0)
|
|
41
|
-
return false;
|
|
42
|
-
return selection.roots.every((root, index) => validRoot(root) &&
|
|
43
|
-
root !== '' &&
|
|
44
|
-
selection.roots.every((other, at) => at === index || !within(other, root)));
|
|
45
|
-
}
|
|
46
|
-
const OPENING_LANE = 'opening';
|
|
47
|
-
const CONVERSATION_LANE = 'conversations';
|
|
48
|
-
const ROOM_READS = 256;
|
|
21
|
+
import { createStore } from './store.js';
|
|
22
|
+
import { composedRuntime } from './surface.js';
|
|
23
|
+
import { adopt, discover } from './sync.js';
|
|
49
24
|
const ROOM_ATTEMPTS = 16;
|
|
50
|
-
const ERASE_BATCH = 512;
|
|
51
|
-
const QUERY_BYTES = 1 << 20;
|
|
52
|
-
// A Room the Provider answers not ready is asked again after these waits, the last repeating, so a chunk build that catches up within seconds installs at once and a long one is asked every 15 seconds.
|
|
53
|
-
const NOT_READY_MS = [1_000, 2_000, 3_000, 5_000, 8_000, 11_000, 15_000];
|
|
54
|
-
// A Room that locks offline stays readable this long after the connection drops, so a laptop waking or changing network keeps working while a disconnected device locks soon after.
|
|
55
|
-
const LOCK_GRACE_MS = 120_000;
|
|
56
|
-
function operationOf(input) {
|
|
57
|
-
const operation = input?.operation_id;
|
|
58
|
-
return typeof operation === 'string' ? operation : null;
|
|
59
|
-
}
|
|
60
25
|
export function composeRuntime(options) {
|
|
61
|
-
const
|
|
62
|
-
const roomTypes =
|
|
63
|
-
let authorization = options.authorization;
|
|
64
|
-
let discoveries = 0;
|
|
65
|
-
const usableWhenHeld = options.usableWhenHeld ?? [];
|
|
66
|
-
const build = options.build ?? '';
|
|
67
|
-
const declared = new Map();
|
|
68
|
-
const unheldModels = new Map();
|
|
69
|
-
const unreadable = new Set();
|
|
26
|
+
const { adapter, shell } = options;
|
|
27
|
+
const roomTypes = adapter.roomTypes ?? NO_ROOM_TYPES;
|
|
70
28
|
const builtIn = builtInModel(roomTypes);
|
|
71
|
-
const typedPaths =
|
|
72
|
-
const
|
|
73
|
-
const
|
|
74
|
-
const
|
|
75
|
-
async ask(request) {
|
|
76
|
-
try {
|
|
77
|
-
return await options.database.ask(request);
|
|
78
|
-
}
|
|
79
|
-
catch (error) {
|
|
80
|
-
if (refusedForSpace(error))
|
|
81
|
-
filled();
|
|
82
|
-
throw error;
|
|
83
|
-
}
|
|
84
|
-
},
|
|
85
|
-
};
|
|
86
|
-
const socket = createSocket(options.adapter);
|
|
29
|
+
const typedPaths = adapter.typedPaths ?? [];
|
|
30
|
+
const retries = createRetries(shell);
|
|
31
|
+
const socket = createSocket(adapter, retries, options.authorization);
|
|
32
|
+
const presence = createPresence(notReadyWaitMs);
|
|
87
33
|
const store = createStore({
|
|
88
|
-
|
|
34
|
+
retries,
|
|
35
|
+
database: {
|
|
36
|
+
async ask(request) {
|
|
37
|
+
try {
|
|
38
|
+
return await options.database.ask(request);
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
if (refusedForSpace(error))
|
|
42
|
+
filled(runtime);
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
},
|
|
89
47
|
roomTypes,
|
|
90
48
|
builtIn,
|
|
91
49
|
typedPaths,
|
|
92
|
-
|
|
93
|
-
|
|
50
|
+
persistence: shell.persistence,
|
|
51
|
+
lost: presence.lost,
|
|
52
|
+
losing: presence.losing,
|
|
53
|
+
opening: (local) => open(runtime, local),
|
|
54
|
+
unpublished: uncaught,
|
|
55
|
+
changed(set) {
|
|
56
|
+
if (set.unindexed.length > 0)
|
|
57
|
+
prepare(runtime, set.unindexed);
|
|
58
|
+
for (const locator of set.due)
|
|
59
|
+
submitLater(runtime, locator);
|
|
60
|
+
return publishChangeSet(runtime, set);
|
|
61
|
+
},
|
|
62
|
+
deliverReadiness: (owed, rooms) => deliverOwedReadiness(runtime, owed, rooms),
|
|
63
|
+
spaceFreed: () => spaceFreed(runtime),
|
|
94
64
|
});
|
|
95
|
-
const { covering, durably, exclusive, composed, publication } = store;
|
|
96
|
-
const openLocal = store.open;
|
|
97
|
-
const isFenced = store.fences.has;
|
|
98
|
-
const storedRooms = store.rooms;
|
|
99
|
-
const recordAt = store.record;
|
|
100
|
-
const workOf = store.work;
|
|
101
|
-
const replicaOf = store.replica;
|
|
102
|
-
const draftsOf = store.drafts;
|
|
103
|
-
const draftOf = store.draft;
|
|
104
|
-
const reads = socket.reads;
|
|
105
|
-
let roomsListed = false;
|
|
106
|
-
const scopedRefusals = new Map();
|
|
107
|
-
function modelIn(locator, stored) {
|
|
108
|
-
if (refusal?.code === 'revoked-authorization')
|
|
109
|
-
return { status: 'unavailable', reason: 'revoked' };
|
|
110
|
-
return stored?.record.model ?? unheldModels.get(locator) ?? NO_MODEL;
|
|
111
|
-
}
|
|
112
|
-
function compositionFor(record) {
|
|
113
|
-
return composed({
|
|
114
|
-
...record,
|
|
115
|
-
model: modelIn(record.locator, { record, indexed: true, documents: 0 }),
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
let refusal = null;
|
|
119
|
-
let storageLoss = null;
|
|
120
|
-
let status = { state: 'syncing', providerUnreachable: false, resting: false };
|
|
121
|
-
let shown = STARTING;
|
|
122
|
-
let discoveryPaused = false;
|
|
123
|
-
let closed = false;
|
|
124
|
-
let stopping = false;
|
|
125
|
-
let underway = null;
|
|
126
|
-
let streamed = null;
|
|
127
65
|
const recovery = (options.recovery ?? createRecovery()).scope(ROOM_ATTEMPTS);
|
|
128
|
-
const
|
|
129
|
-
const calling = new Set();
|
|
130
|
-
const repairable = new Map();
|
|
131
|
-
const unready = new Map();
|
|
132
|
-
let complete = false;
|
|
133
|
-
let recorded = false;
|
|
134
|
-
const stale = new Set();
|
|
135
|
-
const reloadsDue = new Set();
|
|
136
|
-
const lanes = new Map();
|
|
137
|
-
const roomLanes = new Map();
|
|
138
|
-
let roomWork = 0;
|
|
139
|
-
const roomReads = { free: ROOM_READS, waiting: [] };
|
|
140
|
-
const working = new Set();
|
|
141
|
-
const roomListeners = new Set();
|
|
142
|
-
const unlistedRooms = new Set();
|
|
143
|
-
const refusalListeners = new Set();
|
|
144
|
-
const storageLossListeners = new Set();
|
|
145
|
-
const statusListeners = new Set();
|
|
146
|
-
const queueSubscriptions = new Set();
|
|
147
|
-
const liveQueries = createLiveQueries((plan, cover) => answeredPlan(plan, cover), () => covering());
|
|
148
|
-
const figures = createFigures({
|
|
149
|
-
cover: figuresCover,
|
|
150
|
-
rooms: async (rooms, since) => database.ask({
|
|
151
|
-
kind: 'replica.tallies',
|
|
152
|
-
totals: { ...(await planned(ROOM_TOTALS, null)), limit: null },
|
|
153
|
-
rooms,
|
|
154
|
-
since,
|
|
155
|
-
}),
|
|
156
|
-
conversations: (conversations, since) => database.ask({ kind: 'conversation.tallies', conversations, since }),
|
|
157
|
-
});
|
|
158
|
-
let currencyQueued = null;
|
|
159
|
-
const confirmed = new Set();
|
|
160
|
-
let lockGrace = null;
|
|
161
|
-
const lockListeners = new Set();
|
|
162
|
-
let serviceMode = 'open';
|
|
163
|
-
let haltedRefusal = false;
|
|
164
|
-
let openSignals = 0;
|
|
165
|
-
const serviceModeListeners = new Set();
|
|
166
|
-
const memberListeners = new Set();
|
|
167
|
-
let authorityChanges = 0;
|
|
168
|
-
const authorityChangeListeners = new Set();
|
|
169
|
-
const unpreparedDemand = new Set();
|
|
170
|
-
const unpreparable = new Set();
|
|
171
|
-
let preparing = false;
|
|
172
|
-
let persistence = options.shell.persistence;
|
|
173
|
-
let heldWhenOpened = false;
|
|
174
|
-
let full = false;
|
|
175
|
-
let probing = null;
|
|
176
|
-
let testing = null;
|
|
177
|
-
const readinessWatches = new Set();
|
|
178
|
-
let readinessQueued = false;
|
|
179
|
-
const conversations = createConversationStore({
|
|
180
|
-
call: (name, input) => invokeAction(name, input),
|
|
181
|
-
history: (request) => readConversationHistory(request),
|
|
182
|
-
durably,
|
|
183
|
-
local: () => openLocal(),
|
|
184
|
-
again: () => {
|
|
185
|
-
void ready.then(scheduleCurrency);
|
|
186
|
-
},
|
|
187
|
-
settled: () => {
|
|
188
|
-
show();
|
|
189
|
-
},
|
|
190
|
-
stream: () => options.adapter.conversations,
|
|
191
|
-
publish: publishChangeSet,
|
|
192
|
-
online: providerReached,
|
|
193
|
-
full: () => full,
|
|
194
|
-
closed: () => closed,
|
|
195
|
-
unanswered: resendLater,
|
|
196
|
-
});
|
|
197
|
-
const lastAnswers = readAnswers({
|
|
198
|
-
database,
|
|
199
|
-
build,
|
|
200
|
-
unkept: options.adapter.unkeptReads ?? [],
|
|
201
|
-
leading: options.adapter.leadingReads ?? [],
|
|
202
|
-
recovery: recovery.scope(),
|
|
203
|
-
call: (name, input) => md.call(name, input),
|
|
204
|
-
online: providerReached,
|
|
205
|
-
closed: () => closed,
|
|
206
|
-
full: () => full,
|
|
207
|
-
filled,
|
|
208
|
-
});
|
|
209
|
-
let readyFromStore = () => { };
|
|
210
|
-
const ready = new Promise((resolve) => {
|
|
211
|
-
readyFromStore = resolve;
|
|
212
|
-
});
|
|
213
|
-
let notifyDiscovered = () => { };
|
|
214
|
-
const firstDiscovery = new Promise((resolve) => {
|
|
215
|
-
notifyDiscovered = resolve;
|
|
216
|
-
});
|
|
217
|
-
let roomsNamed = false;
|
|
218
|
-
const seeding = {
|
|
219
|
-
store,
|
|
220
|
-
reached: reads.reached,
|
|
221
|
-
listed: () => roomsListed,
|
|
222
|
-
declares: (room) => declared.has(room),
|
|
223
|
-
};
|
|
224
|
-
const drafting = {
|
|
225
|
-
store,
|
|
226
|
-
closed: () => closed,
|
|
227
|
-
publishChangeSet,
|
|
228
|
-
statusShown: () => moveTo(status),
|
|
229
|
-
};
|
|
230
|
-
const saver = {
|
|
66
|
+
const runtime = {
|
|
231
67
|
store,
|
|
232
68
|
socket,
|
|
233
|
-
|
|
234
|
-
localSaves,
|
|
235
|
-
ready,
|
|
236
|
-
firstDiscovery,
|
|
237
|
-
full: () => full,
|
|
238
|
-
closed: () => closed,
|
|
239
|
-
refused: () => refusal !== null,
|
|
240
|
-
roomsNamed: () => roomsNamed,
|
|
241
|
-
authorized: () => authorization,
|
|
242
|
-
ensureAuthorization,
|
|
243
|
-
connectionNow,
|
|
244
|
-
scoped,
|
|
245
|
-
refuse,
|
|
246
|
-
compositionFor,
|
|
247
|
-
prepare,
|
|
248
|
-
publishChangeSet,
|
|
249
|
-
pendingShown,
|
|
250
|
-
submitLater: (locator) => submitLater(accepting, locator),
|
|
251
|
-
};
|
|
252
|
-
const accepting = {
|
|
253
|
-
...saver,
|
|
69
|
+
presence,
|
|
254
70
|
recovery,
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
discovered: (locator) => discovered.get(locator),
|
|
259
|
-
readable(room) {
|
|
260
|
-
unreadable.delete(room.locator);
|
|
261
|
-
list(room, true);
|
|
262
|
-
},
|
|
263
|
-
unreadable: roomUnreadable,
|
|
264
|
-
advanced,
|
|
265
|
-
withhold,
|
|
266
|
-
writesHeld,
|
|
267
|
-
openSignals: () => openSignals,
|
|
268
|
-
haltedSince,
|
|
269
|
-
rest,
|
|
270
|
-
statusShown: () => moveTo(status),
|
|
271
|
-
};
|
|
272
|
-
const installer = {
|
|
273
|
-
store,
|
|
274
|
-
socket,
|
|
275
|
-
journal,
|
|
71
|
+
retries,
|
|
72
|
+
shell,
|
|
73
|
+
localSaves: adapter.localSaves === true,
|
|
276
74
|
roomTypes,
|
|
277
75
|
builtIn,
|
|
278
76
|
typedPaths,
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
usable(room) {
|
|
290
|
-
stale.delete(room.locator);
|
|
291
|
-
unpreparable.delete(room.locator);
|
|
292
|
-
if (unreadable.delete(room.locator))
|
|
293
|
-
scopedRefusals.delete(room.locator);
|
|
294
|
-
list(room, true);
|
|
295
|
-
},
|
|
296
|
-
publishChangeSet,
|
|
297
|
-
unpair: (local) => unpair(accepting, local),
|
|
298
|
-
statusShown: () => moveTo(status),
|
|
299
|
-
submitLater: (locator) => submitLater(accepting, locator),
|
|
300
|
-
};
|
|
301
|
-
function notifyReady() {
|
|
302
|
-
notifyDiscovered();
|
|
303
|
-
readyFromStore();
|
|
304
|
-
}
|
|
305
|
-
async function attended() {
|
|
306
|
-
const held = await (await openLocal()).status();
|
|
307
|
-
const attentionRequired = held.returned.flatMap((record) => record.returned === null || isFenced(record.locator)
|
|
308
|
-
? []
|
|
309
|
-
: [{ room: record.locator, path: record.path, returned: structuredClone(record.returned) }]);
|
|
310
|
-
attentionRequired.sort((left, right) => {
|
|
311
|
-
if (left.room !== right.room)
|
|
312
|
-
return left.room < right.room ? -1 : 1;
|
|
313
|
-
if (left.path !== right.path)
|
|
314
|
-
return left.path < right.path ? -1 : 1;
|
|
315
|
-
return 0;
|
|
316
|
-
});
|
|
317
|
-
const changed = new Map();
|
|
318
|
-
if (!localSaves) {
|
|
319
|
-
for (const change of held.unsent) {
|
|
320
|
-
if (!isFenced(change.room))
|
|
321
|
-
changed.set(`${change.room}\u0000${change.path}`, change);
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
const unsent = [...changed]
|
|
325
|
-
.sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0))
|
|
326
|
-
.map(([, change]) => change);
|
|
327
|
-
return {
|
|
328
|
-
queued: !localSaves && held.queued.some((locator) => !isFenced(locator)),
|
|
329
|
-
lastCompleted: held.lastCompleted,
|
|
330
|
-
attentionRequired,
|
|
331
|
-
unsent,
|
|
332
|
-
reset: held.resets.reduce((earliest, { locator, reset }) => isFenced(locator) || (earliest !== null && earliest <= reset) ? earliest : reset, null),
|
|
333
|
-
};
|
|
334
|
-
}
|
|
335
|
-
async function queuedWork() {
|
|
336
|
-
const held = await (await openLocal()).queued();
|
|
337
|
-
return !localSaves && held.some((locator) => !isFenced(locator));
|
|
338
|
-
}
|
|
339
|
-
function declarationOf(locator, stored) {
|
|
340
|
-
const held = declared.get(locator);
|
|
341
|
-
const room = stored?.record ?? held?.room ?? discovered.get(locator);
|
|
342
|
-
return room === undefined ? undefined : { room, available: held?.available ?? true };
|
|
343
|
-
}
|
|
344
|
-
function listedLocators(held) {
|
|
345
|
-
return [
|
|
346
|
-
...declared.keys(),
|
|
347
|
-
...[...held.keys()].filter((locator) => !declared.has(locator)),
|
|
348
|
-
].filter((locator) => !isFenced(locator));
|
|
349
|
-
}
|
|
350
|
-
function selectionModel() {
|
|
351
|
-
return refusal?.code === 'revoked-authorization'
|
|
352
|
-
? { status: 'unavailable', reason: 'revoked' }
|
|
353
|
-
: builtIn;
|
|
354
|
-
}
|
|
355
|
-
function listedRoom(locator, stored) {
|
|
356
|
-
const declaration = declarationOf(locator, stored);
|
|
357
|
-
if (declaration === undefined)
|
|
358
|
-
return undefined;
|
|
359
|
-
const { room, available } = declaration;
|
|
360
|
-
const selection = room.typedSelection ?? null;
|
|
361
|
-
return {
|
|
362
|
-
name: room.name,
|
|
363
|
-
locator,
|
|
364
|
-
handle: room.handle,
|
|
365
|
-
operations: room.operations,
|
|
366
|
-
replica: room.replica,
|
|
367
|
-
model: modelTypes(modelIn(locator, stored)),
|
|
368
|
-
typedSelection: selection === null ? null : { roots: selection.roots, model: modelTypes(selectionModel()) },
|
|
369
|
-
providerOwnedPaths: room.providerOwnedPaths,
|
|
370
|
-
roomVersion: stored?.record.roomVersion ?? null,
|
|
371
|
-
synchronizationAvailable: available,
|
|
372
|
-
refusal: scopedRefusals.get(locator) ?? null,
|
|
373
|
-
documents: stored?.documents ?? 0,
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
function listingOf(held) {
|
|
377
|
-
const listing = new Map();
|
|
378
|
-
for (const locator of listedLocators(held)) {
|
|
379
|
-
const room = listedRoom(locator, held.get(locator));
|
|
380
|
-
if (room !== undefined)
|
|
381
|
-
listing.set(locator, room);
|
|
382
|
-
}
|
|
383
|
-
return listing;
|
|
384
|
-
}
|
|
385
|
-
async function patchedListing(changed) {
|
|
386
|
-
if (roomsShown === null)
|
|
387
|
-
return null;
|
|
388
|
-
const held = await storedRooms([...changed]);
|
|
389
|
-
const listing = new Map(roomsShown);
|
|
390
|
-
for (const locator of changed) {
|
|
391
|
-
const room = isFenced(locator) ? undefined : listedRoom(locator, held.get(locator));
|
|
392
|
-
if (room === undefined || !listing.has(locator))
|
|
393
|
-
return null;
|
|
394
|
-
listing.set(locator, room);
|
|
395
|
-
}
|
|
396
|
-
return listing;
|
|
397
|
-
}
|
|
398
|
-
async function listingNow() {
|
|
399
|
-
return listingOf(await storedRooms());
|
|
400
|
-
}
|
|
401
|
-
function lockedIn(listing, locator) {
|
|
402
|
-
return listing.get(locator)?.replica.offline === 'locked' && !confirmed.has(locator);
|
|
403
|
-
}
|
|
404
|
-
function shownRooms(listing) {
|
|
405
|
-
return structuredClone([...listing.values()].map(({ documents, ...room }) => lockedIn(listing, room.locator)
|
|
406
|
-
? {
|
|
407
|
-
...room,
|
|
408
|
-
model: modelTypes(NO_MODEL),
|
|
409
|
-
typedSelection: null,
|
|
410
|
-
documents: 0,
|
|
411
|
-
locked: true,
|
|
412
|
-
}
|
|
413
|
-
: { ...room, documents, locked: false }));
|
|
414
|
-
}
|
|
415
|
-
async function offlineModes() {
|
|
416
|
-
const modes = new Map();
|
|
417
|
-
for (const [locator, { room }] of declared) {
|
|
418
|
-
const declaration = room ?? discovered.get(locator);
|
|
419
|
-
if (declaration !== undefined)
|
|
420
|
-
modes.set(locator, declaration.replica.offline);
|
|
421
|
-
}
|
|
422
|
-
for (const [locator, { offline }] of (await covering()).rooms)
|
|
423
|
-
modes.set(locator, offline);
|
|
424
|
-
return modes;
|
|
425
|
-
}
|
|
426
|
-
async function locked(locator) {
|
|
427
|
-
if (isFenced(locator) || confirmed.has(locator))
|
|
428
|
-
return false;
|
|
429
|
-
const held = declared.get(locator)?.room ?? discovered.get(locator);
|
|
430
|
-
const mode = (await (await openLocal()).offline(locator)) ?? held?.replica.offline;
|
|
431
|
-
return mode === 'locked';
|
|
432
|
-
}
|
|
433
|
-
async function lockedRooms() {
|
|
434
|
-
return [...(await offlineModes())]
|
|
435
|
-
.filter(([locator, mode]) => mode === 'locked' && !confirmed.has(locator) && !isFenced(locator))
|
|
436
|
-
.map(([locator]) => locator)
|
|
437
|
-
.sort();
|
|
438
|
-
}
|
|
439
|
-
async function relocked(before) {
|
|
440
|
-
const after = await lockedRooms();
|
|
441
|
-
if (JSON.stringify(after) === JSON.stringify(before))
|
|
442
|
-
return;
|
|
443
|
-
journal.record(`locked ${after.length}`);
|
|
444
|
-
emitRooms();
|
|
445
|
-
for (const notify of [...lockListeners])
|
|
446
|
-
notify();
|
|
447
|
-
}
|
|
448
|
-
function confirmLocks(listed) {
|
|
449
|
-
if (!socket.isOpen())
|
|
450
|
-
return;
|
|
451
|
-
for (const room of listed) {
|
|
452
|
-
if (room.replica.admission === 'admitted')
|
|
453
|
-
confirmed.add(room.locator);
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
function connectionLost() {
|
|
457
|
-
if (lockGrace !== null || closed)
|
|
458
|
-
return;
|
|
459
|
-
lockGrace = setTimeout(() => {
|
|
460
|
-
lockGrace = null;
|
|
461
|
-
if (socket.isOpen() || closed)
|
|
462
|
-
return;
|
|
463
|
-
void lockedRooms().then((before) => {
|
|
464
|
-
confirmed.clear();
|
|
465
|
-
return relocked(before);
|
|
466
|
-
}, () => { });
|
|
467
|
-
}, LOCK_GRACE_MS);
|
|
468
|
-
}
|
|
469
|
-
function connectionLive() {
|
|
470
|
-
if (lockGrace !== null)
|
|
471
|
-
clearTimeout(lockGrace);
|
|
472
|
-
lockGrace = null;
|
|
473
|
-
}
|
|
474
|
-
function watchLocks(notify) {
|
|
475
|
-
lockListeners.add(notify);
|
|
476
|
-
return () => {
|
|
477
|
-
lockListeners.delete(notify);
|
|
478
|
-
};
|
|
479
|
-
}
|
|
480
|
-
async function roomFacts(reach = null) {
|
|
481
|
-
const held = await storedRooms();
|
|
482
|
-
const rooms = [...listingOf(held).values()]
|
|
483
|
-
.filter((room) => holds(reach, room.locator))
|
|
484
|
-
.map((room) => {
|
|
485
|
-
const governed = governs(reach, room);
|
|
486
|
-
const attached = [
|
|
487
|
-
...(governed.collection ? [modelIn(room.locator, held.get(room.locator))] : []),
|
|
488
|
-
...(governed.selection ? [builtIn] : []),
|
|
489
|
-
];
|
|
490
|
-
return {
|
|
491
|
-
locator: room.locator,
|
|
492
|
-
available: attached.every((model) => model.status !== 'unavailable'),
|
|
493
|
-
types: attached.flatMap((model) => model.status === 'unavailable' ? [] : Object.values(model.types)),
|
|
494
|
-
};
|
|
495
|
-
});
|
|
496
|
-
return { listed: roomsListed, rooms };
|
|
497
|
-
}
|
|
498
|
-
function emitRooms(changed) {
|
|
499
|
-
recoverReads();
|
|
500
|
-
queueReadiness();
|
|
501
|
-
return changed === undefined ? publishRooms('all') : publishRooms('changed', changed);
|
|
502
|
-
}
|
|
503
|
-
function emitKnownRooms() {
|
|
504
|
-
conversations.learned();
|
|
505
|
-
if (unlistedRooms.size === 0)
|
|
506
|
-
return;
|
|
507
|
-
void publishRooms('known');
|
|
508
|
-
}
|
|
509
|
-
let roomsOwed = null;
|
|
510
|
-
let roomsShown = null;
|
|
511
|
-
function publishRooms(mode, touched = []) {
|
|
512
|
-
if (closed)
|
|
513
|
-
return Promise.resolve();
|
|
514
|
-
if (roomsOwed !== null) {
|
|
515
|
-
roomsOwed.all ||= mode === 'all';
|
|
516
|
-
for (const locator of touched) {
|
|
517
|
-
(mode === 'changed' ? roomsOwed.changed : roomsOwed.counted).add(locator);
|
|
518
|
-
}
|
|
519
|
-
return roomsOwed.shown;
|
|
520
|
-
}
|
|
521
|
-
const owing = {
|
|
522
|
-
all: mode === 'all',
|
|
523
|
-
changed: new Set(mode === 'changed' ? touched : []),
|
|
524
|
-
counted: new Set(mode === 'changed' ? [] : touched),
|
|
525
|
-
shown: Promise.resolve(),
|
|
526
|
-
};
|
|
527
|
-
roomsOwed = owing;
|
|
528
|
-
owing.shown = publication(async () => {
|
|
529
|
-
const owed = owing;
|
|
530
|
-
if (roomsOwed === owing)
|
|
531
|
-
roomsOwed = null;
|
|
532
|
-
if (closed || (roomListeners.size === 0 && unlistedRooms.size === 0))
|
|
533
|
-
return;
|
|
534
|
-
const patched = owed.all || owed.changed.size === 0 || unlistedRooms.size > 0
|
|
535
|
-
? null
|
|
536
|
-
: await patchedListing(new Set([...owed.changed, ...owed.counted]));
|
|
537
|
-
if (patched === null && owed.changed.size > 0)
|
|
538
|
-
owed.all = true;
|
|
539
|
-
if (patched === null && !owed.all && owed.counted.size > 0) {
|
|
540
|
-
const held = await covering().catch(() => null);
|
|
541
|
-
if (held === null)
|
|
542
|
-
return;
|
|
543
|
-
owed.all = [...owed.counted].some((locator) => held.rooms.has(locator) &&
|
|
544
|
-
roomsShown?.get(locator)?.documents !== held.rooms.get(locator).documents);
|
|
545
|
-
if (owed.all) {
|
|
546
|
-
recoverReads();
|
|
547
|
-
queueReadiness();
|
|
548
|
-
}
|
|
549
|
-
else if (unlistedRooms.size === 0)
|
|
550
|
-
return;
|
|
551
|
-
}
|
|
552
|
-
const listing = patched ?? (await listingNow().catch(() => null));
|
|
553
|
-
if (listing === null || closed)
|
|
554
|
-
return;
|
|
555
|
-
const value = shownRooms(listing);
|
|
556
|
-
const joining = unlistedRooms.size > 0 &&
|
|
557
|
-
(roomsListed || listing.size > 0 || refusal !== null || status.providerUnreachable);
|
|
558
|
-
if (!owed.all && patched === null && !joining)
|
|
559
|
-
return;
|
|
560
|
-
const moved = owed.all ||
|
|
561
|
-
patched !== null ||
|
|
562
|
-
(roomsShown !== null && JSON.stringify(shownRooms(roomsShown)) !== JSON.stringify(value));
|
|
563
|
-
if (moved)
|
|
564
|
-
for (const listener of roomListeners)
|
|
565
|
-
listener(value);
|
|
566
|
-
if (joining) {
|
|
567
|
-
for (const listener of [...unlistedRooms]) {
|
|
568
|
-
unlistedRooms.delete(listener);
|
|
569
|
-
roomListeners.add(listener);
|
|
570
|
-
listener(value);
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
roomsShown = roomListeners.size === 0 ? null : listing;
|
|
574
|
-
});
|
|
575
|
-
return owing.shown;
|
|
576
|
-
}
|
|
577
|
-
function emitRefusal() {
|
|
578
|
-
queueReadiness();
|
|
579
|
-
for (const listener of refusalListeners)
|
|
580
|
-
listener(refusal);
|
|
581
|
-
emitKnownRooms();
|
|
582
|
-
}
|
|
583
|
-
function emitStorageLoss() {
|
|
584
|
-
queueReadiness();
|
|
585
|
-
for (const listener of storageLossListeners)
|
|
586
|
-
listener(storageLoss);
|
|
587
|
-
}
|
|
588
|
-
function emitMember(member) {
|
|
589
|
-
for (const listener of memberListeners)
|
|
590
|
-
listener(member);
|
|
591
|
-
}
|
|
592
|
-
function moveTo(next) {
|
|
593
|
-
if (closed)
|
|
594
|
-
return Promise.resolve();
|
|
595
|
-
status = next;
|
|
596
|
-
return show();
|
|
597
|
-
}
|
|
598
|
-
function transferring() {
|
|
599
|
-
return { ...status, state: 'syncing', resting: false };
|
|
600
|
-
}
|
|
601
|
-
let statusOwed = null;
|
|
602
|
-
let statusShown = Promise.resolve();
|
|
603
|
-
function show() {
|
|
604
|
-
if (closed)
|
|
605
|
-
return Promise.resolve();
|
|
606
|
-
queueReadiness();
|
|
607
|
-
emitKnownRooms();
|
|
608
|
-
const moved = { moving: status, current: conversations.current() };
|
|
609
|
-
if (statusOwed !== null) {
|
|
610
|
-
statusOwed.push(moved);
|
|
611
|
-
return statusShown;
|
|
612
|
-
}
|
|
613
|
-
const owed = [moved];
|
|
614
|
-
statusOwed = owed;
|
|
615
|
-
statusShown = publication(async () => {
|
|
616
|
-
if (statusOwed === owed)
|
|
617
|
-
statusOwed = null;
|
|
618
|
-
const held = await attended();
|
|
619
|
-
if (closed)
|
|
620
|
-
return;
|
|
621
|
-
for (const { moving, current } of owed) {
|
|
622
|
-
const state = moving.resting && held.queued ? 'pending' : moving.state;
|
|
623
|
-
const next = {
|
|
624
|
-
state: state === 'synced' && !current ? 'syncing' : state,
|
|
625
|
-
lastCompleted: held.lastCompleted,
|
|
626
|
-
providerUnreachable: moving.providerUnreachable,
|
|
627
|
-
attentionRequired: held.attentionRequired,
|
|
628
|
-
unsent: held.unsent,
|
|
629
|
-
reset: held.reset,
|
|
630
|
-
};
|
|
631
|
-
if (same(shown, next))
|
|
632
|
-
continue;
|
|
633
|
-
shown = next;
|
|
634
|
-
for (const listener of statusListeners)
|
|
635
|
-
listener(shown);
|
|
636
|
-
}
|
|
637
|
-
});
|
|
638
|
-
return statusShown;
|
|
639
|
-
}
|
|
640
|
-
function entryOf(states, batch) {
|
|
641
|
-
if (localSaves)
|
|
642
|
-
return Object.freeze({ batch, state: 'local' });
|
|
643
|
-
const state = states.get(batch.id);
|
|
644
|
-
return Object.freeze({ batch, ...(state?.submission ?? { state: 'waiting' }) });
|
|
645
|
-
}
|
|
646
|
-
async function entriesOf(locator) {
|
|
647
|
-
if (isFenced(locator))
|
|
648
|
-
return [];
|
|
649
|
-
const { queue, states } = await workOf(locator);
|
|
650
|
-
return queue.map((batch) => entryOf(states, batch));
|
|
651
|
-
}
|
|
652
|
-
function emitQueue(locator) {
|
|
653
|
-
const listening = () => [...queueSubscriptions].filter((subscription) => subscription.locator === locator);
|
|
654
|
-
if (closed || listening().length === 0)
|
|
655
|
-
return Promise.resolve();
|
|
656
|
-
return publication(async () => {
|
|
657
|
-
if (closed || listening().length === 0)
|
|
658
|
-
return;
|
|
659
|
-
const entries = await entriesOf(locator);
|
|
660
|
-
if (closed)
|
|
661
|
-
return;
|
|
662
|
-
for (const subscription of listening())
|
|
663
|
-
subscription.notify(entries);
|
|
664
|
-
});
|
|
665
|
-
}
|
|
666
|
-
function applyChangeSet(set) {
|
|
667
|
-
liveQueries.touched(set);
|
|
668
|
-
figures.touched(set);
|
|
669
|
-
}
|
|
670
|
-
function recoverReads() {
|
|
671
|
-
liveQueries.recover();
|
|
672
|
-
figures.recover();
|
|
673
|
-
}
|
|
674
|
-
function unpublished(error) {
|
|
675
|
-
journal.record(`publication failed ${error instanceof Error ? error.name : 'unknown'}`);
|
|
676
|
-
}
|
|
677
|
-
async function prepareReplica(locator) {
|
|
678
|
-
for (;;) {
|
|
679
|
-
const held = (await storedRooms([locator])).get(locator);
|
|
680
|
-
if (held === undefined || closed || held.indexed)
|
|
681
|
-
return;
|
|
682
|
-
const key = held.record.key;
|
|
683
|
-
const done = await exclusive(async () => {
|
|
684
|
-
const local = await openLocal();
|
|
685
|
-
return store.rederive(local, key, composed(held.record));
|
|
686
|
-
});
|
|
687
|
-
if (!done.done)
|
|
688
|
-
continue;
|
|
689
|
-
if ((await storedRooms([locator])).get(locator)?.record.key !== key)
|
|
690
|
-
continue;
|
|
691
|
-
publishChangeSet(changeSet(locator, { documents: null }));
|
|
692
|
-
recoverReads();
|
|
693
|
-
queueReadiness();
|
|
694
|
-
return;
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
async function unprepared() {
|
|
698
|
-
const held = (await covering()).rooms;
|
|
699
|
-
return [...unpreparedDemand, ...held.keys()].find((locator) => !(held.get(locator)?.indexed ?? false) && !unpreparable.has(locator));
|
|
700
|
-
}
|
|
701
|
-
function unpreparedBy(locator, error) {
|
|
702
|
-
unpreparable.add(locator);
|
|
703
|
-
journal.record(`preparation failed ${locator} ${error instanceof Error ? error.name : 'unknown'}`);
|
|
704
|
-
queueReadiness();
|
|
705
|
-
}
|
|
706
|
-
function prepare(locators = []) {
|
|
707
|
-
for (const locator of locators)
|
|
708
|
-
unpreparedDemand.add(locator);
|
|
709
|
-
if (preparing)
|
|
710
|
-
return;
|
|
711
|
-
preparing = true;
|
|
712
|
-
void (async () => {
|
|
713
|
-
try {
|
|
714
|
-
for (let waiting = await unprepared(); waiting !== undefined; waiting = await unprepared()) {
|
|
715
|
-
await yielded();
|
|
716
|
-
if (closed)
|
|
717
|
-
return;
|
|
718
|
-
unpreparedDemand.delete(waiting);
|
|
719
|
-
await prepareReplica(waiting).catch((error) => unpreparedBy(waiting, error));
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
catch {
|
|
723
|
-
journal.record('preparation failed');
|
|
724
|
-
}
|
|
725
|
-
finally {
|
|
726
|
-
preparing = false;
|
|
727
|
-
}
|
|
728
|
-
})();
|
|
729
|
-
}
|
|
730
|
-
function roomsOfReach(reach) {
|
|
731
|
-
return new Set([...reach.rooms, ...reach.documents.map(roomOfKey)]);
|
|
732
|
-
}
|
|
733
|
-
function everyRoom(held) {
|
|
734
|
-
return [...new Set([...declared.keys(), ...held.rooms.keys(), ...held.queues.keys()])];
|
|
735
|
-
}
|
|
736
|
-
function reachedRooms(plan, held) {
|
|
737
|
-
const reached = plan.reach === null ? null : roomsOfReach(plan.reach);
|
|
738
|
-
const named = plan.rooms ?? (reached === null ? everyRoom(held) : [...reached]);
|
|
739
|
-
return [...new Set(named)]
|
|
740
|
-
.filter((locator) => (reached === null || reached.has(locator)) && !plan.locked.includes(locator))
|
|
741
|
-
.sort();
|
|
742
|
-
}
|
|
743
|
-
function awaited(locator) {
|
|
744
|
-
return (!roomsListed ||
|
|
745
|
-
(declared.has(locator) && discovered.get(locator)?.replica.admission === 'admitted'));
|
|
746
|
-
}
|
|
747
|
-
function covered(plan, held) {
|
|
748
|
-
return coveredRooms(reachedRooms(plan, held), plan.rooms === null && plan.reach === null, held);
|
|
749
|
-
}
|
|
750
|
-
async function figuresCover() {
|
|
751
|
-
const held = await covering();
|
|
752
|
-
const locked = await lockedRooms();
|
|
753
|
-
const cover = coveredRooms(everyRoom(held)
|
|
754
|
-
.filter((locator) => !locked.includes(locator))
|
|
755
|
-
.sort(), true, held);
|
|
756
|
-
awaitPreparation(cover);
|
|
757
|
-
return 'pending' in cover ? cover : { rooms: cover.rooms };
|
|
758
|
-
}
|
|
759
|
-
function coveredRooms(locators, whole, held) {
|
|
760
|
-
if (locators.length === 0 && whole && !roomsListed)
|
|
761
|
-
return { pending: [] };
|
|
762
|
-
const pending = [];
|
|
763
|
-
const coverage = [];
|
|
764
|
-
const overlay = [];
|
|
765
|
-
const read = [];
|
|
766
|
-
for (const locator of locators) {
|
|
767
|
-
const replica = held.rooms.get(locator);
|
|
768
|
-
if (replica === undefined) {
|
|
769
|
-
if (awaited(locator))
|
|
770
|
-
pending.push({ room: locator, awaits: 'acquisition' });
|
|
771
|
-
continue;
|
|
772
|
-
}
|
|
773
|
-
if (!replica.indexed) {
|
|
774
|
-
pending.push({ room: locator, awaits: 'preparation' });
|
|
775
|
-
continue;
|
|
776
|
-
}
|
|
777
|
-
coverage.push({
|
|
778
|
-
room: locator,
|
|
779
|
-
generation: replica.transfer,
|
|
780
|
-
current: !roomInFlight(store, locator),
|
|
781
|
-
});
|
|
782
|
-
read.push({ locator, key: replica.key });
|
|
783
|
-
for (const id of held.queues.get(locator) ?? [])
|
|
784
|
-
overlay.push(id);
|
|
785
|
-
}
|
|
786
|
-
if (pending.length > 0)
|
|
787
|
-
return { pending };
|
|
788
|
-
return { coverage: { rooms: coverage, indexes: [], overlay }, rooms: read };
|
|
789
|
-
}
|
|
790
|
-
function awaitPreparation(cover) {
|
|
791
|
-
if (!('pending' in cover))
|
|
792
|
-
return;
|
|
793
|
-
const waiting = cover.pending.filter(({ awaits }) => awaits === 'preparation');
|
|
794
|
-
if (waiting.length > 0)
|
|
795
|
-
prepare(waiting.map(({ room }) => room));
|
|
796
|
-
}
|
|
797
|
-
function requiredRooms(given, held) {
|
|
798
|
-
const reach = planReach(given);
|
|
799
|
-
const listed = new Set(listedLocators(held));
|
|
800
|
-
if (reach === null)
|
|
801
|
-
return { listed: roomsListed, rooms: [...listed].sort() };
|
|
802
|
-
const named = [...roomsOfReach(reach)].sort();
|
|
803
|
-
if (roomsListed)
|
|
804
|
-
return { listed: true, rooms: named.filter((room) => listed.has(room)) };
|
|
805
|
-
return { listed: named.every((room) => held.has(room)), rooms: named };
|
|
806
|
-
}
|
|
807
|
-
function roomState(locator, held) {
|
|
808
|
-
const listedRoom = discovered.get(locator);
|
|
809
|
-
if (roomsListed &&
|
|
810
|
-
(listedRoom === undefined ||
|
|
811
|
-
listedRoom.replica.admission !== 'admitted' ||
|
|
812
|
-
unreadable.has(locator)))
|
|
813
|
-
return 'unadmitted';
|
|
814
|
-
const stored = held.get(locator);
|
|
815
|
-
if (stored === undefined)
|
|
816
|
-
return 'acquiring';
|
|
817
|
-
if (!stored.indexed)
|
|
818
|
-
return 'preparing';
|
|
819
|
-
return 'complete';
|
|
820
|
-
}
|
|
821
|
-
function conversationCoverage(reach) {
|
|
822
|
-
const declaredConversations = reach === null || reach.conversations;
|
|
823
|
-
if (!declaredConversations || options.adapter.conversations === undefined)
|
|
824
|
-
return 'undeclared';
|
|
825
|
-
if (!conversations.known() || !conversations.current())
|
|
826
|
-
return 'acquiring';
|
|
827
|
-
return 'complete';
|
|
828
|
-
}
|
|
829
|
-
function connectionOf(lastCompleted) {
|
|
830
|
-
if (socket.isOpen())
|
|
831
|
-
return 'connected';
|
|
832
|
-
if (status.providerUnreachable)
|
|
833
|
-
return 'unreachable';
|
|
834
|
-
return options.adapter.roomStream === undefined && lastCompleted !== null
|
|
835
|
-
? 'connected'
|
|
836
|
-
: 'connecting';
|
|
837
|
-
}
|
|
838
|
-
async function connectionNow() {
|
|
839
|
-
if (socket.isOpen() || status.providerUnreachable || options.adapter.roomStream !== undefined) {
|
|
840
|
-
return connectionOf(null);
|
|
841
|
-
}
|
|
842
|
-
return connectionOf((await (await openLocal()).facts()).lastCompleted);
|
|
843
|
-
}
|
|
844
|
-
async function readinessHeld() {
|
|
845
|
-
const local = await openLocal();
|
|
846
|
-
const [held, facts] = await Promise.all([covering(), local.facts()]);
|
|
847
|
-
return { held: held.rooms, facts };
|
|
848
|
-
}
|
|
849
|
-
function readinessFor(port, reach, { held, facts }) {
|
|
850
|
-
const required = requiredRooms(reach, held);
|
|
851
|
-
const coverage = required.rooms.map((room) => ({ room, state: roomState(room, held) }));
|
|
852
|
-
const preparing = coverage
|
|
853
|
-
.filter((room) => room.state === 'preparing' && !unpreparable.has(room.room))
|
|
854
|
-
.map(({ room }) => room);
|
|
855
|
-
if (preparing.length > 0)
|
|
856
|
-
prepare(preparing);
|
|
857
|
-
return readinessOf({
|
|
858
|
-
code: port.code,
|
|
859
|
-
coverage: {
|
|
860
|
-
listed: required.listed,
|
|
861
|
-
rooms: coverage,
|
|
862
|
-
conversations: conversationCoverage(reach),
|
|
77
|
+
writeRule: (declaration) => writeRule(declaration, adapter.pathRule ?? (() => true), adapter.localSaves === true),
|
|
78
|
+
usableWhenHeld: options.usableWhenHeld ?? [],
|
|
79
|
+
build: options.build ?? '',
|
|
80
|
+
conversations: createLiveConversations({
|
|
81
|
+
call: (name, input, attempt) => invokeAction(runtime, name, input, { attempt }),
|
|
82
|
+
history: (request) => readConversationHistory(runtime, request),
|
|
83
|
+
durably: store.durably,
|
|
84
|
+
local: store.open,
|
|
85
|
+
again: () => {
|
|
86
|
+
void socket.ready.then(() => scheduleCurrency(runtime));
|
|
863
87
|
},
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
house: refusal !== null ? 'refused' : writesHeld() ? 'write-halted' : 'open',
|
|
867
|
-
space: full ? 'full' : null,
|
|
868
|
-
installation: installationOf(store, coverage, held),
|
|
869
|
-
lost: storageLoss !== null,
|
|
870
|
-
established: facts.targets[port.target]?.established === true ||
|
|
871
|
-
(heldWhenOpened && usableWhenHeld.includes(port.target)),
|
|
872
|
-
});
|
|
873
|
-
}
|
|
874
|
-
const establishing = new Set();
|
|
875
|
-
function establish(name) {
|
|
876
|
-
if (establishing.has(name))
|
|
877
|
-
return;
|
|
878
|
-
establishing.add(name);
|
|
879
|
-
void durably(async (local) => {
|
|
880
|
-
const { targets } = await local.facts();
|
|
881
|
-
await local.fact('targets', { ...targets, [name]: { established: true } });
|
|
882
|
-
})
|
|
883
|
-
.then(() => {
|
|
884
|
-
queueReadiness();
|
|
885
|
-
void probeEachMinute();
|
|
886
|
-
}, () => {
|
|
887
|
-
journal.record(`target record failed ${name}`);
|
|
888
|
-
})
|
|
889
|
-
.finally(() => establishing.delete(name));
|
|
890
|
-
}
|
|
891
|
-
function deliverReadiness(watch, held) {
|
|
892
|
-
if (!readinessWatches.has(watch))
|
|
893
|
-
return;
|
|
894
|
-
const readiness = readinessFor(watch.port, watch.reach, held);
|
|
895
|
-
if (readiness.ready && held.facts.targets[watch.port.target]?.established !== true) {
|
|
896
|
-
establish(watch.port.target);
|
|
897
|
-
}
|
|
898
|
-
const shownReadiness = JSON.stringify(readiness);
|
|
899
|
-
if (shownReadiness === watch.last)
|
|
900
|
-
return;
|
|
901
|
-
watch.last = shownReadiness;
|
|
902
|
-
watch.deliver(structuredClone(readiness));
|
|
903
|
-
}
|
|
904
|
-
let readinessShown = Promise.resolve();
|
|
905
|
-
const readinessOwed = new Set();
|
|
906
|
-
function deliverAll(watches) {
|
|
907
|
-
const queued = readinessOwed.size > 0;
|
|
908
|
-
for (const watch of watches)
|
|
909
|
-
readinessOwed.add(watch);
|
|
910
|
-
if (queued)
|
|
911
|
-
return;
|
|
912
|
-
readinessShown = readinessShown
|
|
913
|
-
.then(async () => {
|
|
914
|
-
const owed = [...readinessOwed];
|
|
915
|
-
readinessOwed.clear();
|
|
916
|
-
const held = await readinessHeld();
|
|
917
|
-
if (closed)
|
|
918
|
-
return;
|
|
919
|
-
for (const watch of owed)
|
|
920
|
-
deliverReadiness(watch, held);
|
|
921
|
-
})
|
|
922
|
-
.catch(unpublished);
|
|
923
|
-
}
|
|
924
|
-
function queueReadiness() {
|
|
925
|
-
if (readinessQueued || closed || readinessWatches.size === 0)
|
|
926
|
-
return;
|
|
927
|
-
readinessQueued = true;
|
|
928
|
-
queueMicrotask(() => {
|
|
929
|
-
readinessQueued = false;
|
|
930
|
-
if (closed)
|
|
931
|
-
return;
|
|
932
|
-
deliverAll([...readinessWatches]);
|
|
933
|
-
});
|
|
934
|
-
}
|
|
935
|
-
function watchReadiness(port, reach, deliver) {
|
|
936
|
-
const watch = { port, reach, deliver, last: null };
|
|
937
|
-
readinessWatches.add(watch);
|
|
938
|
-
void openLocal().then(() => deliverAll([watch]), () => deliverAll([watch]));
|
|
939
|
-
return () => {
|
|
940
|
-
readinessWatches.delete(watch);
|
|
941
|
-
};
|
|
942
|
-
}
|
|
943
|
-
function providerReached() {
|
|
944
|
-
return (socket.isOpen() || !status.providerUnreachable) && refusal === null;
|
|
945
|
-
}
|
|
946
|
-
function filled() {
|
|
947
|
-
if (full || closed)
|
|
948
|
-
return;
|
|
949
|
-
full = true;
|
|
950
|
-
journal.record('space full');
|
|
951
|
-
queueReadiness();
|
|
952
|
-
void probeEachMinute();
|
|
953
|
-
}
|
|
954
|
-
async function installedTarget() {
|
|
955
|
-
if (heldWhenOpened && usableWhenHeld.length > 0)
|
|
956
|
-
return true;
|
|
957
|
-
const { targets } = await (await openLocal()).facts();
|
|
958
|
-
return Object.values(targets).some((target) => target.established);
|
|
959
|
-
}
|
|
960
|
-
async function probeEachMinute() {
|
|
961
|
-
if (!full || probing !== null || closed)
|
|
962
|
-
return;
|
|
963
|
-
if (!(await installedTarget().catch(() => false)))
|
|
964
|
-
return;
|
|
965
|
-
if (!full || probing !== null || closed)
|
|
966
|
-
return;
|
|
967
|
-
// A device whose storage filled after its install tries a write each minute, so space freed elsewhere ends its read-only mode without a Retry.
|
|
968
|
-
probing = setInterval(() => {
|
|
969
|
-
void probed();
|
|
970
|
-
}, 60_000);
|
|
971
|
-
}
|
|
972
|
-
function probed() {
|
|
973
|
-
testing ??= database.ask({ kind: 'space.probe' }).then(() => {
|
|
974
|
-
testing = null;
|
|
975
|
-
spaceFreed();
|
|
976
|
-
return true;
|
|
977
|
-
}, () => {
|
|
978
|
-
testing = null;
|
|
979
|
-
return false;
|
|
980
|
-
});
|
|
981
|
-
return testing;
|
|
982
|
-
}
|
|
983
|
-
function spaceFreed() {
|
|
984
|
-
if (!full)
|
|
985
|
-
return;
|
|
986
|
-
full = false;
|
|
987
|
-
if (probing !== null)
|
|
988
|
-
clearInterval(probing);
|
|
989
|
-
probing = null;
|
|
990
|
-
unpreparable.clear();
|
|
991
|
-
journal.record('space freed');
|
|
992
|
-
queueReadiness();
|
|
993
|
-
if (closed || authorization === undefined)
|
|
994
|
-
return;
|
|
995
|
-
if (options.adapter.roomStream === undefined)
|
|
996
|
-
void sync('unstreamed', null);
|
|
997
|
-
else
|
|
998
|
-
void resynchronize();
|
|
999
|
-
}
|
|
1000
|
-
function sameCover(left, right) {
|
|
1001
|
-
return JSON.stringify(left) === JSON.stringify(right);
|
|
1002
|
-
}
|
|
1003
|
-
async function coveredRows(plan, cover) {
|
|
1004
|
-
for (;;) {
|
|
1005
|
-
const before = await cover();
|
|
1006
|
-
if ('pending' in before)
|
|
1007
|
-
return { pending: before };
|
|
1008
|
-
const local = await openLocal();
|
|
1009
|
-
const keys = before.rooms.map(({ key }) => key);
|
|
1010
|
-
const changes = local.changes(keys);
|
|
1011
|
-
const current = () => local.changes(keys) === changes;
|
|
1012
|
-
const rows = await windowRows(local, plan, before.rooms, current);
|
|
1013
|
-
const after = await cover();
|
|
1014
|
-
if (rows !== null && sameCover(before, after))
|
|
1015
|
-
return { rows, cover: before, current };
|
|
1016
|
-
}
|
|
1017
|
-
}
|
|
1018
|
-
async function windowRows(local, plan, rooms, unchanged) {
|
|
1019
|
-
const first = await local.query(plan, rooms, QUERY_BYTES);
|
|
1020
|
-
if (first.more === undefined || !('documents' in first)) {
|
|
1021
|
-
const { more: _more, ...rows } = first;
|
|
1022
|
-
return rows;
|
|
1023
|
-
}
|
|
1024
|
-
const documents = [...first.documents];
|
|
1025
|
-
for (let more = first.more; more !== undefined;) {
|
|
1026
|
-
if (!unchanged())
|
|
1027
|
-
return null;
|
|
1028
|
-
const page = await local.query({
|
|
1029
|
-
...plan,
|
|
1030
|
-
after: { text: more, values: JSON.parse(more) },
|
|
1031
|
-
limit: plan.limit === null ? null : plan.limit - documents.length,
|
|
1032
|
-
total: false,
|
|
1033
|
-
}, rooms, QUERY_BYTES);
|
|
1034
|
-
if (!('documents' in page))
|
|
1035
|
-
return null;
|
|
1036
|
-
documents.push(...page.documents);
|
|
1037
|
-
more = page.more;
|
|
1038
|
-
if (more === undefined) {
|
|
1039
|
-
return {
|
|
1040
|
-
documents,
|
|
1041
|
-
...(page.next === undefined ? {} : { next: page.next }),
|
|
1042
|
-
...(first.total === undefined ? {} : { total: first.total }),
|
|
1043
|
-
};
|
|
1044
|
-
}
|
|
1045
|
-
}
|
|
1046
|
-
return null;
|
|
1047
|
-
}
|
|
1048
|
-
function keyedOf(rows, coverage) {
|
|
1049
|
-
if ('groups' in rows) {
|
|
1050
|
-
return {
|
|
1051
|
-
result: { ...rows, coverage },
|
|
1052
|
-
keys: rows.groups.map((group) => JSON.stringify(group.key)),
|
|
1053
|
-
};
|
|
1054
|
-
}
|
|
1055
|
-
return {
|
|
1056
|
-
result: { ...rows, coverage },
|
|
1057
|
-
keys: rows.documents.map((document) => documentKey(document.room, document.path)),
|
|
1058
|
-
};
|
|
1059
|
-
}
|
|
1060
|
-
async function answeredPlan(plan, cover) {
|
|
1061
|
-
const answered = await coveredRows(plan, cover);
|
|
1062
|
-
if ('pending' in answered) {
|
|
1063
|
-
const pending = answered.pending;
|
|
1064
|
-
return { result: pending, keys: [], current: () => true };
|
|
1065
|
-
}
|
|
1066
|
-
const held = answered.cover;
|
|
1067
|
-
return { ...keyedOf(answered.rows, held.coverage), current: answered.current };
|
|
1068
|
-
}
|
|
1069
|
-
async function coverNow(plan) {
|
|
1070
|
-
const cover = covered(plan, await covering());
|
|
1071
|
-
awaitPreparation(cover);
|
|
1072
|
-
return cover;
|
|
1073
|
-
}
|
|
1074
|
-
async function rowsQuery(plan) {
|
|
1075
|
-
const held = await covering();
|
|
1076
|
-
const before = covered(plan, held);
|
|
1077
|
-
awaitPreparation(before);
|
|
1078
|
-
const seeded = seedApplies(seeding, plan, held);
|
|
1079
|
-
if (seeded !== null)
|
|
1080
|
-
return seededResult(seeded).result;
|
|
1081
|
-
if ('pending' in before)
|
|
1082
|
-
return before;
|
|
1083
|
-
const answered = await coveredRows(plan, () => coverNow(plan));
|
|
1084
|
-
if ('pending' in answered)
|
|
1085
|
-
return answered.pending;
|
|
1086
|
-
const shownCover = answered.cover;
|
|
1087
|
-
return { ...answered.rows, coverage: shownCover.coverage };
|
|
1088
|
-
}
|
|
1089
|
-
function publishChangeSet(set) {
|
|
1090
|
-
applyChangeSet(set);
|
|
1091
|
-
for (const { locator, path } of set.documents) {
|
|
1092
|
-
if (path !== null)
|
|
1093
|
-
store.attached.drop(locator, path);
|
|
1094
|
-
}
|
|
1095
|
-
const shownNow = [];
|
|
1096
|
-
if (set.queues.length > 0 || set.drafts.length > 0)
|
|
1097
|
-
shownNow.push(show());
|
|
1098
|
-
if (set.rooms.length > 0)
|
|
1099
|
-
shownNow.push(emitRooms(set.rooms));
|
|
1100
|
-
else if (set.documents.length > 0) {
|
|
1101
|
-
shownNow.push(publishRooms('counted', [...new Set(set.documents.map(({ locator }) => locator))]));
|
|
1102
|
-
}
|
|
1103
|
-
for (const locator of set.queues)
|
|
1104
|
-
shownNow.push(emitQueue(locator));
|
|
1105
|
-
if (set.drafts.length > 0)
|
|
1106
|
-
shownNow.push(publishRoomDrafts(drafting, false));
|
|
1107
|
-
else if (set.documents.length > 0) {
|
|
1108
|
-
shownNow.push(publishRoomDrafts(drafting, false, set.documents));
|
|
1109
|
-
}
|
|
1110
|
-
for (const app of set.presentations)
|
|
1111
|
-
shownNow.push(emitPresentation(drafting, app));
|
|
1112
|
-
conversations.changed(set);
|
|
1113
|
-
return Promise.all(shownNow).then(() => undefined);
|
|
1114
|
-
}
|
|
1115
|
-
async function refuse(reason, provider = null) {
|
|
1116
|
-
discoveryPaused = true;
|
|
1117
|
-
socket.abortReads();
|
|
1118
|
-
try {
|
|
1119
|
-
if (reason === 'revoked-authorization') {
|
|
1120
|
-
await exclusive(async () => {
|
|
1121
|
-
const revoked = { status: 'unavailable', reason: 'revoked' };
|
|
1122
|
-
let held = new Map();
|
|
1123
|
-
try {
|
|
1124
|
-
const local = await openLocal();
|
|
1125
|
-
held = await storedRooms();
|
|
1126
|
-
await local.commit({
|
|
1127
|
-
rooms: [...held.values()].map(({ record: kept }) => {
|
|
1128
|
-
const record = { ...kept, model: revoked };
|
|
1129
|
-
return {
|
|
1130
|
-
locator: record.locator,
|
|
1131
|
-
key: record.key,
|
|
1132
|
-
record: storedRecord(record),
|
|
1133
|
-
composed: composedOf(composed(record)),
|
|
1134
|
-
};
|
|
1135
|
-
}),
|
|
1136
|
-
});
|
|
1137
|
-
}
|
|
1138
|
-
catch {
|
|
1139
|
-
journal.record('model revocation persistence failed');
|
|
1140
|
-
}
|
|
1141
|
-
const listedNow = listedLocators(held);
|
|
1142
|
-
for (const locator of listedNow) {
|
|
1143
|
-
if (!held.has(locator))
|
|
1144
|
-
unheldModels.set(locator, revoked);
|
|
1145
|
-
}
|
|
1146
|
-
publishChangeSet(mergeChangeSets(listedNow.map((locator) => changeSet(locator, { documents: null, room: true }))));
|
|
1147
|
-
prepare();
|
|
1148
|
-
});
|
|
1149
|
-
}
|
|
1150
|
-
}
|
|
1151
|
-
finally {
|
|
1152
|
-
refusal = {
|
|
1153
|
-
code: reason,
|
|
1154
|
-
retryable: false,
|
|
1155
|
-
reauthorizationOffered: reason === 'revoked-authorization',
|
|
1156
|
-
refusal: provider,
|
|
1157
|
-
};
|
|
1158
|
-
journal.record(`connection refused ${reason}`);
|
|
1159
|
-
emitRefusal();
|
|
1160
|
-
}
|
|
1161
|
-
}
|
|
1162
|
-
function list(room, synchronizationAvailable) {
|
|
1163
|
-
if (isFenced(room.locator))
|
|
1164
|
-
return;
|
|
1165
|
-
declared.set(room.locator, {
|
|
1166
|
-
room: {
|
|
1167
|
-
name: room.name,
|
|
1168
|
-
locator: room.locator,
|
|
1169
|
-
handle: room.handle,
|
|
1170
|
-
operations: room.operations,
|
|
1171
|
-
replica: room.replica,
|
|
1172
|
-
typedSelection: room.typedSelection ?? null,
|
|
1173
|
-
providerOwnedPaths: room.providerOwnedPaths,
|
|
88
|
+
settled: () => {
|
|
89
|
+
show(runtime);
|
|
1174
90
|
},
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
const listed = found.filter(({ locator }) => !isFenced(locator) && (!stillFenced.has(locator) || relisted.has(locator)));
|
|
1208
|
-
const listedLocatorsNow = new Set(listed.map((room) => room.locator));
|
|
1209
|
-
for (const locator of listedLocatorsNow)
|
|
1210
|
-
scopedRefusals.delete(locator);
|
|
1211
|
-
if (left === undefined)
|
|
1212
|
-
discovered.clear();
|
|
1213
|
-
for (const room of listed)
|
|
1214
|
-
discovered.set(room.locator, room);
|
|
1215
|
-
const named = new Set(found.map((room) => room.locator));
|
|
1216
|
-
await loseRooms(left ??
|
|
1217
|
-
[...new Set([...declared.keys(), ...kept.held, ...store.installs.transferring()])].filter((locator) => !named.has(locator)));
|
|
1218
|
-
const held = await storedRooms(listed.map((room) => room.locator));
|
|
1219
|
-
for (const room of listed) {
|
|
1220
|
-
const record = held.get(room.locator)?.record;
|
|
1221
|
-
if (record !== undefined && record.replica.selection !== room.replica.selection) {
|
|
1222
|
-
listHeld(room.locator);
|
|
1223
|
-
continue;
|
|
1224
|
-
}
|
|
1225
|
-
list(room, room.replica.admission === 'admitted' && !unreadable.has(room.locator));
|
|
1226
|
-
}
|
|
1227
|
-
confirmLocks(listed);
|
|
1228
|
-
roomsListed = true;
|
|
1229
|
-
emitRooms();
|
|
1230
|
-
await relocked(lockedBefore);
|
|
1231
|
-
return listed;
|
|
1232
|
-
}
|
|
1233
|
-
function forget(locator) {
|
|
1234
|
-
confirmed.delete(locator);
|
|
1235
|
-
forgetInstall(store, locator);
|
|
1236
|
-
declared.delete(locator);
|
|
1237
|
-
discovered.delete(locator);
|
|
1238
|
-
unreadable.delete(locator);
|
|
1239
|
-
stale.delete(locator);
|
|
1240
|
-
repairable.delete(locator);
|
|
1241
|
-
unpreparable.delete(locator);
|
|
1242
|
-
scopedRefusals.delete(locator);
|
|
1243
|
-
unheldModels.delete(locator);
|
|
1244
|
-
store.forget(locator);
|
|
1245
|
-
}
|
|
1246
|
-
async function eraseFenced(locators) {
|
|
1247
|
-
for (const locator of locators) {
|
|
1248
|
-
while (await durably((local) => local.erase(locator, ERASE_BATCH)))
|
|
1249
|
-
await yielded();
|
|
1250
|
-
}
|
|
1251
|
-
}
|
|
1252
|
-
let sweeping = false;
|
|
1253
|
-
function sweep() {
|
|
1254
|
-
if (sweeping || closed)
|
|
1255
|
-
return;
|
|
1256
|
-
sweeping = true;
|
|
1257
|
-
void (async () => {
|
|
1258
|
-
try {
|
|
1259
|
-
while (!closed && (await durably((local) => local.erase(null, ERASE_BATCH)))) {
|
|
1260
|
-
await yielded();
|
|
1261
|
-
}
|
|
1262
|
-
}
|
|
1263
|
-
catch {
|
|
1264
|
-
journal.record('erasure failed');
|
|
1265
|
-
}
|
|
1266
|
-
finally {
|
|
1267
|
-
sweeping = false;
|
|
1268
|
-
}
|
|
1269
|
-
})();
|
|
1270
|
-
}
|
|
1271
|
-
function fenceLive(locators) {
|
|
1272
|
-
for (const locator of new Set(locators)) {
|
|
1273
|
-
if (!store.fences.raise(locator, discoveries))
|
|
1274
|
-
continue;
|
|
1275
|
-
forget(locator);
|
|
1276
|
-
journal.record(`room lost ${locator}`);
|
|
1277
|
-
}
|
|
1278
|
-
}
|
|
1279
|
-
async function loseRooms(locators) {
|
|
1280
|
-
fenceLive(locators);
|
|
1281
|
-
const fresh = [...new Set(locators)].filter((locator) => {
|
|
1282
|
-
const fence = store.fences.of(locator);
|
|
1283
|
-
if (fence === undefined || fence.written)
|
|
1284
|
-
return false;
|
|
1285
|
-
store.fences.mark(locator, true);
|
|
1286
|
-
return true;
|
|
1287
|
-
});
|
|
1288
|
-
if (fresh.length === 0)
|
|
1289
|
-
return;
|
|
1290
|
-
let held = [];
|
|
1291
|
-
try {
|
|
1292
|
-
await durably(async (local) => {
|
|
1293
|
-
held = await local.roomDrafts();
|
|
1294
|
-
await local.fence(fresh);
|
|
1295
|
-
});
|
|
1296
|
-
}
|
|
1297
|
-
catch (error) {
|
|
1298
|
-
for (const locator of fresh)
|
|
1299
|
-
store.fences.mark(locator, false);
|
|
1300
|
-
throw error;
|
|
1301
|
-
}
|
|
1302
|
-
finally {
|
|
1303
|
-
publishChangeSet(mergeChangeSets(fresh.map((locator) => changeSet(locator, {
|
|
1304
|
-
documents: null,
|
|
1305
|
-
room: true,
|
|
1306
|
-
queue: true,
|
|
1307
|
-
drafts: held.filter((draft) => draft.locator === locator).map(({ path }) => path),
|
|
1308
|
-
}))));
|
|
1309
|
-
recoverReads();
|
|
1310
|
-
moveTo(status);
|
|
1311
|
-
}
|
|
1312
|
-
await durably((local) => unpair(accepting, local));
|
|
1313
|
-
await eraseFenced(fresh);
|
|
1314
|
-
}
|
|
1315
|
-
async function open(opened) {
|
|
1316
|
-
if (closed)
|
|
1317
|
-
throw new Error('runtime closed');
|
|
1318
|
-
const interrupted = await opened.fences();
|
|
1319
|
-
const kept = options.shell.generation;
|
|
1320
|
-
const storedGeneration = await opened.generation();
|
|
1321
|
-
const loss = detectStorageLoss(storedGeneration, kept);
|
|
1322
|
-
const generation = nextGeneration(storedGeneration, kept);
|
|
1323
|
-
if (generation !== storedGeneration)
|
|
1324
|
-
await opened.rememberGeneration(generation);
|
|
1325
|
-
if (generation !== kept)
|
|
1326
|
-
await options.shell.keepGeneration(generation);
|
|
1327
|
-
if (loss !== null) {
|
|
1328
|
-
storageLoss = loss;
|
|
1329
|
-
emitStorageLoss();
|
|
1330
|
-
}
|
|
1331
|
-
const held = await opened.opened();
|
|
1332
|
-
heldWhenOpened = usableWhenHeld.length > 0 && (await opened.covering()).rooms.size > 0;
|
|
1333
|
-
try {
|
|
1334
|
-
await opened.demote();
|
|
1335
|
-
await database.ask({ kind: 'read.builds', build });
|
|
1336
|
-
for (const key of held.orphans)
|
|
1337
|
-
await opened.release(key);
|
|
1338
|
-
}
|
|
1339
|
-
catch (error) {
|
|
1340
|
-
if (!refusedForSpace(error))
|
|
1341
|
-
throw error;
|
|
1342
|
-
}
|
|
1343
|
-
if (held.erasing || held.orphans.length > 0)
|
|
1344
|
-
queueMicrotask(sweep);
|
|
1345
|
-
await conversations.opened(opened);
|
|
1346
|
-
if (interrupted.length > 0) {
|
|
1347
|
-
queueMicrotask(() => {
|
|
1348
|
-
void eraseFenced(interrupted).catch(() => {
|
|
1349
|
-
journal.record('erasure failed');
|
|
1350
|
-
});
|
|
1351
|
-
});
|
|
1352
|
-
}
|
|
1353
|
-
moveTo(status);
|
|
1354
|
-
prepare();
|
|
1355
|
-
emitRooms();
|
|
1356
|
-
void exclusive(() => unpair(accepting, opened)).catch(() => {
|
|
1357
|
-
journal.record('unpair failed');
|
|
1358
|
-
});
|
|
1359
|
-
return opened;
|
|
1360
|
-
}
|
|
1361
|
-
async function readHistory(locator, path, query = {}) {
|
|
1362
|
-
if (closed || refusal !== null)
|
|
1363
|
-
return UNCERTAIN;
|
|
1364
|
-
await firstDiscovery;
|
|
1365
|
-
if (!roomsNamed)
|
|
1366
|
-
return UNCERTAIN;
|
|
1367
|
-
try {
|
|
1368
|
-
await ensureAuthorization();
|
|
1369
|
-
}
|
|
1370
|
-
catch {
|
|
1371
|
-
return UNCERTAIN;
|
|
1372
|
-
}
|
|
1373
|
-
if (closed || authorization === undefined || refusal !== null)
|
|
1374
|
-
return UNCERTAIN;
|
|
1375
|
-
const since = store.fences.count();
|
|
1376
|
-
const transfer = ['transfer', 'history', locator, path];
|
|
1377
|
-
if (query.revision !== undefined)
|
|
1378
|
-
transfer.push(query.revision);
|
|
1379
|
-
if (query.pageToken !== undefined)
|
|
1380
|
-
transfer.push(query.pageToken);
|
|
1381
|
-
journal.record(transfer.join(' '));
|
|
1382
|
-
try {
|
|
1383
|
-
const result = await options.adapter.history(authorization, {
|
|
1384
|
-
locator,
|
|
1385
|
-
path,
|
|
1386
|
-
...(query.revision === undefined ? {} : { revision: query.revision }),
|
|
1387
|
-
...(query.pageToken === undefined ? {} : { pageToken: query.pageToken }),
|
|
1388
|
-
});
|
|
1389
|
-
journal.record(`outcome history ${result.kind}`);
|
|
1390
|
-
if (store.fences.since(locator, since))
|
|
1391
|
-
return failure({ kind: 'room-not-readable', locator });
|
|
1392
|
-
if (result.kind === 'scoped-refusal') {
|
|
1393
|
-
const room = await recordAt(result.locator);
|
|
1394
|
-
if (room !== undefined)
|
|
1395
|
-
await scoped(result, room, await openLocal());
|
|
1396
|
-
}
|
|
1397
|
-
if (result.kind === 'revoked-authorization')
|
|
1398
|
-
await refuse('revoked-authorization');
|
|
1399
|
-
return historyAnswer(result);
|
|
1400
|
-
}
|
|
1401
|
-
catch {
|
|
1402
|
-
journal.record('outcome history transport-uncertainty');
|
|
1403
|
-
return UNCERTAIN;
|
|
1404
|
-
}
|
|
1405
|
-
}
|
|
1406
|
-
async function located(room, path, destination) {
|
|
1407
|
-
const crossing = roomTarget(destination);
|
|
1408
|
-
const listing = await listingNow();
|
|
1409
|
-
if (crossing === null) {
|
|
1410
|
-
const named = target(path, destination);
|
|
1411
|
-
return named === null || !listing.has(room) ? null : { room, path: named };
|
|
1412
|
-
}
|
|
1413
|
-
const held = [...listing.values()].find((listed) => listed.handle === crossing.handle);
|
|
1414
|
-
return held === undefined ? null : { room: held.locator, path: crossing.path };
|
|
1415
|
-
}
|
|
1416
|
-
async function resolveReference(room, path, destination) {
|
|
1417
|
-
if (roomTarget(destination) === null && target(path, destination) === null) {
|
|
1418
|
-
return ABSENT_TARGET;
|
|
1419
|
-
}
|
|
1420
|
-
const at = await located(room, path, destination);
|
|
1421
|
-
if (at === null)
|
|
1422
|
-
return UNREADABLE;
|
|
1423
|
-
if (await locked(at.room))
|
|
1424
|
-
return LOCKED_UNREADABLE;
|
|
1425
|
-
const held = await recordAt(at.room);
|
|
1426
|
-
if (held !== undefined &&
|
|
1427
|
-
(await (await openLocal()).entries(held.key, 'visible', [at.path])).length > 0) {
|
|
1428
|
-
return { kind: 'document', ...at };
|
|
1429
|
-
}
|
|
1430
|
-
const history = await readHistory(at.room, at.path);
|
|
1431
|
-
if (history.kind === 'no-history' || history.kind === 'revision')
|
|
1432
|
-
return ABSENT_TARGET;
|
|
1433
|
-
if (history.kind !== 'history')
|
|
1434
|
-
return history;
|
|
1435
|
-
const [last] = history.revisions;
|
|
1436
|
-
if (last === undefined)
|
|
1437
|
-
return ABSENT_TARGET;
|
|
1438
|
-
return history.deleted === true
|
|
1439
|
-
? { kind: 'deleted', ...at, revision: last }
|
|
1440
|
-
: { kind: 'document', ...at };
|
|
1441
|
-
}
|
|
1442
|
-
async function cleanUp() {
|
|
1443
|
-
const freed = await database.ask({ kind: 'opened.clean' });
|
|
1444
|
-
return freed + ((await options.shell.cleanUp?.()) ?? 0);
|
|
1445
|
-
}
|
|
1446
|
-
async function declaredAction(name) {
|
|
1447
|
-
for (const capability of (await (await openLocal()).facts()).capabilities) {
|
|
1448
|
-
for (const action of capability.actions ?? []) {
|
|
1449
|
-
if (action.name === name)
|
|
1450
|
-
return action;
|
|
1451
|
-
}
|
|
1452
|
-
}
|
|
1453
|
-
return undefined;
|
|
1454
|
-
}
|
|
1455
|
-
async function calledRooms(name, input) {
|
|
1456
|
-
const member = (await declaredAction(name))?.room;
|
|
1457
|
-
if (member === undefined || input === null || typeof input !== 'object')
|
|
1458
|
-
return [];
|
|
1459
|
-
const named = input[member];
|
|
1460
|
-
return (Array.isArray(named) ? named : [named]).filter((room) => typeof room === 'string');
|
|
1461
|
-
}
|
|
1462
|
-
async function invokeAction(name, input, dispatching) {
|
|
1463
|
-
await ready;
|
|
1464
|
-
if (closed || refusal !== null)
|
|
1465
|
-
return UNCERTAIN;
|
|
1466
|
-
if ((await declaredAction(name)) === undefined)
|
|
1467
|
-
return undeclared(name);
|
|
1468
|
-
try {
|
|
1469
|
-
await ensureAuthorization();
|
|
1470
|
-
}
|
|
1471
|
-
catch {
|
|
1472
|
-
return UNCERTAIN;
|
|
1473
|
-
}
|
|
1474
|
-
if (closed || authorization === undefined || refusal !== null)
|
|
1475
|
-
return UNCERTAIN;
|
|
1476
|
-
await dispatching?.();
|
|
1477
|
-
journal.record(`transfer action ${name}`);
|
|
1478
|
-
try {
|
|
1479
|
-
const result = await options.adapter.action(authorization, { name, input });
|
|
1480
|
-
if (result.kind === 'scoped-refusal') {
|
|
1481
|
-
const room = await recordAt(result.locator);
|
|
1482
|
-
if (room !== undefined)
|
|
1483
|
-
await scoped(result, room, await openLocal());
|
|
1484
|
-
}
|
|
1485
|
-
if (result.kind === 'revoked-authorization')
|
|
1486
|
-
await refuse('revoked-authorization');
|
|
1487
|
-
journal.record(`outcome action ${result.kind}`);
|
|
1488
|
-
if (result.kind === 'rejected')
|
|
1489
|
-
journal.record(`refusal rejected ${result.code}`);
|
|
1490
|
-
return calledResult(result);
|
|
1491
|
-
}
|
|
1492
|
-
catch {
|
|
1493
|
-
journal.record('outcome action transport-uncertainty');
|
|
1494
|
-
return UNCERTAIN;
|
|
1495
|
-
}
|
|
1496
|
-
}
|
|
1497
|
-
async function readConversationHistory(request) {
|
|
1498
|
-
await ready;
|
|
1499
|
-
const stream = options.adapter.conversations;
|
|
1500
|
-
if (closed || refusal !== null || stream?.history === undefined)
|
|
1501
|
-
return UNCERTAIN;
|
|
1502
|
-
journal.record('transfer conversation history');
|
|
1503
|
-
try {
|
|
1504
|
-
const result = await stream.history(request);
|
|
1505
|
-
if (result.kind === 'revoked-authorization')
|
|
1506
|
-
await refuse('revoked-authorization');
|
|
1507
|
-
journal.record(`outcome conversation history ${result.kind}`);
|
|
1508
|
-
return calledResult(result);
|
|
1509
|
-
}
|
|
1510
|
-
catch {
|
|
1511
|
-
journal.record('outcome conversation history transport-uncertainty');
|
|
1512
|
-
return UNCERTAIN;
|
|
1513
|
-
}
|
|
1514
|
-
}
|
|
1515
|
-
function unsettled(result) {
|
|
1516
|
-
return result.kind !== 'action' && result.retryable;
|
|
1517
|
-
}
|
|
1518
|
-
async function recoveredAction(name, input) {
|
|
1519
|
-
const operation = operationOf(input);
|
|
1520
|
-
if (operation === null)
|
|
1521
|
-
return invokeAction(name, input);
|
|
1522
|
-
const kept = async () => (await openLocal()).keepCall({ operation, name, input });
|
|
1523
|
-
calling.add(operation);
|
|
1524
|
-
try {
|
|
1525
|
-
let result = await invokeAction(name, input, kept);
|
|
1526
|
-
if (unsettled(result)) {
|
|
1527
|
-
await recovery.attempt(`action ${operation}`, async () => {
|
|
1528
|
-
if (closed || refusal !== null)
|
|
1529
|
-
return 'done';
|
|
1530
|
-
if (!socket.isOpen())
|
|
1531
|
-
return 'again';
|
|
1532
|
-
result = await invokeAction(name, input, kept);
|
|
1533
|
-
return unsettled(result) ? 'again' : 'done';
|
|
1534
|
-
}, { again: true });
|
|
1535
|
-
}
|
|
1536
|
-
if (!unsettled(result))
|
|
1537
|
-
await (await openLocal()).dropCall(operation);
|
|
1538
|
-
return result;
|
|
1539
|
-
}
|
|
1540
|
-
finally {
|
|
1541
|
-
calling.delete(operation);
|
|
1542
|
-
}
|
|
1543
|
-
}
|
|
1544
|
-
async function resendCalls(signal) {
|
|
1545
|
-
if (!socket.isOpen() || closed || stopping)
|
|
1546
|
-
return 'done';
|
|
1547
|
-
const local = await openLocal();
|
|
1548
|
-
for (const held of await local.calls()) {
|
|
1549
|
-
if (calling.has(held.operation))
|
|
1550
|
-
continue;
|
|
1551
|
-
if (closed || signal.aborted)
|
|
1552
|
-
return 'done';
|
|
1553
|
-
calling.add(held.operation);
|
|
1554
|
-
try {
|
|
1555
|
-
if (unsettled(await invokeAction(held.name, held.input)))
|
|
1556
|
-
return 'again';
|
|
1557
|
-
await local.dropCall(held.operation);
|
|
1558
|
-
}
|
|
1559
|
-
finally {
|
|
1560
|
-
calling.delete(held.operation);
|
|
1561
|
-
}
|
|
1562
|
-
}
|
|
1563
|
-
return 'done';
|
|
1564
|
-
}
|
|
1565
|
-
function continuing() {
|
|
1566
|
-
return !closed && !discoveryPaused;
|
|
1567
|
-
}
|
|
1568
|
-
async function advance(room, local, authorized, running, unheld) {
|
|
1569
|
-
const live = () => running() && !full;
|
|
1570
|
-
if (!live())
|
|
1571
|
-
return unconfirmed;
|
|
1572
|
-
const held = unheld === undefined ? await recordAt(room.locator) : undefined;
|
|
1573
|
-
if (options.adapter.roomStream !== undefined &&
|
|
1574
|
-
(held !== undefined || !store.installs.of(room.locator).beyondHorizon)) {
|
|
1575
|
-
stale.delete(room.locator);
|
|
1576
|
-
await name(room.locator);
|
|
1577
|
-
return null;
|
|
1578
|
-
}
|
|
1579
|
-
const refresh = reads.refresh;
|
|
1580
|
-
if (held !== undefined && refresh !== undefined) {
|
|
1581
|
-
const refreshed = await store.ahead((hold) => refreshedRoom(room, local, authorized, held, live, hold, refresh));
|
|
1582
|
-
if (refreshed === 'behind')
|
|
1583
|
-
return advance(room, local, authorized, live);
|
|
1584
|
-
if (refreshed !== 'beyond-horizon')
|
|
1585
|
-
return refreshed;
|
|
1586
|
-
if (reloadsDue.has(room.locator))
|
|
1587
|
-
return unconfirmed;
|
|
1588
|
-
replaced(store, [room.locator]);
|
|
1589
|
-
}
|
|
1590
|
-
const prior = held === undefined ? undefined : await recordAt(room.locator);
|
|
1591
|
-
const from = standingAt(store, room.locator, prior);
|
|
1592
|
-
const bootstrapped = await pullBootstrap(installer, authorized, room, local, live, prior, reads, unheld);
|
|
1593
|
-
if (bootstrapped.kind !== 'staged')
|
|
1594
|
-
return live() ? bootstrapped : unconfirmed;
|
|
1595
|
-
const landing = { kind: 'bootstrapped', refreshed: bootstrapped.refreshed, from };
|
|
1596
|
-
if (await publish(installer, local, room, bootstrapped.record, landing, live)) {
|
|
1597
|
-
return live() ? null : unconfirmed;
|
|
1598
|
-
}
|
|
1599
|
-
return (await reachedBy(installer, room.locator, bootstrapped.record.roomVersion))
|
|
1600
|
-
? null
|
|
1601
|
-
: advance(room, local, authorized, live);
|
|
1602
|
-
}
|
|
1603
|
-
async function refreshedRoom(room, local, authorized, held, live, hold, refresh) {
|
|
1604
|
-
const refreshed = await pullRefresh(installer, authorized, held, live, hold, refresh);
|
|
1605
|
-
if (!live())
|
|
1606
|
-
return unconfirmed;
|
|
1607
|
-
if (refreshed.kind === 'still-current') {
|
|
1608
|
-
stale.delete(room.locator);
|
|
1609
|
-
if (unreadable.delete(room.locator)) {
|
|
1610
|
-
scopedRefusals.delete(room.locator);
|
|
1611
|
-
list(room, true);
|
|
1612
|
-
emitRooms();
|
|
1613
|
-
}
|
|
1614
|
-
return null;
|
|
1615
|
-
}
|
|
1616
|
-
if (refreshed.kind === 'advanced') {
|
|
1617
|
-
const landing = {
|
|
1618
|
-
kind: 'refreshed',
|
|
1619
|
-
from: refreshed.from,
|
|
1620
|
-
remodelled: refreshed.remodelled,
|
|
1621
|
-
};
|
|
1622
|
-
if (!(await publish(installer, local, room, refreshed.record, landing, live))) {
|
|
1623
|
-
const now = (await recordAt(room.locator))?.roomVersion;
|
|
1624
|
-
const reached = now !== undefined &&
|
|
1625
|
-
options.adapter.reached(room.locator, now, refreshed.record.roomVersion);
|
|
1626
|
-
return reached ? null : 'behind';
|
|
1627
|
-
}
|
|
1628
|
-
await retainModel(local, room, null);
|
|
1629
|
-
return null;
|
|
1630
|
-
}
|
|
1631
|
-
return refreshed.kind === 'beyond-horizon' ? 'beyond-horizon' : refreshed;
|
|
1632
|
-
}
|
|
1633
|
-
async function withhold(withheld, room, local, progress) {
|
|
1634
|
-
if (withheld.kind === 'unconfirmed' || isFenced(room.locator))
|
|
1635
|
-
return;
|
|
1636
|
-
if (withheld.kind === 'not-ready') {
|
|
1637
|
-
progress.complete = false;
|
|
1638
|
-
askAgain(room.locator);
|
|
1639
|
-
return;
|
|
1640
|
-
}
|
|
1641
|
-
stale.add(room.locator);
|
|
1642
|
-
if (withheld.kind === 'transport-uncertainty' || withheld.kind === 'retryable-refusal') {
|
|
1643
|
-
const reset = withheld.kind === 'retryable-refusal' ? withheld.reset : undefined;
|
|
1644
|
-
repairable.set(room.locator, reset);
|
|
1645
|
-
repairLater(room.locator, reset === undefined ? { again: true } : { at: Date.parse(reset) });
|
|
1646
|
-
}
|
|
1647
|
-
else
|
|
1648
|
-
repairable.delete(room.locator);
|
|
1649
|
-
if (withheld.kind === 'scoped-refusal') {
|
|
1650
|
-
await scoped(withheld, room, local);
|
|
1651
|
-
}
|
|
1652
|
-
else if (withheld.kind === 'transport-uncertainty') {
|
|
1653
|
-
progress.reachable = false;
|
|
1654
|
-
}
|
|
1655
|
-
else if (withheld.kind === 'retryable-refusal') {
|
|
1656
|
-
journal.record(`refusal ${withheld.kind} ${withheld.code}`);
|
|
1657
|
-
}
|
|
1658
|
-
else if (withheld.kind === 'room-not-readable') {
|
|
1659
|
-
roomUnreadable(room);
|
|
1660
|
-
}
|
|
1661
|
-
else if (withheld.kind === 'revoked-authorization') {
|
|
1662
|
-
await refuse('revoked-authorization');
|
|
1663
|
-
}
|
|
1664
|
-
}
|
|
1665
|
-
function roomUnreadable(room) {
|
|
1666
|
-
unreadable.add(room.locator);
|
|
1667
|
-
list(room, false);
|
|
1668
|
-
emitRooms();
|
|
1669
|
-
}
|
|
1670
|
-
function askAgain(locator) {
|
|
1671
|
-
const held = unready.get(locator) ?? { tries: 0, timer: null };
|
|
1672
|
-
unready.set(locator, held);
|
|
1673
|
-
if (held.timer !== null)
|
|
1674
|
-
return;
|
|
1675
|
-
const wait = NOT_READY_MS[Math.min(held.tries, NOT_READY_MS.length - 1)];
|
|
1676
|
-
held.tries += 1;
|
|
1677
|
-
held.timer = setTimeout(() => {
|
|
1678
|
-
held.timer = null;
|
|
1679
|
-
const room = discovered.get(locator);
|
|
1680
|
-
const unstreamed = options.adapter.roomStream === undefined;
|
|
1681
|
-
if (closed || stopping || (!unstreamed && !socket.isOpen()) || room === undefined)
|
|
1682
|
-
return;
|
|
1683
|
-
const progress = { complete: true, reachable: true };
|
|
1684
|
-
void openLocal()
|
|
1685
|
-
.then((local) => roomTask(locator, (live) => loadRoom(room, local, progress, live)))
|
|
1686
|
-
.then(() => rest(progress), () => undefined);
|
|
1687
|
-
}, wait);
|
|
1688
|
-
}
|
|
1689
|
-
function readied(locator) {
|
|
1690
|
-
const held = unready.get(locator);
|
|
1691
|
-
if (held === undefined)
|
|
1692
|
-
return;
|
|
1693
|
-
if (held.timer !== null)
|
|
1694
|
-
clearTimeout(held.timer);
|
|
1695
|
-
unready.delete(locator);
|
|
1696
|
-
}
|
|
1697
|
-
function unreadied() {
|
|
1698
|
-
for (const locator of [...unready.keys()])
|
|
1699
|
-
readied(locator);
|
|
1700
|
-
}
|
|
1701
|
-
function declaredAsHeld(held, room) {
|
|
1702
|
-
return (held === undefined ||
|
|
1703
|
-
(JSON.stringify(held.operations) === JSON.stringify(room.operations) &&
|
|
1704
|
-
JSON.stringify(held.replica) === JSON.stringify(room.replica) &&
|
|
1705
|
-
JSON.stringify(held.providerOwnedPaths) === JSON.stringify(room.providerOwnedPaths) &&
|
|
1706
|
-
JSON.stringify(held.typedSelection) === JSON.stringify(room.typedSelection ?? null)));
|
|
1707
|
-
}
|
|
1708
|
-
async function keepNaming(local, room) {
|
|
1709
|
-
const held = await recordAt(room.locator);
|
|
1710
|
-
if (held === undefined || (held.name === room.name && held.handle === room.handle))
|
|
1711
|
-
return;
|
|
1712
|
-
await exclusive(async () => {
|
|
1713
|
-
if (!continuing())
|
|
1714
|
-
return;
|
|
1715
|
-
const current = await recordAt(room.locator);
|
|
1716
|
-
if (current === undefined)
|
|
1717
|
-
return;
|
|
1718
|
-
const record = { ...current, name: room.name, handle: room.handle };
|
|
1719
|
-
await local.commit({
|
|
1720
|
-
rooms: [
|
|
1721
|
-
{
|
|
1722
|
-
locator: room.locator,
|
|
1723
|
-
key: record.key,
|
|
1724
|
-
record: storedRecord(record),
|
|
1725
|
-
composed: composedOf(composed(record)),
|
|
1726
|
-
},
|
|
1727
|
-
],
|
|
1728
|
-
});
|
|
1729
|
-
emitRooms();
|
|
1730
|
-
});
|
|
1731
|
-
}
|
|
1732
|
-
async function modelNow(locator) {
|
|
1733
|
-
return (await recordAt(locator))?.model ?? unheldModels.get(locator) ?? NO_MODEL;
|
|
1734
|
-
}
|
|
1735
|
-
async function retainModel(local, room, given) {
|
|
1736
|
-
const held = await recordAt(room.locator);
|
|
1737
|
-
const previous = held?.model ?? unheldModels.get(room.locator) ?? NO_MODEL;
|
|
1738
|
-
const next = given ?? previous;
|
|
1739
|
-
if (JSON.stringify(previous) === JSON.stringify(next) && declaredAsHeld(held, room))
|
|
1740
|
-
return;
|
|
1741
|
-
await exclusive(async () => {
|
|
1742
|
-
if (!continuing())
|
|
1743
|
-
return;
|
|
1744
|
-
const work = await workOf(room.locator);
|
|
1745
|
-
const current = work.replica;
|
|
1746
|
-
let changed = null;
|
|
1747
|
-
if (current !== undefined) {
|
|
1748
|
-
const record = {
|
|
1749
|
-
...current.record,
|
|
1750
|
-
name: room.name,
|
|
1751
|
-
handle: room.handle,
|
|
1752
|
-
operations: room.operations,
|
|
1753
|
-
replica: room.replica,
|
|
1754
|
-
providerOwnedPaths: room.providerOwnedPaths,
|
|
1755
|
-
typedSelection: room.typedSelection ?? null,
|
|
1756
|
-
model: next,
|
|
1757
|
-
};
|
|
1758
|
-
const confirmedLayer = await sliceAt(store, record, 'confirmed', current.queue, current.overlaid);
|
|
1759
|
-
const replayedLayer = await replayed(store, record, confirmedLayer, current.queue);
|
|
1760
|
-
const diff = await overlayDiff(record, current, replayedLayer.visible, replayedLayer.touched);
|
|
1761
|
-
const answer = await local.commit({
|
|
1762
|
-
rooms: [
|
|
1763
|
-
{
|
|
1764
|
-
locator: room.locator,
|
|
1765
|
-
key: record.key,
|
|
1766
|
-
record: storedRecord(record),
|
|
1767
|
-
roots: record.typedSelection?.roots ?? [],
|
|
1768
|
-
composed: composedOf(composed(record)),
|
|
1769
|
-
overlay: diff.overlay,
|
|
1770
|
-
},
|
|
1771
|
-
],
|
|
1772
|
-
});
|
|
1773
|
-
preparedAfter(saver, room.locator, answer);
|
|
1774
|
-
changed = changeSet(room.locator, {
|
|
1775
|
-
documents: answer.changed[room.locator] ?? [],
|
|
1776
|
-
room: true,
|
|
1777
|
-
});
|
|
1778
|
-
}
|
|
1779
|
-
else
|
|
1780
|
-
unheldModels.set(room.locator, next);
|
|
1781
|
-
list(room, room.replica.admission === 'admitted' && !unreadable.has(room.locator));
|
|
1782
|
-
if (changed === null)
|
|
1783
|
-
emitRooms();
|
|
1784
|
-
else
|
|
1785
|
-
publishChangeSet(changed);
|
|
1786
|
-
});
|
|
1787
|
-
}
|
|
1788
|
-
async function scoped(result, room, local) {
|
|
1789
|
-
if (result.scope === 'content' && !result.retryable) {
|
|
1790
|
-
journal.record(`scoped refusal ${room.locator} ${result.scope} ${result.code}`);
|
|
1791
|
-
await loseRooms([room.locator]);
|
|
1792
|
-
return;
|
|
1793
|
-
}
|
|
1794
|
-
scopedRefusals.set(room.locator, {
|
|
1795
|
-
code: result.code,
|
|
1796
|
-
fields: result.fields,
|
|
1797
|
-
retryable: result.retryable,
|
|
1798
|
-
});
|
|
1799
|
-
let scopedAway = room;
|
|
1800
|
-
if (result.scope === 'model')
|
|
1801
|
-
await retainModel(local, room, {
|
|
1802
|
-
status: 'unavailable',
|
|
1803
|
-
reason: 'not-readable',
|
|
1804
|
-
});
|
|
1805
|
-
else if (result.scope !== 'operation') {
|
|
1806
|
-
unreadable.add(room.locator);
|
|
1807
|
-
scopedAway = { ...room, replica: { ...room.replica, admission: 'unavailable' } };
|
|
1808
|
-
await retainModel(local, scopedAway, null);
|
|
1809
|
-
}
|
|
1810
|
-
if (!continuing())
|
|
1811
|
-
return;
|
|
1812
|
-
list(scopedAway, scopedAway.replica.admission === 'admitted' && !unreadable.has(room.locator));
|
|
1813
|
-
emitRooms();
|
|
1814
|
-
journal.record(`scoped refusal ${room.locator} ${result.scope} ${result.code}`);
|
|
1815
|
-
}
|
|
1816
|
-
async function name(locator) {
|
|
1817
|
-
const room = discovered.get(locator);
|
|
1818
|
-
if (!socket.streamed || room === undefined || !socket.isOpen() || !socket.isHanded())
|
|
1819
|
-
return;
|
|
1820
|
-
const [held] = await heldRooms([room]);
|
|
1821
|
-
if (held === undefined || !socket.isOpen() || !socket.isHanded())
|
|
1822
|
-
return;
|
|
1823
|
-
socket.name(held);
|
|
1824
|
-
}
|
|
1825
|
-
async function heldRooms(listed) {
|
|
1826
|
-
const stored = await storedRooms(listed.map((room) => room.locator));
|
|
1827
|
-
return listed.flatMap((room) => {
|
|
1828
|
-
const record = stored.get(room.locator)?.record;
|
|
1829
|
-
if (record === undefined) {
|
|
1830
|
-
return room.replica.admission === 'admitted'
|
|
1831
|
-
? [{ locator: room.locator, roomVersion: null }]
|
|
1832
|
-
: [];
|
|
1833
|
-
}
|
|
1834
|
-
if (record.replica.selection !== room.replica.selection)
|
|
1835
|
-
return [];
|
|
1836
|
-
return [{ locator: room.locator, roomVersion: record.roomVersion }];
|
|
1837
|
-
});
|
|
1838
|
-
}
|
|
1839
|
-
async function synchronize(listed, local, progress, attempt, live) {
|
|
1840
|
-
const heldNow = await storedRooms(listed.map(({ locator }) => locator));
|
|
1841
|
-
const prepared = await Promise.allSettled(listed.map((room) => prepareRoom(room, local, progress, heldNow.get(room.locator)?.record)));
|
|
1842
|
-
if (!live())
|
|
1843
|
-
return;
|
|
1844
|
-
const ready = listed.filter((_, index) => {
|
|
1845
|
-
const outcome = prepared[index];
|
|
1846
|
-
return outcome?.status === 'fulfilled' && outcome.value;
|
|
1847
|
-
});
|
|
1848
|
-
const held = await heldRooms(ready);
|
|
1849
|
-
if (!live())
|
|
1850
|
-
return;
|
|
1851
|
-
if (attempt === 'opened') {
|
|
1852
|
-
socket.hand(held);
|
|
1853
|
-
}
|
|
1854
|
-
const kept = attempt === 'unstreamed'
|
|
1855
|
-
? new Set()
|
|
1856
|
-
: new Set((await storedRooms(ready.map(({ locator }) => locator))).keys());
|
|
1857
|
-
for (const room of ready) {
|
|
1858
|
-
if (attempt === 'opened' || kept.has(room.locator)) {
|
|
1859
|
-
if (attempt === 'authorized')
|
|
1860
|
-
await name(room.locator);
|
|
1861
|
-
continue;
|
|
1862
|
-
}
|
|
1863
|
-
void roomTask(room.locator, (live) => loadRoom(room, local, progress, live)).then(() => rest(progress));
|
|
1864
|
-
}
|
|
1865
|
-
const failed = prepared.find((room) => room.status === 'rejected');
|
|
1866
|
-
if (failed !== undefined)
|
|
1867
|
-
throw failed.reason;
|
|
1868
|
-
}
|
|
1869
|
-
async function prepareRoom(room, local, progress, prior) {
|
|
1870
|
-
if (closed || authorization === undefined) {
|
|
1871
|
-
progress.complete = false;
|
|
1872
|
-
return false;
|
|
1873
|
-
}
|
|
1874
|
-
if (prior !== undefined && prior.replica.selection !== room.replica.selection) {
|
|
1875
|
-
listHeld(room.locator);
|
|
1876
|
-
emitRooms();
|
|
1877
|
-
journal.record(`selection changed ${room.locator}`);
|
|
1878
|
-
progress.complete = false;
|
|
1879
|
-
return false;
|
|
1880
|
-
}
|
|
1881
|
-
if (prior !== undefined && !declaredAsHeld(prior, room))
|
|
1882
|
-
await retainModel(local, room, null);
|
|
1883
|
-
if (prior !== undefined && (prior.name !== room.name || prior.handle !== room.handle)) {
|
|
1884
|
-
await keepNaming(local, room);
|
|
1885
|
-
}
|
|
1886
|
-
if (discoveryPaused || authorization === undefined)
|
|
1887
|
-
return false;
|
|
1888
|
-
return room.replica.admission === 'admitted';
|
|
1889
|
-
}
|
|
1890
|
-
async function caughtUp(room, local, progress, live) {
|
|
1891
|
-
if (options.adapter.roomStream !== undefined || authorization === undefined)
|
|
1892
|
-
return;
|
|
1893
|
-
if (!live() || !(await behind(installer, room)))
|
|
1894
|
-
return;
|
|
1895
|
-
const withheld = await advance(room, local, authorization, live);
|
|
1896
|
-
if (withheld !== null)
|
|
1897
|
-
await withhold(withheld, room, local, progress);
|
|
1898
|
-
}
|
|
1899
|
-
function revoked(model) {
|
|
1900
|
-
return model?.status === 'unavailable' && model.reason === 'revoked';
|
|
1901
|
-
}
|
|
1902
|
-
async function loadRoom(room, local, progress, live, unheld) {
|
|
1903
|
-
if (!live() || authorization === undefined)
|
|
1904
|
-
return;
|
|
1905
|
-
const prior = unheld === undefined ? await recordAt(room.locator) : undefined;
|
|
1906
|
-
if (prior !== undefined) {
|
|
1907
|
-
if (!(await prepareRoom(room, local, progress, prior)))
|
|
1908
|
-
return;
|
|
1909
|
-
if (revoked(prior.model)) {
|
|
1910
|
-
await reload(room, local, progress, live);
|
|
1911
|
-
return;
|
|
1912
|
-
}
|
|
1913
|
-
}
|
|
1914
|
-
const withheld = await advance(room, local, authorization, live, unheld);
|
|
1915
|
-
if (withheld !== null) {
|
|
1916
|
-
await withhold(withheld, room, local, progress);
|
|
1917
|
-
return;
|
|
1918
|
-
}
|
|
1919
|
-
await caughtUp(room, local, progress, live);
|
|
1920
|
-
}
|
|
1921
|
-
async function reload(room, local, progress, live) {
|
|
1922
|
-
try {
|
|
1923
|
-
if (live())
|
|
1924
|
-
reloadsDue.delete(room.locator);
|
|
1925
|
-
if (!live() || authorization === undefined)
|
|
1926
|
-
return;
|
|
1927
|
-
const prior = await recordAt(room.locator);
|
|
1928
|
-
const from = standingAt(store, room.locator, prior);
|
|
1929
|
-
const bootstrapped = await pullBootstrap(installer, authorization, room, local, live, prior);
|
|
1930
|
-
if (bootstrapped.kind !== 'staged') {
|
|
1931
|
-
if (live())
|
|
1932
|
-
await withhold(bootstrapped, room, local, progress);
|
|
1933
|
-
return;
|
|
1934
|
-
}
|
|
1935
|
-
await publish(installer, local, room, bootstrapped.record, { kind: 'bootstrapped', refreshed: bootstrapped.refreshed, from }, live);
|
|
1936
|
-
await caughtUp(room, local, progress, live);
|
|
1937
|
-
}
|
|
1938
|
-
finally {
|
|
1939
|
-
caughtUpTo(installer, room.locator);
|
|
1940
|
-
}
|
|
1941
|
-
}
|
|
1942
|
-
async function asked(authorized) {
|
|
1943
|
-
try {
|
|
1944
|
-
return (await options.adapter.discover?.(authorized)) ?? { kind: 'transport-uncertainty' };
|
|
1945
|
-
}
|
|
1946
|
-
catch {
|
|
1947
|
-
return { kind: 'transport-uncertainty' };
|
|
1948
|
-
}
|
|
1949
|
-
}
|
|
1950
|
-
function malformed(listed) {
|
|
1951
|
-
for (const room of listed) {
|
|
1952
|
-
if (room.operations === undefined ||
|
|
1953
|
-
!['write', 'erase'].every((operation) => Array.isArray(room.operations[operation]) &&
|
|
1954
|
-
room.operations[operation].every((scope) => typeof scope === 'string')) ||
|
|
1955
|
-
room.replica === undefined ||
|
|
1956
|
-
typeof room.replica.selection !== 'string' ||
|
|
1957
|
-
room.replica.selection.length === 0 ||
|
|
1958
|
-
!['admitted', 'unavailable'].includes(room.replica.admission) ||
|
|
1959
|
-
!['locked', 'open'].includes(room.replica.offline)) {
|
|
1960
|
-
return `invalid model or replica declaration ${room.locator}`;
|
|
1961
|
-
}
|
|
1962
|
-
if (!declaredSelection(room))
|
|
1963
|
-
return `invalid typed selection declaration ${room.locator}`;
|
|
1964
|
-
}
|
|
1965
|
-
return null;
|
|
1966
|
-
}
|
|
1967
|
-
async function signIn(local, signedIn) {
|
|
1968
|
-
if (JSON.stringify(signedIn) === JSON.stringify((await local.facts()).member))
|
|
1969
|
-
return;
|
|
1970
|
-
await local.fact('member', signedIn);
|
|
1971
|
-
emitMember(signedIn);
|
|
1972
|
-
}
|
|
1973
|
-
async function discoverRooms(local, progress, attempt, live, found) {
|
|
1974
|
-
if (closed || authorization === undefined) {
|
|
1975
|
-
notifyReady();
|
|
1976
|
-
return;
|
|
1977
|
-
}
|
|
1978
|
-
moveTo(transferring());
|
|
1979
|
-
discoveries += 1;
|
|
1980
|
-
const started = discoveries;
|
|
1981
|
-
const result = found ?? (await asked(authorization));
|
|
1982
|
-
if (result.kind === 'discovery')
|
|
1983
|
-
roomsNamed = true;
|
|
1984
|
-
if (!live()) {
|
|
1985
|
-
progress.complete = false;
|
|
1986
|
-
notifyReady();
|
|
1987
|
-
return;
|
|
1988
|
-
}
|
|
1989
|
-
journal.record(`connection discover ${result.kind}`);
|
|
1990
|
-
if (result.kind === 'transport-uncertainty') {
|
|
1991
|
-
await cachedModels(local);
|
|
1992
|
-
progress.reachable = false;
|
|
1993
|
-
progress.complete = false;
|
|
1994
|
-
notifyReady();
|
|
1995
|
-
return;
|
|
1996
|
-
}
|
|
1997
|
-
if (result.kind === 'contract-unavailable') {
|
|
1998
|
-
journal.record(`connection contract unavailable ${result.code}`);
|
|
1999
|
-
await refuse('missing-required-guarantee', {
|
|
2000
|
-
code: result.code,
|
|
2001
|
-
fields: result.fields,
|
|
2002
|
-
retryable: result.retryable,
|
|
2003
|
-
});
|
|
2004
|
-
notifyReady();
|
|
2005
|
-
return;
|
|
2006
|
-
}
|
|
2007
|
-
if (result.kind === 'revoked-authorization') {
|
|
2008
|
-
await refuse('revoked-authorization');
|
|
2009
|
-
notifyReady();
|
|
2010
|
-
return;
|
|
2011
|
-
}
|
|
2012
|
-
const invalid = malformed(result.rooms);
|
|
2013
|
-
if (invalid !== null) {
|
|
2014
|
-
journal.record(invalid);
|
|
2015
|
-
await refuse('missing-required-guarantee');
|
|
2016
|
-
notifyReady();
|
|
2017
|
-
return;
|
|
2018
|
-
}
|
|
2019
|
-
if (refusal !== null) {
|
|
2020
|
-
refusal = null;
|
|
2021
|
-
emitRefusal();
|
|
2022
|
-
}
|
|
2023
|
-
const held = await local.facts();
|
|
2024
|
-
if (JSON.stringify(result.providerLimits) !== JSON.stringify(held.providerLimits) ||
|
|
2025
|
-
JSON.stringify(result.capabilities) !== JSON.stringify(held.capabilities)) {
|
|
2026
|
-
await local.fact('providerLimits', result.providerLimits);
|
|
2027
|
-
await local.fact('capabilities', result.capabilities);
|
|
2028
|
-
}
|
|
2029
|
-
notifyReady();
|
|
2030
|
-
await signIn(local, result.member ?? null);
|
|
2031
|
-
await synchronize(await applyDiscovery(result.rooms, started), local, progress, attempt, live);
|
|
2032
|
-
}
|
|
2033
|
-
async function ensureAuthorization() {
|
|
2034
|
-
if (authorization !== undefined)
|
|
2035
|
-
return;
|
|
2036
|
-
try {
|
|
2037
|
-
authorization = await options.adapter.authorize();
|
|
2038
|
-
journal.record('connection authorize');
|
|
2039
|
-
}
|
|
2040
|
-
catch (error) {
|
|
2041
|
-
journal.record('connection authorize transport-uncertainty');
|
|
2042
|
-
throw error;
|
|
2043
|
-
}
|
|
2044
|
-
}
|
|
2045
|
-
async function admittedRead(work) {
|
|
2046
|
-
while (roomReads.free === 0) {
|
|
2047
|
-
await new Promise((admit) => roomReads.waiting.push(admit));
|
|
2048
|
-
}
|
|
2049
|
-
roomReads.free -= 1;
|
|
2050
|
-
try {
|
|
2051
|
-
await work();
|
|
2052
|
-
}
|
|
2053
|
-
finally {
|
|
2054
|
-
roomReads.free += 1;
|
|
2055
|
-
roomReads.waiting.shift()?.();
|
|
2056
|
-
}
|
|
2057
|
-
}
|
|
2058
|
-
function inRoom(locator, work) {
|
|
2059
|
-
roomWork += 1;
|
|
2060
|
-
const ran = (roomLanes.get(locator) ?? Promise.resolve()).then(async () => {
|
|
2061
|
-
try {
|
|
2062
|
-
return await work(continuing);
|
|
2063
|
-
}
|
|
2064
|
-
finally {
|
|
2065
|
-
roomWork -= 1;
|
|
2066
|
-
}
|
|
2067
|
-
});
|
|
2068
|
-
const chain = ran.then(() => undefined, () => undefined);
|
|
2069
|
-
roomLanes.set(locator, chain);
|
|
2070
|
-
working.add(chain);
|
|
2071
|
-
void chain.finally(() => {
|
|
2072
|
-
working.delete(chain);
|
|
2073
|
-
if (roomLanes.get(locator) === chain)
|
|
2074
|
-
roomLanes.delete(locator);
|
|
2075
|
-
});
|
|
2076
|
-
return ran;
|
|
2077
|
-
}
|
|
2078
|
-
function roomTask(locator, work) {
|
|
2079
|
-
if (stopping)
|
|
2080
|
-
return Promise.resolve();
|
|
2081
|
-
return inRoom(locator, (live) => admittedRead(() => work(live))).catch(() => undefined);
|
|
2082
|
-
}
|
|
2083
|
-
function advanced(room, local, authorized) {
|
|
2084
|
-
return inRoom(room.locator, (live) => advance(room, local, authorized, live));
|
|
2085
|
-
}
|
|
2086
|
-
function task(lane, work) {
|
|
2087
|
-
if (stopping)
|
|
2088
|
-
return Promise.resolve();
|
|
2089
|
-
const held = lanes.get(lane);
|
|
2090
|
-
const opened = socket.generation();
|
|
2091
|
-
const current = held?.generation === opened ? held : { generation: opened, chain: Promise.resolve() };
|
|
2092
|
-
const turn = current.generation;
|
|
2093
|
-
const live = () => turn === socket.generation() && continuing();
|
|
2094
|
-
const run = current.chain.then(() => work(live)).catch(() => undefined);
|
|
2095
|
-
current.chain = run;
|
|
2096
|
-
lanes.set(lane, current);
|
|
2097
|
-
working.add(run);
|
|
2098
|
-
void run.finally(() => working.delete(run));
|
|
2099
|
-
return run;
|
|
2100
|
-
}
|
|
2101
|
-
function sync(attempt, found) {
|
|
2102
|
-
return task(OPENING_LANE, async (live) => {
|
|
2103
|
-
if (!live()) {
|
|
2104
|
-
readyFromStore();
|
|
2105
|
-
return;
|
|
2106
|
-
}
|
|
2107
|
-
const progress = { complete: true, reachable: true };
|
|
2108
|
-
underway = attempt === 'opened' ? progress : null;
|
|
2109
|
-
if (attempt === 'opened')
|
|
2110
|
-
streamed = progress;
|
|
2111
|
-
complete = false;
|
|
2112
|
-
recorded = false;
|
|
2113
|
-
moveTo(transferring());
|
|
2114
|
-
try {
|
|
2115
|
-
const local = await openLocal();
|
|
2116
|
-
if (!live())
|
|
2117
|
-
return;
|
|
2118
|
-
await ensureAuthorization();
|
|
2119
|
-
if (!live())
|
|
2120
|
-
return;
|
|
2121
|
-
await discoverRooms(local, progress, attempt, live, found);
|
|
2122
|
-
}
|
|
2123
|
-
catch {
|
|
2124
|
-
progress.complete = false;
|
|
2125
|
-
if (attempt === 'opened' && !socket.isHanded())
|
|
2126
|
-
progress.reachable = false;
|
|
2127
|
-
}
|
|
2128
|
-
finally {
|
|
2129
|
-
notifyReady();
|
|
2130
|
-
}
|
|
2131
|
-
if (!live())
|
|
2132
|
-
return;
|
|
2133
|
-
if (attempt !== 'opened') {
|
|
2134
|
-
await finishOpening(progress, live);
|
|
2135
|
-
return;
|
|
2136
|
-
}
|
|
2137
|
-
if (socket.isHanded() || closed || progress.reachable)
|
|
2138
|
-
return;
|
|
2139
|
-
underway = null;
|
|
2140
|
-
await rest(progress, live);
|
|
2141
|
-
});
|
|
2142
|
-
}
|
|
2143
|
-
function resynchronize() {
|
|
2144
|
-
return task(OPENING_LANE, async (live) => {
|
|
2145
|
-
if (!live() || !socket.isAnnounced())
|
|
2146
|
-
return;
|
|
2147
|
-
const progress = { complete: true, reachable: true };
|
|
2148
|
-
moveTo(transferring());
|
|
2149
|
-
try {
|
|
2150
|
-
const local = await openLocal();
|
|
2151
|
-
if (!live())
|
|
2152
|
-
return;
|
|
2153
|
-
await synchronize([...discovered.values()], local, progress, 'unstreamed', live);
|
|
2154
|
-
}
|
|
2155
|
-
catch {
|
|
2156
|
-
progress.complete = false;
|
|
2157
|
-
}
|
|
2158
|
-
if (live())
|
|
2159
|
-
await finishOpening(progress, live);
|
|
2160
|
-
});
|
|
2161
|
-
}
|
|
2162
|
-
function countAuthorityChange() {
|
|
2163
|
-
authorityChanges += 1;
|
|
2164
|
-
for (const listener of authorityChangeListeners)
|
|
2165
|
-
listener(authorityChanges);
|
|
2166
|
-
lastAnswers.again();
|
|
2167
|
-
}
|
|
2168
|
-
function changeAuthority(change) {
|
|
2169
|
-
return task(OPENING_LANE, async (live) => {
|
|
2170
|
-
if (!live() || !socket.isAnnounced())
|
|
2171
|
-
return;
|
|
2172
|
-
countAuthorityChange();
|
|
2173
|
-
const progress = { complete: true, reachable: true };
|
|
2174
|
-
moveTo(transferring());
|
|
2175
|
-
try {
|
|
2176
|
-
const local = await openLocal();
|
|
2177
|
-
if (!live())
|
|
2178
|
-
return;
|
|
2179
|
-
const invalid = malformed(change.rooms);
|
|
2180
|
-
if (invalid !== null) {
|
|
2181
|
-
journal.record(invalid);
|
|
2182
|
-
await refuse('missing-required-guarantee');
|
|
2183
|
-
return;
|
|
2184
|
-
}
|
|
2185
|
-
if (change.member !== undefined)
|
|
2186
|
-
await signIn(local, change.member);
|
|
2187
|
-
discoveries += 1;
|
|
2188
|
-
const listed = await applyDiscovery(change.rooms, discoveries, change.left);
|
|
2189
|
-
await synchronize(listed, local, progress, 'authorized', live);
|
|
2190
|
-
}
|
|
2191
|
-
catch {
|
|
2192
|
-
progress.complete = false;
|
|
2193
|
-
}
|
|
2194
|
-
if (!live())
|
|
2195
|
-
return;
|
|
2196
|
-
if (!progress.complete)
|
|
2197
|
-
complete = false;
|
|
2198
|
-
await rest(progress, live);
|
|
2199
|
-
});
|
|
2200
|
-
}
|
|
2201
|
-
function closedSocket(revoked, lost, opened) {
|
|
2202
|
-
notifyReady();
|
|
2203
|
-
unreadied();
|
|
2204
|
-
connectionLost();
|
|
2205
|
-
if (lost)
|
|
2206
|
-
conversations.lost();
|
|
2207
|
-
if (revoked) {
|
|
2208
|
-
void refuse('revoked-authorization');
|
|
2209
|
-
return;
|
|
2210
|
-
}
|
|
2211
|
-
complete = false;
|
|
2212
|
-
if (streamed !== null)
|
|
2213
|
-
streamed.complete = false;
|
|
2214
|
-
moveTo({
|
|
2215
|
-
...status,
|
|
2216
|
-
state: status.state === 'synced' ? 'syncing' : status.state,
|
|
2217
|
-
providerUnreachable: status.providerUnreachable || (!opened && lost),
|
|
2218
|
-
});
|
|
2219
|
-
void closing(opened);
|
|
2220
|
-
}
|
|
2221
|
-
function closing(opened) {
|
|
2222
|
-
return task(OPENING_LANE, async (live) => {
|
|
2223
|
-
if (!live())
|
|
2224
|
-
return;
|
|
2225
|
-
const local = await openLocal();
|
|
2226
|
-
if (!opened && !socket.isOpen())
|
|
2227
|
-
await cachedModels(local);
|
|
2228
|
-
await rest(null, live);
|
|
2229
|
-
});
|
|
2230
|
-
}
|
|
2231
|
-
async function cachedModels(local) {
|
|
2232
|
-
const held = await storedRooms();
|
|
2233
|
-
await settleModels(listedLocators(held).flatMap((locator) => {
|
|
2234
|
-
const room = discovered.get(locator) ?? declarationOf(locator, held.get(locator))?.room;
|
|
2235
|
-
return room === undefined ? [] : [room];
|
|
2236
|
-
}), 'cached', local);
|
|
2237
|
-
}
|
|
2238
|
-
function caughtUpRooms(expired) {
|
|
2239
|
-
const progress = underway;
|
|
2240
|
-
underway = null;
|
|
2241
|
-
socket.named();
|
|
2242
|
-
const cycle = progress ?? { complete: true, reachable: true };
|
|
2243
|
-
replaced(store, expired);
|
|
2244
|
-
const installing = store.installs.underway();
|
|
2245
|
-
const held = progress === null ? storedRooms(expired) : storedRooms();
|
|
2246
|
-
const reloading = progress === null
|
|
2247
|
-
? Promise.resolve([])
|
|
2248
|
-
: held.then((stored) => owedReloads(expired, stored));
|
|
2249
|
-
const replacing = Promise.all([held, reloading]).then(([stored, owed]) => reloadNow((room) => expired.includes(room.locator) || owed.includes(room.locator), cycle, stored, installing));
|
|
2250
|
-
return task(OPENING_LANE, async (live) => {
|
|
2251
|
-
if (!live())
|
|
2252
|
-
return;
|
|
2253
|
-
if (progress === null) {
|
|
2254
|
-
await replacing;
|
|
2255
|
-
await rest(cycle, live);
|
|
2256
|
-
return;
|
|
2257
|
-
}
|
|
2258
|
-
try {
|
|
2259
|
-
const local = await openLocal();
|
|
2260
|
-
await replacing;
|
|
2261
|
-
if (progress.reachable) {
|
|
2262
|
-
const stored = await held;
|
|
2263
|
-
const owed = await reloading;
|
|
2264
|
-
await settleModels([...discovered.values()].filter((room) => stored.has(room.locator) &&
|
|
2265
|
-
!expired.includes(room.locator) &&
|
|
2266
|
-
!owed.includes(room.locator)), 'current', local);
|
|
2267
|
-
}
|
|
2268
|
-
}
|
|
2269
|
-
catch {
|
|
2270
|
-
progress.complete = false;
|
|
2271
|
-
progress.reachable = false;
|
|
2272
|
-
}
|
|
2273
|
-
if (live())
|
|
2274
|
-
await finishOpening(progress, live);
|
|
2275
|
-
});
|
|
2276
|
-
}
|
|
2277
|
-
function owedReloads(expired, held) {
|
|
2278
|
-
const reloading = [...discovered.values()]
|
|
2279
|
-
.filter((room) => !expired.includes(room.locator) && revoked(held.get(room.locator)?.record.model))
|
|
2280
|
-
.map((room) => room.locator);
|
|
2281
|
-
for (const locator of reloading)
|
|
2282
|
-
catching(store, locator);
|
|
2283
|
-
recoverReads();
|
|
2284
|
-
return reloading;
|
|
2285
|
-
}
|
|
2286
|
-
async function reloadNow(replaced, progress, held, installing) {
|
|
2287
|
-
const reloaded = [...discovered.values()].filter((room) => held.has(room.locator) && !installing.has(room.locator) && replaced(room));
|
|
2288
|
-
const acquired = [...discovered.values()].filter((room) => (!held.has(room.locator) || installing.has(room.locator)) &&
|
|
2289
|
-
room.replica.admission === 'admitted' &&
|
|
2290
|
-
replaced(room));
|
|
2291
|
-
for (const room of reloaded)
|
|
2292
|
-
reloadsDue.add(room.locator);
|
|
2293
|
-
try {
|
|
2294
|
-
const local = await openLocal();
|
|
2295
|
-
const kept = acquired.length === 0 ? new Set() : (await local.held()).staged;
|
|
2296
|
-
for (const room of reloaded) {
|
|
2297
|
-
void roomTask(room.locator, (live) => reload(room, local, progress, live)).then(() => rest(progress));
|
|
2298
|
-
}
|
|
2299
|
-
for (const room of acquired) {
|
|
2300
|
-
const unheld = installing.has(room.locator)
|
|
2301
|
-
? undefined
|
|
2302
|
-
: { staged: kept.has(room.locator) };
|
|
2303
|
-
void roomTask(room.locator, (live) => loadRoom(room, local, progress, live, unheld)).then(() => rest(progress));
|
|
2304
|
-
}
|
|
2305
|
-
}
|
|
2306
|
-
catch {
|
|
2307
|
-
for (const room of reloaded)
|
|
2308
|
-
reloadsDue.delete(room.locator);
|
|
2309
|
-
progress.reachable = false;
|
|
2310
|
-
}
|
|
2311
|
-
}
|
|
2312
|
-
async function settleModels(held, status, local) {
|
|
2313
|
-
const owed = (model) => model.status !== 'unavailable' && model.status !== status;
|
|
2314
|
-
const declaredAnew = await exclusive(async () => {
|
|
2315
|
-
if (!continuing())
|
|
2316
|
-
return [];
|
|
2317
|
-
const stored = await storedRooms(held.map(({ locator }) => locator));
|
|
2318
|
-
const unchanged = held.filter((room) => {
|
|
2319
|
-
const record = stored.get(room.locator)?.record;
|
|
2320
|
-
return (owed(record?.model ?? unheldModels.get(room.locator) ?? NO_MODEL) &&
|
|
2321
|
-
declaredAsHeld(record, room));
|
|
2322
|
-
});
|
|
2323
|
-
const records = unchanged.flatMap((room) => {
|
|
2324
|
-
const record = stored.get(room.locator)?.record;
|
|
2325
|
-
return record === undefined
|
|
2326
|
-
? []
|
|
2327
|
-
: [{ ...record, model: { ...record.model, status } }];
|
|
2328
|
-
});
|
|
2329
|
-
if (records.length > 0) {
|
|
2330
|
-
try {
|
|
2331
|
-
await local.commit({
|
|
2332
|
-
rooms: records.map((record) => ({
|
|
2333
|
-
locator: record.locator,
|
|
2334
|
-
key: record.key,
|
|
2335
|
-
record: storedRecord(record),
|
|
2336
|
-
composed: composedOf(composed(record)),
|
|
2337
|
-
})),
|
|
2338
|
-
});
|
|
2339
|
-
}
|
|
2340
|
-
catch {
|
|
2341
|
-
journal.record(`model ${status} persistence failed`);
|
|
2342
|
-
}
|
|
2343
|
-
}
|
|
2344
|
-
for (const room of unchanged) {
|
|
2345
|
-
const unheld = unheldModels.get(room.locator);
|
|
2346
|
-
if (!stored.has(room.locator) && unheld !== undefined) {
|
|
2347
|
-
unheldModels.set(room.locator, { ...unheld, status });
|
|
2348
|
-
}
|
|
2349
|
-
list(room, room.replica.admission === 'admitted' && !unreadable.has(room.locator));
|
|
2350
|
-
}
|
|
2351
|
-
if (unchanged.length > 0)
|
|
2352
|
-
emitRooms();
|
|
2353
|
-
return held.filter((room) => {
|
|
2354
|
-
const record = stored.get(room.locator)?.record;
|
|
2355
|
-
return (!unchanged.includes(room) &&
|
|
2356
|
-
owed(record?.model ?? unheldModels.get(room.locator) ?? NO_MODEL));
|
|
2357
|
-
});
|
|
2358
|
-
});
|
|
2359
|
-
for (const room of declaredAnew) {
|
|
2360
|
-
const model = await modelNow(room.locator);
|
|
2361
|
-
if (owed(model))
|
|
2362
|
-
await retainModel(local, room, { ...model, status });
|
|
2363
|
-
}
|
|
2364
|
-
}
|
|
2365
|
-
function repairLater(locator, when) {
|
|
2366
|
-
if (!socket.isOpen() || repairing.has(locator))
|
|
2367
|
-
return;
|
|
2368
|
-
void recovery.attempt(`repair ${locator}`, (signal) => repairPass(locator, signal), when);
|
|
2369
|
-
}
|
|
2370
|
-
async function repairPass(locator, signal) {
|
|
2371
|
-
const room = discovered.get(locator);
|
|
2372
|
-
if (!socket.isOpen() || closed || stopping || discoveryPaused || !stale.has(locator))
|
|
2373
|
-
return 'done';
|
|
2374
|
-
if (room === undefined || room.replica.admission !== 'admitted')
|
|
2375
|
-
return 'done';
|
|
2376
|
-
const progress = { complete: true, reachable: true };
|
|
2377
|
-
repairing.add(locator);
|
|
2378
|
-
try {
|
|
2379
|
-
const local = await openLocal();
|
|
2380
|
-
await roomTask(locator, async (live) => {
|
|
2381
|
-
if (!live() || authorization === undefined)
|
|
2382
|
-
return;
|
|
2383
|
-
const withheld = await advance(room, local, authorization, live);
|
|
2384
|
-
if (withheld !== null && live())
|
|
2385
|
-
await withhold(withheld, room, local, progress);
|
|
2386
|
-
});
|
|
2387
|
-
}
|
|
2388
|
-
catch {
|
|
2389
|
-
stale.add(locator);
|
|
2390
|
-
progress.reachable = false;
|
|
2391
|
-
}
|
|
2392
|
-
finally {
|
|
2393
|
-
repairing.delete(locator);
|
|
2394
|
-
}
|
|
2395
|
-
if (signal.aborted)
|
|
2396
|
-
return 'done';
|
|
2397
|
-
await rest(progress);
|
|
2398
|
-
if (!stale.has(locator)) {
|
|
2399
|
-
if ((await (await openLocal()).order(locator)).length > 0)
|
|
2400
|
-
submitLater(accepting, locator);
|
|
2401
|
-
return 'done';
|
|
2402
|
-
}
|
|
2403
|
-
if (!repairable.has(locator))
|
|
2404
|
-
return 'done';
|
|
2405
|
-
const reset = repairable.get(locator);
|
|
2406
|
-
return reset === undefined ? 'again' : { at: Date.parse(reset) };
|
|
2407
|
-
}
|
|
2408
|
-
function resendLater() {
|
|
2409
|
-
if (!socket.isOpen())
|
|
2410
|
-
return;
|
|
2411
|
-
void recovery.attempt('resend', resendPass, { again: true });
|
|
2412
|
-
}
|
|
2413
|
-
async function resendPass(signal) {
|
|
2414
|
-
if (!socket.isOpen() || closed || stopping || discoveryPaused)
|
|
2415
|
-
return 'done';
|
|
2416
|
-
const reading = { complete: true, reachable: true };
|
|
2417
|
-
try {
|
|
2418
|
-
await conversations.resend(reading);
|
|
2419
|
-
}
|
|
2420
|
-
catch {
|
|
2421
|
-
reading.reachable = false;
|
|
2422
|
-
}
|
|
2423
|
-
if (signal.aborted)
|
|
2424
|
-
return 'done';
|
|
2425
|
-
return reading.reachable && reading.complete ? 'done' : 'again';
|
|
2426
|
-
}
|
|
2427
|
-
async function finishOpening(progress, live) {
|
|
2428
|
-
try {
|
|
2429
|
-
if (!live()) {
|
|
2430
|
-
progress.complete = false;
|
|
2431
|
-
return;
|
|
2432
|
-
}
|
|
2433
|
-
await submitAll(accepting);
|
|
2434
|
-
for (const locator of stale)
|
|
2435
|
-
if (repairable.has(locator))
|
|
2436
|
-
repairLater(locator, {});
|
|
2437
|
-
await conversations.reconcile();
|
|
2438
|
-
const reading = { complete: true, reachable: true };
|
|
2439
|
-
await conversations.catchUp(reading);
|
|
2440
|
-
if (progress.reachable && reading.reachable)
|
|
2441
|
-
await conversations.resend(reading);
|
|
2442
|
-
if (progress.reachable)
|
|
2443
|
-
void recovery.attempt('calls', resendCalls);
|
|
2444
|
-
if (!reading.reachable) {
|
|
2445
|
-
progress.reachable = false;
|
|
2446
|
-
resendLater();
|
|
2447
|
-
}
|
|
2448
|
-
}
|
|
2449
|
-
catch {
|
|
2450
|
-
progress.complete = false;
|
|
2451
|
-
}
|
|
2452
|
-
finally {
|
|
2453
|
-
if (live()) {
|
|
2454
|
-
complete = progress.complete;
|
|
2455
|
-
await rest(progress, live);
|
|
2456
|
-
}
|
|
2457
|
-
}
|
|
2458
|
-
}
|
|
2459
|
-
async function rest(progress, live = () => !closed) {
|
|
2460
|
-
if (!live())
|
|
2461
|
-
return;
|
|
2462
|
-
const settled = () => complete && roomWork === 0 && !socket.naming() && stale.size === 0;
|
|
2463
|
-
let synced = settled();
|
|
2464
|
-
if (synced && !recorded) {
|
|
2465
|
-
const queued = await queuedWork().catch(() => true);
|
|
2466
|
-
if (!live())
|
|
2467
|
-
return;
|
|
2468
|
-
synced = !queued && settled();
|
|
2469
|
-
}
|
|
2470
|
-
if (synced && !recorded) {
|
|
2471
|
-
const completedAt = new Date().toISOString();
|
|
2472
|
-
try {
|
|
2473
|
-
await (await openLocal()).fact('lastCompleted', completedAt);
|
|
2474
|
-
if (!live())
|
|
2475
|
-
return;
|
|
2476
|
-
recorded = true;
|
|
2477
|
-
}
|
|
2478
|
-
catch {
|
|
2479
|
-
synced = false;
|
|
2480
|
-
}
|
|
2481
|
-
}
|
|
2482
|
-
moveTo({
|
|
2483
|
-
state: synced && complete && roomWork === 0 && !socket.naming() ? 'synced' : 'syncing',
|
|
2484
|
-
providerUnreachable: progress === null ? status.providerUnreachable : !progress.reachable,
|
|
2485
|
-
resting: true,
|
|
2486
|
-
});
|
|
2487
|
-
}
|
|
2488
|
-
function applyRoom(change) {
|
|
2489
|
-
const { locator } = change;
|
|
2490
|
-
if (stopping)
|
|
2491
|
-
return Promise.resolve();
|
|
2492
|
-
return inRoom(locator, async (live) => {
|
|
2493
|
-
await socket.answered(locator);
|
|
2494
|
-
const progress = underway ?? { complete: true, reachable: true };
|
|
2495
|
-
try {
|
|
2496
|
-
const local = await openLocal();
|
|
2497
|
-
for (;;) {
|
|
2498
|
-
if (!live())
|
|
2499
|
-
return null;
|
|
2500
|
-
const room = discovered.get(locator);
|
|
2501
|
-
if (room === undefined || room.replica.admission !== 'admitted')
|
|
2502
|
-
return null;
|
|
2503
|
-
const held = await recordAt(locator);
|
|
2504
|
-
if (held === undefined) {
|
|
2505
|
-
if (change.from !== null)
|
|
2506
|
-
return null;
|
|
2507
|
-
await installCarried(room, local, change, live, progress);
|
|
2508
|
-
return progress;
|
|
2509
|
-
}
|
|
2510
|
-
if (held.replica.selection !== room.replica.selection)
|
|
2511
|
-
return null;
|
|
2512
|
-
const at = held.roomVersion;
|
|
2513
|
-
if (options.adapter.reached(locator, at, change.roomVersion))
|
|
2514
|
-
return progress;
|
|
2515
|
-
if (change.from === null || !options.adapter.reached(locator, at, change.from)) {
|
|
2516
|
-
await name(locator);
|
|
2517
|
-
return progress;
|
|
2518
|
-
}
|
|
2519
|
-
if (underway === null)
|
|
2520
|
-
moveTo(transferring());
|
|
2521
|
-
const writing = () => live() && !full;
|
|
2522
|
-
const refreshed = await store.ahead((hold) => pullRefresh(installer, authorization ?? {}, held, writing, hold, () => change.read()));
|
|
2523
|
-
if (refreshed.kind !== 'advanced')
|
|
2524
|
-
return progress;
|
|
2525
|
-
const landing = {
|
|
2526
|
-
kind: 'refreshed',
|
|
2527
|
-
from: refreshed.from,
|
|
2528
|
-
remodelled: refreshed.remodelled,
|
|
2529
|
-
};
|
|
2530
|
-
if (await publish(installer, local, room, refreshed.record, landing, writing)) {
|
|
2531
|
-
stale.delete(locator);
|
|
2532
|
-
if (!declaredAsHeld(held, room))
|
|
2533
|
-
await retainModel(local, room, null);
|
|
2534
|
-
return progress;
|
|
2535
|
-
}
|
|
2536
|
-
}
|
|
2537
|
-
}
|
|
2538
|
-
catch {
|
|
2539
|
-
if (!stale.has(locator))
|
|
2540
|
-
void name(locator);
|
|
2541
|
-
stale.add(locator);
|
|
2542
|
-
return progress;
|
|
2543
|
-
}
|
|
2544
|
-
}).then((progress) => rest(progress), () => undefined);
|
|
2545
|
-
}
|
|
2546
|
-
async function installCarried(room, local, change, live, progress) {
|
|
2547
|
-
if (authorization === undefined)
|
|
2548
|
-
return;
|
|
2549
|
-
if (underway === null)
|
|
2550
|
-
moveTo(transferring());
|
|
2551
|
-
const prior = await recordAt(room.locator);
|
|
2552
|
-
const from = standingAt(store, room.locator, prior);
|
|
2553
|
-
const page = await change.read();
|
|
2554
|
-
const carried = {
|
|
2555
|
-
bootstrap: () => Promise.resolve({
|
|
2556
|
-
kind: 'bootstrap',
|
|
2557
|
-
roomVersion: page.roomVersion,
|
|
2558
|
-
...(page.models === undefined ? {} : { models: page.models }),
|
|
2559
|
-
documents: page.documents,
|
|
2560
|
-
facts: page.facts,
|
|
2561
|
-
revisions: page.revisions,
|
|
2562
|
-
...(page.fileIds === undefined ? {} : { fileIds: page.fileIds }),
|
|
2563
|
-
...(page.created === undefined ? {} : { created: page.created }),
|
|
2564
|
-
changes: page.changes,
|
|
2565
|
-
binaryFiles: page.binaryFiles,
|
|
2566
|
-
...(page.folders === undefined ? {} : { folders: page.folders }),
|
|
2567
|
-
}),
|
|
2568
|
-
reached: (locator, roomVersion, target) => options.adapter.reached(locator, roomVersion, target),
|
|
2569
|
-
};
|
|
2570
|
-
const bootstrapped = await pullBootstrap(installer, authorization, room, local, live, prior, carried);
|
|
2571
|
-
if (bootstrapped.kind !== 'staged') {
|
|
2572
|
-
if (live())
|
|
2573
|
-
await withhold(bootstrapped, room, local, progress);
|
|
2574
|
-
return;
|
|
2575
|
-
}
|
|
2576
|
-
await publish(installer, local, room, bootstrapped.record, { kind: 'bootstrapped', refreshed: bootstrapped.refreshed, from }, live);
|
|
2577
|
-
}
|
|
2578
|
-
function writesHeld() {
|
|
2579
|
-
return serviceMode === 'write-halted' || haltedRefusal;
|
|
2580
|
-
}
|
|
2581
|
-
function signalled(mode) {
|
|
2582
|
-
if (closed)
|
|
2583
|
-
return;
|
|
2584
|
-
if (mode === 'open')
|
|
2585
|
-
openSignals += 1;
|
|
2586
|
-
const releasing = mode === 'open' && (haltedRefusal || serviceMode === 'write-halted');
|
|
2587
|
-
if (mode === 'open')
|
|
2588
|
-
haltedRefusal = false;
|
|
2589
|
-
if (mode !== serviceMode) {
|
|
2590
|
-
serviceMode = mode;
|
|
2591
|
-
queueReadiness();
|
|
2592
|
-
journal.record(`service mode ${mode}`);
|
|
2593
|
-
for (const listener of serviceModeListeners)
|
|
2594
|
-
listener(mode);
|
|
2595
|
-
}
|
|
2596
|
-
if (releasing)
|
|
2597
|
-
submitAll(accepting);
|
|
2598
|
-
}
|
|
2599
|
-
function haltedSince(opens) {
|
|
2600
|
-
if (serviceMode === 'open' && openSignals !== opens)
|
|
2601
|
-
return false;
|
|
2602
|
-
haltedRefusal = true;
|
|
2603
|
-
return true;
|
|
2604
|
-
}
|
|
2605
|
-
function pendingShown() {
|
|
2606
|
-
if (!localSaves)
|
|
2607
|
-
recorded = false;
|
|
2608
|
-
return moveTo(localSaves ? status : { ...status, state: 'pending', resting: false });
|
|
2609
|
-
}
|
|
2610
|
-
function scheduleCurrency() {
|
|
2611
|
-
if (currencyQueued === socket.generation() || closed || stopping)
|
|
2612
|
-
return;
|
|
2613
|
-
currencyQueued = socket.generation();
|
|
2614
|
-
void task(CONVERSATION_LANE, runCurrency);
|
|
2615
|
-
}
|
|
2616
|
-
async function runCurrency(live) {
|
|
2617
|
-
if (currencyQueued === socket.generation())
|
|
2618
|
-
currencyQueued = null;
|
|
2619
|
-
if (!live())
|
|
2620
|
-
return;
|
|
2621
|
-
await conversations.reconcile();
|
|
2622
|
-
if (conversations.current()) {
|
|
2623
|
-
show();
|
|
2624
|
-
return;
|
|
2625
|
-
}
|
|
2626
|
-
const progress = { complete: false, reachable: !status.providerUnreachable };
|
|
2627
|
-
moveTo(transferring());
|
|
2628
|
-
try {
|
|
2629
|
-
await conversations.catchUp(progress);
|
|
2630
|
-
}
|
|
2631
|
-
catch {
|
|
2632
|
-
progress.reachable = false;
|
|
2633
|
-
}
|
|
2634
|
-
await rest(progress, live);
|
|
2635
|
-
}
|
|
2636
|
-
function close() {
|
|
2637
|
-
if (closed)
|
|
2638
|
-
return;
|
|
2639
|
-
closed = true;
|
|
2640
|
-
notifyDiscovered();
|
|
2641
|
-
unreadied();
|
|
2642
|
-
if (probing !== null)
|
|
2643
|
-
clearInterval(probing);
|
|
2644
|
-
connectionLive();
|
|
2645
|
-
lockListeners.clear();
|
|
2646
|
-
readinessWatches.clear();
|
|
2647
|
-
socket.withdraw();
|
|
2648
|
-
withdrawServiceMode?.();
|
|
2649
|
-
conversations.close();
|
|
2650
|
-
void recovery.retire();
|
|
2651
|
-
roomListeners.clear();
|
|
2652
|
-
unlistedRooms.clear();
|
|
2653
|
-
refusalListeners.clear();
|
|
2654
|
-
storageLossListeners.clear();
|
|
2655
|
-
statusListeners.clear();
|
|
2656
|
-
serviceModeListeners.clear();
|
|
2657
|
-
memberListeners.clear();
|
|
2658
|
-
authorityChangeListeners.clear();
|
|
2659
|
-
liveQueries.close();
|
|
2660
|
-
figures.close();
|
|
2661
|
-
queueSubscriptions.clear();
|
|
2662
|
-
store.close();
|
|
2663
|
-
}
|
|
2664
|
-
async function stop() {
|
|
2665
|
-
stopping = true;
|
|
2666
|
-
unreadied();
|
|
2667
|
-
socket.stopReads();
|
|
2668
|
-
await recovery.retire();
|
|
2669
|
-
await Promise.all(working);
|
|
2670
|
-
await store.written();
|
|
2671
|
-
close();
|
|
2672
|
-
}
|
|
2673
|
-
const withdrawServiceMode = options.adapter.serviceMode?.observe(signalled);
|
|
2674
|
-
socket.observeConversations((event) => {
|
|
2675
|
-
if (event.kind === 'list' || event.kind === 'listed' || event.kind === 'unlisted')
|
|
2676
|
-
queueReadiness();
|
|
2677
|
-
conversations.receive(event);
|
|
2678
|
-
if (event.kind !== 'gap' || conversations.current())
|
|
2679
|
-
return;
|
|
2680
|
-
show();
|
|
2681
|
-
scheduleCurrency();
|
|
2682
|
-
});
|
|
2683
|
-
options.signal?.addEventListener('abort', close, { once: true });
|
|
2684
|
-
if (options.signal?.aborted)
|
|
2685
|
-
close();
|
|
2686
|
-
socket.observe(() => !closed && !stopping, (event) => {
|
|
2687
|
-
if (event.kind === 'opened') {
|
|
2688
|
-
connectionLive();
|
|
2689
|
-
conversations.reopened();
|
|
2690
|
-
lastAnswers.reopened();
|
|
2691
|
-
recovery.resume();
|
|
2692
|
-
}
|
|
2693
|
-
else if (event.kind === 'discovered') {
|
|
2694
|
-
if (event.missed)
|
|
2695
|
-
countAuthorityChange();
|
|
2696
|
-
void sync('opened', event.discovery);
|
|
2697
|
-
}
|
|
2698
|
-
else if (event.kind === 'closed')
|
|
2699
|
-
closedSocket(event.revoked, event.lost, event.opened);
|
|
2700
|
-
else if (event.kind === 'caught-up')
|
|
2701
|
-
void caughtUpRooms(event.expired);
|
|
2702
|
-
else if (event.kind === 'authority-changed') {
|
|
2703
|
-
if (socket.isAnnounced() && socket.isOpen())
|
|
2704
|
-
fenceLive(event.left);
|
|
2705
|
-
void changeAuthority(event);
|
|
2706
|
-
}
|
|
2707
|
-
else
|
|
2708
|
-
void applyRoom(event);
|
|
2709
|
-
});
|
|
2710
|
-
void openLocal()
|
|
2711
|
-
.then(async (local) => {
|
|
2712
|
-
if ((await local.facts()).capabilities.length > 0)
|
|
2713
|
-
readyFromStore();
|
|
2714
|
-
})
|
|
2715
|
-
.catch(() => {
|
|
2716
|
-
readyFromStore();
|
|
2717
|
-
});
|
|
2718
|
-
if (options.adapter.roomStream === undefined)
|
|
2719
|
-
void sync('unstreamed', null);
|
|
2720
|
-
async function planned(input, reach) {
|
|
2721
|
-
const plan = planWithin(input, await roomFacts(reach), planReach(reach));
|
|
2722
|
-
const named = plan.paths !== null &&
|
|
2723
|
-
plan.rooms !== null &&
|
|
2724
|
-
plan.paths.length * plan.rooms.length <= MAXIMUM_WINDOW;
|
|
2725
|
-
return plan.limit === null && named ? plan : { ...plan, limit: capped(plan.limit) };
|
|
2726
|
-
}
|
|
2727
|
-
function watchPlan(plan, deliver, fail) {
|
|
2728
|
-
return watchSeeded(seeding, plan, deliver, (behind, answered) => liveQueries.watch(plan, identityOf(plan), (held) => {
|
|
2729
|
-
const cover = covered(plan, held);
|
|
2730
|
-
awaitPreparation(cover);
|
|
2731
|
-
return behind(cover, held);
|
|
2732
|
-
}, answered, fail));
|
|
2733
|
-
}
|
|
2734
|
-
function watchPlanned(planning, deliver, fail) {
|
|
2735
|
-
let stop = null;
|
|
2736
|
-
let stopped = false;
|
|
2737
|
-
planning.then((plan) => {
|
|
2738
|
-
if (!stopped)
|
|
2739
|
-
stop = watchPlan(plan, deliver, fail);
|
|
2740
|
-
}, (error) => {
|
|
2741
|
-
if (!stopped)
|
|
2742
|
-
fail(error);
|
|
2743
|
-
});
|
|
2744
|
-
return () => {
|
|
2745
|
-
stopped = true;
|
|
2746
|
-
stop?.();
|
|
2747
|
-
};
|
|
2748
|
-
}
|
|
2749
|
-
async function searched(input, reach) {
|
|
2750
|
-
const asked = searchPlanOf(input);
|
|
2751
|
-
await ready;
|
|
2752
|
-
const search = options.adapter.search;
|
|
2753
|
-
if (search === undefined) {
|
|
2754
|
-
return {
|
|
2755
|
-
kind: 'undeclared',
|
|
2756
|
-
code: 'action-undeclared',
|
|
2757
|
-
fields: { name: 'search' },
|
|
2758
|
-
retryable: false,
|
|
2759
|
-
};
|
|
2760
|
-
}
|
|
2761
|
-
if (closed || refusal !== null || (await connectionNow()) !== 'connected')
|
|
2762
|
-
return OFFLINE;
|
|
2763
|
-
const reached = reach === null ? null : new Set(reach.rooms);
|
|
2764
|
-
const named = asked.rooms === null
|
|
2765
|
-
? reached === null
|
|
2766
|
-
? null
|
|
2767
|
-
: [...reached]
|
|
2768
|
-
: asked.rooms.filter((room) => reached === null || reached.has(room));
|
|
2769
|
-
const held = named === null ? null : await storedRooms(named);
|
|
2770
|
-
const rooms = named === null ? null : named.filter((room) => held.has(room)).sort();
|
|
2771
|
-
if (rooms !== null && rooms.length === 0)
|
|
2772
|
-
return { kind: 'found', documents: [] };
|
|
2773
|
-
try {
|
|
2774
|
-
await ensureAuthorization();
|
|
2775
|
-
}
|
|
2776
|
-
catch {
|
|
2777
|
-
return UNCERTAIN;
|
|
2778
|
-
}
|
|
2779
|
-
if (closed || authorization === undefined || refusal !== null)
|
|
2780
|
-
return UNCERTAIN;
|
|
2781
|
-
journal.record('transfer search');
|
|
2782
|
-
try {
|
|
2783
|
-
const result = await search(authorization, { text: asked.text, rooms });
|
|
2784
|
-
if (result.kind === 'revoked-authorization')
|
|
2785
|
-
await refuse('revoked-authorization');
|
|
2786
|
-
journal.record(`outcome search ${result.kind}`);
|
|
2787
|
-
if (result.kind === 'rejected')
|
|
2788
|
-
return result;
|
|
2789
|
-
if (result.kind !== 'found')
|
|
2790
|
-
return failure(result);
|
|
2791
|
-
const kept = rooms === null ? null : new Set(rooms);
|
|
2792
|
-
return {
|
|
2793
|
-
kind: 'found',
|
|
2794
|
-
documents: result.documents.filter((hit) => kept === null || kept.has(hit.room)),
|
|
2795
|
-
};
|
|
2796
|
-
}
|
|
2797
|
-
catch {
|
|
2798
|
-
journal.record('outcome search transport-uncertainty');
|
|
2799
|
-
return UNCERTAIN;
|
|
2800
|
-
}
|
|
2801
|
-
}
|
|
2802
|
-
const watch = (topic, input, deliver, fail) => {
|
|
2803
|
-
if (topic === 'room-drafts')
|
|
2804
|
-
return watchRoomDrafts(drafting, deliver);
|
|
2805
|
-
if (topic === 'conversation') {
|
|
2806
|
-
return conversations.watch(input[0], input[1], deliver);
|
|
2807
|
-
}
|
|
2808
|
-
if (topic === 'conversations')
|
|
2809
|
-
return conversations.watchList(deliver);
|
|
2810
|
-
if (topic === 'agents')
|
|
2811
|
-
return conversations.watchAgents(deliver);
|
|
2812
|
-
if (topic === 'figures')
|
|
2813
|
-
return figures.watch(deliver, fail);
|
|
2814
|
-
if (topic === 'read')
|
|
2815
|
-
return lastAnswers.watch(input[0], input[1], deliver);
|
|
2816
|
-
if (topic === 'presentation')
|
|
2817
|
-
return watchPresentation(drafting, input[0], deliver);
|
|
2818
|
-
if (topic === 'readiness') {
|
|
2819
|
-
return watchReadiness({ target: '', code: 'stored' }, null, deliver);
|
|
2820
|
-
}
|
|
2821
|
-
if (topic === 'rooms') {
|
|
2822
|
-
unlistedRooms.add(deliver);
|
|
2823
|
-
emitKnownRooms();
|
|
2824
|
-
return () => {
|
|
2825
|
-
unlistedRooms.delete(deliver);
|
|
2826
|
-
roomListeners.delete(deliver);
|
|
2827
|
-
if (roomListeners.size === 0)
|
|
2828
|
-
roomsShown = null;
|
|
2829
|
-
};
|
|
2830
|
-
}
|
|
2831
|
-
if (topic === 'connection-refusal') {
|
|
2832
|
-
refusalListeners.add(deliver);
|
|
2833
|
-
deliver(refusal);
|
|
2834
|
-
return () => {
|
|
2835
|
-
refusalListeners.delete(deliver);
|
|
2836
|
-
};
|
|
2837
|
-
}
|
|
2838
|
-
if (topic === 'storage-loss') {
|
|
2839
|
-
storageLossListeners.add(deliver);
|
|
2840
|
-
deliver(storageLoss);
|
|
2841
|
-
return () => {
|
|
2842
|
-
storageLossListeners.delete(deliver);
|
|
2843
|
-
};
|
|
2844
|
-
}
|
|
2845
|
-
if (topic === 'sync-status') {
|
|
2846
|
-
statusListeners.add(deliver);
|
|
2847
|
-
deliver(shown);
|
|
2848
|
-
return () => {
|
|
2849
|
-
statusListeners.delete(deliver);
|
|
2850
|
-
};
|
|
2851
|
-
}
|
|
2852
|
-
if (topic === 'service-mode') {
|
|
2853
|
-
serviceModeListeners.add(deliver);
|
|
2854
|
-
deliver(serviceMode);
|
|
2855
|
-
return () => {
|
|
2856
|
-
serviceModeListeners.delete(deliver);
|
|
2857
|
-
};
|
|
2858
|
-
}
|
|
2859
|
-
if (topic === 'member') {
|
|
2860
|
-
memberListeners.add(deliver);
|
|
2861
|
-
void openLocal()
|
|
2862
|
-
.then((local) => local.facts())
|
|
2863
|
-
.then(({ member }) => {
|
|
2864
|
-
if (memberListeners.has(deliver))
|
|
2865
|
-
deliver(member);
|
|
2866
|
-
}, () => { });
|
|
2867
|
-
return () => {
|
|
2868
|
-
memberListeners.delete(deliver);
|
|
2869
|
-
};
|
|
2870
|
-
}
|
|
2871
|
-
if (topic === 'authority-changes') {
|
|
2872
|
-
authorityChangeListeners.add(deliver);
|
|
2873
|
-
deliver(authorityChanges);
|
|
2874
|
-
return () => {
|
|
2875
|
-
authorityChangeListeners.delete(deliver);
|
|
2876
|
-
};
|
|
2877
|
-
}
|
|
2878
|
-
if (topic === 'queue') {
|
|
2879
|
-
const queue = { locator: input[0], notify: deliver };
|
|
2880
|
-
queueSubscriptions.add(queue);
|
|
2881
|
-
void publication(async () => {
|
|
2882
|
-
if (!queueSubscriptions.has(queue))
|
|
2883
|
-
return;
|
|
2884
|
-
const entries = await entriesOf(queue.locator);
|
|
2885
|
-
if (queueSubscriptions.has(queue))
|
|
2886
|
-
queue.notify(entries);
|
|
2887
|
-
});
|
|
2888
|
-
return () => {
|
|
2889
|
-
queueSubscriptions.delete(queue);
|
|
2890
|
-
};
|
|
2891
|
-
}
|
|
2892
|
-
return watchPlanned(planned(input[0], null), deliver, fail);
|
|
2893
|
-
};
|
|
2894
|
-
const md = {
|
|
2895
|
-
loaded() {
|
|
2896
|
-
return openLocal().then(() => undefined);
|
|
2897
|
-
},
|
|
2898
|
-
rooms() {
|
|
2899
|
-
if (closed)
|
|
2900
|
-
return Promise.resolve([]);
|
|
2901
|
-
return new Promise((settle) => {
|
|
2902
|
-
const answer = (value) => {
|
|
2903
|
-
unlistedRooms.delete(answer);
|
|
2904
|
-
roomListeners.delete(answer);
|
|
2905
|
-
settle(value);
|
|
2906
|
-
};
|
|
2907
|
-
unlistedRooms.add(answer);
|
|
2908
|
-
emitKnownRooms();
|
|
2909
|
-
});
|
|
2910
|
-
},
|
|
2911
|
-
query: ((input) => planned(input, null).then(rowsQuery)),
|
|
2912
|
-
search: (input) => searched(input, null),
|
|
2913
|
-
async types(locator) {
|
|
2914
|
-
const held = (await storedRooms([locator])).get(locator);
|
|
2915
|
-
const room = isFenced(locator) ? undefined : declarationOf(locator, held)?.room;
|
|
2916
|
-
return {
|
|
2917
|
-
collection: modelTypes(isFenced(locator) ? NO_MODEL : modelIn(locator, held)),
|
|
2918
|
-
typedSelection: (room?.typedSelection ?? null) === null ? null : modelTypes(builtIn),
|
|
2919
|
-
};
|
|
2920
|
-
},
|
|
2921
|
-
roomDraft: roomDraftSurface(drafting),
|
|
2922
|
-
conversation: conversations.surface,
|
|
2923
|
-
chatDraft: conversations.chatDraft,
|
|
2924
|
-
presentation: presentationSurface(drafting),
|
|
2925
|
-
save(room, named, writtenBy) {
|
|
2926
|
-
return exclusive(() => saveRoomDrafts(saver, room, named, writtenBy));
|
|
2927
|
-
},
|
|
2928
|
-
saveParts(room, parts, writtenBy) {
|
|
2929
|
-
return exclusive(() => saveParts(saver, room, parts, writtenBy));
|
|
2930
|
-
},
|
|
2931
|
-
history(locator, path, query) {
|
|
2932
|
-
return readHistory(locator, path, query);
|
|
2933
|
-
},
|
|
2934
|
-
resolve(room, path, destination) {
|
|
2935
|
-
return resolveReference(room, path, destination);
|
|
2936
|
-
},
|
|
2937
|
-
binaryFile: binaryFileSurface(saver),
|
|
2938
|
-
async call(name, input) {
|
|
2939
|
-
await ready;
|
|
2940
|
-
const lockedNow = await lockedRooms();
|
|
2941
|
-
const room = (await calledRooms(name, input)).find((one) => lockedNow.includes(one));
|
|
2942
|
-
return room === undefined ? recoveredAction(name, input) : lockedRoom(room);
|
|
2943
|
-
},
|
|
2944
|
-
async reached(locator, roomVersion, path) {
|
|
2945
|
-
return path === undefined
|
|
2946
|
-
? reachedBy(installer, locator, roomVersion)
|
|
2947
|
-
: writtenAfter(installer, locator, roomVersion, path);
|
|
2948
|
-
},
|
|
2949
|
-
log() {
|
|
2950
|
-
return journal.export();
|
|
2951
|
-
},
|
|
2952
|
-
cleanUp,
|
|
2953
|
-
unsent: () => database.ask({ kind: 'authored.unsent' }),
|
|
2954
|
-
reset: () => options.shell.reset(),
|
|
2955
|
-
async readAgain() {
|
|
2956
|
-
lastAnswers.again();
|
|
2957
|
-
},
|
|
2958
|
-
persistence() {
|
|
2959
|
-
return persistence === 'granted';
|
|
2960
|
-
},
|
|
2961
|
-
async retry() {
|
|
2962
|
-
journal.record('retry');
|
|
2963
|
-
unpreparable.clear();
|
|
2964
|
-
queueReadiness();
|
|
2965
|
-
if (full)
|
|
2966
|
-
await probed();
|
|
2967
|
-
},
|
|
2968
|
-
observe: observing(watch),
|
|
2969
|
-
async reauthorize() {
|
|
2970
|
-
if (refusal === null || !refusal.reauthorizationOffered)
|
|
2971
|
-
return;
|
|
2972
|
-
journal.record('connection reauthorize');
|
|
2973
|
-
await Promise.all([...lanes.values()]
|
|
2974
|
-
.filter((held) => held.generation === socket.generation())
|
|
2975
|
-
.map((held) => held.chain));
|
|
2976
|
-
await store.written();
|
|
2977
|
-
authorization = undefined;
|
|
2978
|
-
discoveryPaused = false;
|
|
2979
|
-
if (options.adapter.roomStream === undefined)
|
|
2980
|
-
await sync('unstreamed', null);
|
|
2981
|
-
else
|
|
2982
|
-
await task(OPENING_LANE, ensureAuthorization);
|
|
2983
|
-
},
|
|
2984
|
-
};
|
|
2985
|
-
const reachable = {
|
|
2986
|
-
md,
|
|
2987
|
-
watch,
|
|
2988
|
-
plan: planned,
|
|
2989
|
-
query: (plan) => rowsQuery(plan),
|
|
2990
|
-
watchPlan,
|
|
2991
|
-
search: searched,
|
|
2992
|
-
watchReadiness,
|
|
2993
|
-
locked,
|
|
2994
|
-
lockedRooms,
|
|
2995
|
-
watchLocks,
|
|
2996
|
-
located,
|
|
2997
|
-
room: async (locator) => shownRooms(await listingNow()).find((one) => one.locator === locator),
|
|
2998
|
-
};
|
|
2999
|
-
return {
|
|
3000
|
-
md,
|
|
3001
|
-
seed: (entry) => keepEntrySeed(store, entry),
|
|
3002
|
-
stop,
|
|
3003
|
-
watch,
|
|
3004
|
-
persisted(answer) {
|
|
3005
|
-
if (answer === persistence)
|
|
3006
|
-
return;
|
|
3007
|
-
persistence = answer;
|
|
3008
|
-
journal.record(`persistence ${answer}`);
|
|
3009
|
-
queueReadiness();
|
|
3010
|
-
},
|
|
3011
|
-
within: (reach, port) => {
|
|
3012
|
-
if (full)
|
|
3013
|
-
void probed();
|
|
3014
|
-
return narrowed(reachable, reach, port);
|
|
3015
|
-
},
|
|
91
|
+
stream: socket.conversations,
|
|
92
|
+
publish: store.changed,
|
|
93
|
+
online: socket.reached,
|
|
94
|
+
full: store.space.full,
|
|
95
|
+
closed: socket.closed,
|
|
96
|
+
unanswered: () => resendLater(runtime),
|
|
97
|
+
heard: (operation, lost) => socket.lanes.settle(operation, lost),
|
|
98
|
+
recovery: recovery.scope(),
|
|
99
|
+
retries,
|
|
100
|
+
}),
|
|
101
|
+
liveQueries: createLiveQueries((plan, cover) => answeredPlan(runtime, plan, cover), store.covering),
|
|
102
|
+
figures: createFigures({
|
|
103
|
+
cover: () => figuresCover(runtime),
|
|
104
|
+
rooms: (rooms, since) => roomTallies(runtime, rooms, since),
|
|
105
|
+
conversations: (conversations, since) => store.ask({ kind: 'conversation.tallies', conversations, since }),
|
|
106
|
+
}),
|
|
107
|
+
lastAnswers: readAnswers({
|
|
108
|
+
database: store,
|
|
109
|
+
build: options.build ?? '',
|
|
110
|
+
unkept: adapter.unkeptReads ?? [],
|
|
111
|
+
leading: adapter.leadingReads ?? [],
|
|
112
|
+
recovery: recovery.scope(),
|
|
113
|
+
retries,
|
|
114
|
+
call: (name, input, attempt) => called(runtime, name, input, attempt),
|
|
115
|
+
online: socket.reached,
|
|
116
|
+
streaming: socket.isOpen,
|
|
117
|
+
closed: socket.closed,
|
|
118
|
+
full: store.space.full,
|
|
119
|
+
filled: () => filled(runtime),
|
|
120
|
+
}),
|
|
121
|
+
discover: () => discover(runtime),
|
|
122
|
+
adopt: (unstored) => adopt(runtime, unstored),
|
|
3016
123
|
};
|
|
124
|
+
start(runtime, options.signal, options.storeOnNeed === true);
|
|
125
|
+
return composedRuntime(runtime);
|
|
3017
126
|
}
|