@dxos/echo-pipeline 0.8.1 → 0.8.2-main.10c050d
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/dist/lib/browser/{chunk-32WDI3LB.mjs → chunk-3XSXS5EX.mjs} +15 -21
- package/dist/lib/browser/chunk-3XSXS5EX.mjs.map +7 -0
- package/dist/lib/browser/chunk-CGS2ULMK.mjs +11 -0
- package/dist/lib/browser/chunk-CGS2ULMK.mjs.map +7 -0
- package/dist/lib/browser/chunk-TQJTKNMS.mjs +126 -0
- package/dist/lib/browser/chunk-TQJTKNMS.mjs.map +7 -0
- package/dist/lib/browser/filter/index.mjs +11 -0
- package/dist/lib/browser/filter/index.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +1380 -516
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +202 -22
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node/chunk-HOPOFWAL.cjs +147 -0
- package/dist/lib/node/chunk-HOPOFWAL.cjs.map +7 -0
- package/dist/lib/node/chunk-Q7SFCCGT.cjs +33 -0
- package/dist/lib/node/chunk-Q7SFCCGT.cjs.map +7 -0
- package/dist/lib/node/{chunk-TC2PRBEU.cjs → chunk-SG2PL5RH.cjs} +18 -24
- package/dist/lib/node/chunk-SG2PL5RH.cjs.map +7 -0
- package/dist/lib/node/filter/index.cjs +32 -0
- package/dist/lib/node/filter/index.cjs.map +7 -0
- package/dist/lib/node/index.cjs +1381 -525
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +207 -31
- package/dist/lib/node/testing/index.cjs.map +4 -4
- package/dist/lib/node-esm/{chunk-UKOLB3LW.mjs → chunk-3BZP75TJ.mjs} +15 -21
- package/dist/lib/node-esm/chunk-3BZP75TJ.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-RVK35BS7.mjs +126 -0
- package/dist/lib/node-esm/chunk-RVK35BS7.mjs.map +7 -0
- package/dist/lib/node-esm/filter/index.mjs +11 -0
- package/dist/lib/node-esm/filter/index.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +1380 -516
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +202 -22
- package/dist/lib/node-esm/testing/index.mjs.map +4 -4
- package/dist/types/src/automerge/automerge-host.d.ts +6 -4
- package/dist/types/src/automerge/automerge-host.d.ts.map +1 -1
- package/dist/types/src/automerge/collection-synchronizer.d.ts +1 -1
- package/dist/types/src/automerge/collection-synchronizer.d.ts.map +1 -1
- package/dist/types/src/automerge/echo-data-monitor.d.ts +6 -6
- package/dist/types/src/automerge/echo-data-monitor.d.ts.map +1 -1
- package/dist/types/src/automerge/echo-network-adapter.d.ts +4 -1
- package/dist/types/src/automerge/echo-network-adapter.d.ts.map +1 -1
- package/dist/types/src/automerge/heads-store.d.ts +2 -2
- package/dist/types/src/automerge/heads-store.d.ts.map +1 -1
- package/dist/types/src/automerge/leveldb-storage-adapter.d.ts +1 -1
- package/dist/types/src/automerge/leveldb-storage-adapter.d.ts.map +1 -1
- package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts.map +1 -1
- package/dist/types/src/automerge/mesh-echo-replicator.d.ts.map +1 -1
- package/dist/types/src/automerge/network-protocol.d.ts +1 -1
- package/dist/types/src/automerge/network-protocol.d.ts.map +1 -1
- package/dist/types/src/automerge/space-collection.d.ts +1 -1
- package/dist/types/src/automerge/space-collection.d.ts.map +1 -1
- package/dist/types/src/common/feeds.d.ts.map +1 -1
- package/dist/types/src/common/space-id.d.ts.map +1 -1
- package/dist/types/src/db-host/automerge-metrics.d.ts +1 -1
- package/dist/types/src/db-host/automerge-metrics.d.ts.map +1 -1
- package/dist/types/src/db-host/data-service.d.ts.map +1 -1
- package/dist/types/src/db-host/database-root.d.ts +7 -7
- package/dist/types/src/db-host/database-root.d.ts.map +1 -1
- package/dist/types/src/db-host/documents-iterator.d.ts +1 -1
- package/dist/types/src/db-host/documents-iterator.d.ts.map +1 -1
- package/dist/types/src/db-host/documents-synchronizer.d.ts +4 -4
- package/dist/types/src/db-host/documents-synchronizer.d.ts.map +1 -1
- package/dist/types/src/db-host/echo-host.d.ts +13 -2
- package/dist/types/src/db-host/echo-host.d.ts.map +1 -1
- package/dist/types/src/db-host/index.d.ts +0 -1
- package/dist/types/src/db-host/index.d.ts.map +1 -1
- package/dist/types/src/db-host/query-service.d.ts +2 -0
- package/dist/types/src/db-host/query-service.d.ts.map +1 -1
- package/dist/types/src/db-host/space-state-manager.d.ts +4 -3
- package/dist/types/src/db-host/space-state-manager.d.ts.map +1 -1
- package/dist/types/src/edge/echo-edge-replicator.d.ts.map +1 -1
- package/dist/types/src/edge/inflight-request-limiter.d.ts.map +1 -1
- package/dist/types/src/filter/filter-match.d.ts +13 -0
- package/dist/types/src/filter/filter-match.d.ts.map +1 -0
- package/dist/types/src/filter/filter-match.test.d.ts +2 -0
- package/dist/types/src/filter/filter-match.test.d.ts.map +1 -0
- package/dist/types/src/filter/index.d.ts +2 -0
- package/dist/types/src/filter/index.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/metadata/metadata-store.d.ts.map +1 -1
- package/dist/types/src/pipeline/message-selector.d.ts.map +1 -1
- package/dist/types/src/pipeline/pipeline.d.ts.map +1 -1
- package/dist/types/src/pipeline/timeframe-clock.d.ts.map +1 -1
- package/dist/types/src/query/errors.d.ts +23 -0
- package/dist/types/src/query/errors.d.ts.map +1 -0
- package/dist/types/src/query/index.d.ts +5 -0
- package/dist/types/src/query/index.d.ts.map +1 -0
- package/dist/types/src/query/plan.d.ts +132 -0
- package/dist/types/src/query/plan.d.ts.map +1 -0
- package/dist/types/src/query/query-executor.d.ts +83 -0
- package/dist/types/src/query/query-executor.d.ts.map +1 -0
- package/dist/types/src/query/query-planner.d.ts +33 -0
- package/dist/types/src/query/query-planner.d.ts.map +1 -0
- package/dist/types/src/query/query-planner.test.d.ts +2 -0
- package/dist/types/src/query/query-planner.test.d.ts.map +1 -0
- package/dist/types/src/space/admission-discovery-extension.d.ts.map +1 -1
- package/dist/types/src/space/control-pipeline.d.ts.map +1 -1
- package/dist/types/src/space/space-manager.d.ts.map +1 -1
- package/dist/types/src/space/space-protocol.d.ts.map +1 -1
- package/dist/types/src/space/space.d.ts.map +1 -1
- package/dist/types/src/testing/change-metadata.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +2 -0
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/test-agent-builder.d.ts.map +1 -1
- package/dist/types/src/testing/test-data.d.ts +18 -0
- package/dist/types/src/testing/test-data.d.ts.map +1 -0
- package/dist/types/src/testing/test-network-adapter.d.ts +3 -2
- package/dist/types/src/testing/test-network-adapter.d.ts.map +1 -1
- package/dist/types/src/testing/test-schema.d.ts +39 -0
- package/dist/types/src/testing/test-schema.d.ts.map +1 -0
- package/dist/types/src/util.d.ts +2 -2
- package/dist/types/src/util.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +43 -34
- package/src/automerge/automerge-host.test.ts +7 -7
- package/src/automerge/automerge-host.ts +58 -60
- package/src/automerge/automerge-repo.test.ts +65 -65
- package/src/automerge/collection-synchronizer.test.ts +1 -1
- package/src/automerge/collection-synchronizer.ts +11 -10
- package/src/automerge/echo-data-monitor.ts +21 -20
- package/src/automerge/echo-network-adapter.test.ts +1 -1
- package/src/automerge/echo-network-adapter.ts +25 -18
- package/src/automerge/heads-store.ts +4 -3
- package/src/automerge/leveldb-storage-adapter.ts +1 -1
- package/src/automerge/mesh-echo-replicator-connection.ts +6 -5
- package/src/automerge/mesh-echo-replicator.ts +2 -2
- package/src/automerge/network-protocol.ts +2 -1
- package/src/automerge/space-collection.ts +2 -1
- package/src/db-host/automerge-metrics.ts +2 -1
- package/src/db-host/data-service.ts +4 -3
- package/src/db-host/database-root.ts +17 -22
- package/src/db-host/documents-iterator.ts +9 -8
- package/src/db-host/documents-synchronizer.test.ts +2 -2
- package/src/db-host/documents-synchronizer.ts +20 -18
- package/src/db-host/echo-host.ts +44 -15
- package/src/db-host/index.ts +0 -1
- package/src/db-host/query-service.ts +43 -37
- package/src/db-host/space-state-manager.ts +14 -4
- package/src/edge/echo-edge-replicator.test.ts +3 -3
- package/src/edge/echo-edge-replicator.ts +9 -8
- package/src/edge/inflight-request-limiter.ts +4 -4
- package/src/filter/filter-match.test.ts +101 -0
- package/src/filter/filter-match.ts +174 -0
- package/src/filter/index.ts +5 -0
- package/src/index.ts +1 -0
- package/src/metadata/metadata-store.ts +13 -13
- package/src/pipeline/pipeline-stress.test.ts +9 -9
- package/src/pipeline/pipeline.ts +13 -13
- package/src/pipeline/timeframe-clock.ts +5 -5
- package/src/query/errors.ts +7 -0
- package/src/query/index.ts +8 -0
- package/src/query/plan.ts +179 -0
- package/src/query/query-executor.ts +648 -0
- package/src/query/query-planner.test.ts +613 -0
- package/src/query/query-planner.ts +470 -0
- package/src/space/admission-discovery-extension.ts +2 -2
- package/src/space/control-pipeline.ts +8 -8
- package/src/space/space-manager.ts +5 -4
- package/src/space/space-protocol.browser.test.ts +1 -0
- package/src/space/space-protocol.test.ts +1 -0
- package/src/space/space-protocol.ts +4 -4
- package/src/space/space.ts +5 -5
- package/src/testing/index.ts +2 -0
- package/src/testing/test-agent-builder.ts +6 -6
- package/src/testing/test-data.ts +127 -0
- package/src/testing/test-network-adapter.ts +15 -12
- package/src/testing/test-replicator.ts +2 -2
- package/src/testing/test-schema.ts +53 -0
- package/src/util.ts +7 -3
- package/dist/lib/browser/chunk-32WDI3LB.mjs.map +0 -7
- package/dist/lib/node/chunk-TC2PRBEU.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-UKOLB3LW.mjs.map +0 -7
- package/dist/types/src/db-host/query-state.d.ts +0 -41
- package/dist/types/src/db-host/query-state.d.ts.map +0 -1
- package/src/db-host/query-state.ts +0 -217
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import { getHeads } from '@automerge/automerge';
|
|
6
|
+
import type { DocumentId, Heads } from '@automerge/automerge-repo';
|
|
5
7
|
import { onTestFinished, describe, expect, test } from 'vitest';
|
|
6
8
|
|
|
7
|
-
import { getHeads } from '@dxos/automerge/automerge';
|
|
8
|
-
import type { DocumentId, Heads } from '@dxos/automerge/automerge-repo';
|
|
9
9
|
import { IndexMetadataStore } from '@dxos/indexing';
|
|
10
10
|
import type { LevelDB } from '@dxos/kv-store';
|
|
11
11
|
import { createTestLevel } from '@dxos/kv-store/testing';
|
|
@@ -24,7 +24,7 @@ describe('AutomergeHost', () => {
|
|
|
24
24
|
doc.text = 'Hello world';
|
|
25
25
|
});
|
|
26
26
|
await host.repo.flush();
|
|
27
|
-
expect(handle.
|
|
27
|
+
expect(handle.doc()!.text).toEqual('Hello world');
|
|
28
28
|
});
|
|
29
29
|
|
|
30
30
|
test('changes are preserved in storage', async () => {
|
|
@@ -41,9 +41,9 @@ describe('AutomergeHost', () => {
|
|
|
41
41
|
await host.close();
|
|
42
42
|
|
|
43
43
|
const host2 = await setupAutomergeHost({ level });
|
|
44
|
-
const handle2 = host2.repo.find<any>(url);
|
|
44
|
+
const handle2 = await host2.repo.find<any>(url);
|
|
45
45
|
await handle2.whenReady();
|
|
46
|
-
expect(handle2.
|
|
46
|
+
expect(handle2.doc()!.text).toEqual('Hello world');
|
|
47
47
|
await host2.repo.flush();
|
|
48
48
|
});
|
|
49
49
|
|
|
@@ -52,7 +52,7 @@ describe('AutomergeHost', () => {
|
|
|
52
52
|
|
|
53
53
|
const host = await setupAutomergeHost({ level });
|
|
54
54
|
const handle = host.createDoc({ text: 'Hello world' });
|
|
55
|
-
const expectedHeads = getHeads(handle.
|
|
55
|
+
const expectedHeads = getHeads(handle.doc()!);
|
|
56
56
|
await host.flush();
|
|
57
57
|
|
|
58
58
|
expect(await host.getHeads([handle.documentId])).toEqual([expectedHeads]);
|
|
@@ -69,7 +69,7 @@ describe('AutomergeHost', () => {
|
|
|
69
69
|
|
|
70
70
|
const host = await setupAutomergeHost({ level });
|
|
71
71
|
const handles = range(2, () => host.createDoc({ text: 'Hello world' }));
|
|
72
|
-
const expectedHeads: (Heads | undefined)[] = handles.map((handle) => getHeads(handle.
|
|
72
|
+
const expectedHeads: (Heads | undefined)[] = handles.map((handle) => getHeads(handle.doc()!));
|
|
73
73
|
await host.flush();
|
|
74
74
|
|
|
75
75
|
const ids = handles.map((handle) => handle.documentId);
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Event, asyncTimeout } from '@dxos/async';
|
|
6
5
|
import {
|
|
7
6
|
getBackend,
|
|
8
7
|
getHeads,
|
|
@@ -11,7 +10,7 @@ import {
|
|
|
11
10
|
save,
|
|
12
11
|
type Doc,
|
|
13
12
|
type Heads,
|
|
14
|
-
} from '@
|
|
13
|
+
} from '@automerge/automerge';
|
|
15
14
|
import {
|
|
16
15
|
type DocHandleChangePayload,
|
|
17
16
|
Repo,
|
|
@@ -24,9 +23,12 @@ import {
|
|
|
24
23
|
type StorageAdapterInterface,
|
|
25
24
|
type StorageKey,
|
|
26
25
|
interpretAsDocumentId,
|
|
27
|
-
|
|
26
|
+
type HandleState,
|
|
27
|
+
} from '@automerge/automerge-repo';
|
|
28
|
+
|
|
29
|
+
import { Event, asyncTimeout } from '@dxos/async';
|
|
28
30
|
import { Context, Resource, cancelWithContext, type Lifecycle } from '@dxos/context';
|
|
29
|
-
import {
|
|
31
|
+
import { DatabaseDirectory, type CollectionId } from '@dxos/echo-protocol';
|
|
30
32
|
import { type IndexMetadataStore } from '@dxos/indexing';
|
|
31
33
|
import { invariant } from '@dxos/invariant';
|
|
32
34
|
import { PublicKey } from '@dxos/keys';
|
|
@@ -72,6 +74,10 @@ export type CreateDocOptions = {
|
|
|
72
74
|
preserveHistory?: boolean;
|
|
73
75
|
};
|
|
74
76
|
|
|
77
|
+
export const FIND_PARAMS = {
|
|
78
|
+
allowableStates: ['ready', 'requesting'] satisfies HandleState[],
|
|
79
|
+
};
|
|
80
|
+
|
|
75
81
|
/**
|
|
76
82
|
* Abstracts over the AutomergeRepo.
|
|
77
83
|
*/
|
|
@@ -129,7 +135,7 @@ export class AutomergeHost extends Resource {
|
|
|
129
135
|
this._getSpaceKeyByRootDocumentId = getSpaceKeyByRootDocumentId;
|
|
130
136
|
}
|
|
131
137
|
|
|
132
|
-
protected override async _open() {
|
|
138
|
+
protected override async _open(): Promise<void> {
|
|
133
139
|
this._peerId = `host-${this._peerIdProvider?.() ?? PublicKey.random().toHex()}` as PeerId;
|
|
134
140
|
|
|
135
141
|
await this._storage.open?.();
|
|
@@ -175,7 +181,7 @@ export class AutomergeHost extends Resource {
|
|
|
175
181
|
await this._echoNetworkAdapter.whenConnected();
|
|
176
182
|
}
|
|
177
183
|
|
|
178
|
-
protected override async _close() {
|
|
184
|
+
protected override async _close(): Promise<void> {
|
|
179
185
|
await this._collectionSynchronizer.close();
|
|
180
186
|
await this._storage.close?.();
|
|
181
187
|
await this._echoNetworkAdapter.close();
|
|
@@ -197,11 +203,11 @@ export class AutomergeHost extends Resource {
|
|
|
197
203
|
return Object.keys(this._repo.handles).length;
|
|
198
204
|
}
|
|
199
205
|
|
|
200
|
-
async addReplicator(replicator: EchoReplicator) {
|
|
206
|
+
async addReplicator(replicator: EchoReplicator): Promise<void> {
|
|
201
207
|
await this._echoNetworkAdapter.addReplicator(replicator);
|
|
202
208
|
}
|
|
203
209
|
|
|
204
|
-
async removeReplicator(replicator: EchoReplicator) {
|
|
210
|
+
async removeReplicator(replicator: EchoReplicator): Promise<void> {
|
|
205
211
|
await this._echoNetworkAdapter.removeReplicator(replicator);
|
|
206
212
|
}
|
|
207
213
|
|
|
@@ -215,7 +221,7 @@ export class AutomergeHost extends Resource {
|
|
|
215
221
|
handle = this._repo.handles[documentId as DocumentId];
|
|
216
222
|
}
|
|
217
223
|
if (!handle) {
|
|
218
|
-
handle = this._repo.find(documentId as DocumentId);
|
|
224
|
+
handle = await this._repo.find(documentId as DocumentId, FIND_PARAMS);
|
|
219
225
|
}
|
|
220
226
|
|
|
221
227
|
// `whenReady` creates a timeout so we guard it with an if to skip it if the handle is already ready.
|
|
@@ -279,30 +285,28 @@ export class AutomergeHost extends Resource {
|
|
|
279
285
|
if (headsToWait.length > 0) {
|
|
280
286
|
await Promise.all(
|
|
281
287
|
headsToWait.map(async (entry, index) => {
|
|
282
|
-
const handle = await this.loadDoc<
|
|
288
|
+
const handle = await this.loadDoc<DatabaseDirectory>(Context.default(), entry.documentId as DocumentId);
|
|
283
289
|
await waitForHeads(handle, entry.heads!);
|
|
284
290
|
}),
|
|
285
291
|
);
|
|
286
292
|
}
|
|
287
293
|
|
|
288
294
|
// Flush to disk handles loaded to memory also so that the indexer can pick up the changes.
|
|
289
|
-
await this._repo.flush(
|
|
295
|
+
await this._repo.flush(
|
|
296
|
+
documentIds.filter((documentId) => this._repo.handles[documentId] && this._repo.handles[documentId].isReady()),
|
|
297
|
+
);
|
|
290
298
|
}
|
|
291
299
|
|
|
292
|
-
async reIndexHeads(documentIds: DocumentId[]) {
|
|
300
|
+
async reIndexHeads(documentIds: DocumentId[]): Promise<void> {
|
|
293
301
|
for (const documentId of documentIds) {
|
|
294
302
|
log.info('re-indexing heads for document', { documentId });
|
|
295
|
-
const handle = this._repo.find(documentId);
|
|
296
|
-
|
|
297
|
-
if (handle.inState(['requesting'])) {
|
|
303
|
+
const handle = await this._repo.find(documentId, FIND_PARAMS);
|
|
304
|
+
if (!handle.isReady()) {
|
|
298
305
|
log.warn('document is not available locally, skipping', { documentId });
|
|
299
306
|
continue; // Handle not available locally.
|
|
300
307
|
}
|
|
301
308
|
|
|
302
|
-
const
|
|
303
|
-
invariant(doc);
|
|
304
|
-
|
|
305
|
-
const heads = getHeads(doc);
|
|
309
|
+
const heads = handle.heads();
|
|
306
310
|
const batch = this._db.batch();
|
|
307
311
|
this._headsStore.setHeads(documentId, heads, batch);
|
|
308
312
|
await batch.write();
|
|
@@ -331,12 +335,12 @@ export class AutomergeHost extends Resource {
|
|
|
331
335
|
return false;
|
|
332
336
|
}
|
|
333
337
|
|
|
334
|
-
private async _beforeSave({ path, batch }: BeforeSaveParams) {
|
|
338
|
+
private async _beforeSave({ path, batch }: BeforeSaveParams): Promise<void> {
|
|
335
339
|
const handle = this._repo.handles[path[0] as DocumentId];
|
|
336
|
-
if (!handle) {
|
|
340
|
+
if (!handle || !handle.isReady()) {
|
|
337
341
|
return;
|
|
338
342
|
}
|
|
339
|
-
const doc = handle.
|
|
343
|
+
const doc = handle.doc();
|
|
340
344
|
if (!doc) {
|
|
341
345
|
return;
|
|
342
346
|
}
|
|
@@ -344,7 +348,7 @@ export class AutomergeHost extends Resource {
|
|
|
344
348
|
const heads = getHeads(doc);
|
|
345
349
|
this._headsStore.setHeads(handle.documentId, heads, batch);
|
|
346
350
|
|
|
347
|
-
const spaceKey =
|
|
351
|
+
const spaceKey = DatabaseDirectory.getSpaceKey(doc) ?? undefined;
|
|
348
352
|
const objectIds = Object.keys(doc.objects ?? {});
|
|
349
353
|
const encodedIds = objectIds.map((objectId) =>
|
|
350
354
|
objectPointerCodec.encode({ documentId: handle.documentId, objectId, spaceKey }),
|
|
@@ -365,11 +369,11 @@ export class AutomergeHost extends Resource {
|
|
|
365
369
|
/**
|
|
366
370
|
* Called by AutomergeStorageAdapter after levelDB batch commit.
|
|
367
371
|
*/
|
|
368
|
-
private async _afterSave(path: StorageKey) {
|
|
372
|
+
private async _afterSave(path: StorageKey): Promise<void> {
|
|
369
373
|
this._indexMetadataStore.notifyMarkedDirty();
|
|
370
374
|
|
|
371
375
|
const documentId = path[0] as DocumentId;
|
|
372
|
-
const document = this._repo.handles[documentId]?.
|
|
376
|
+
const document = this._repo.handles[documentId]?.doc();
|
|
373
377
|
if (document) {
|
|
374
378
|
const heads = getHeads(document);
|
|
375
379
|
this._onHeadsChanged(documentId, heads);
|
|
@@ -377,7 +381,7 @@ export class AutomergeHost extends Resource {
|
|
|
377
381
|
}
|
|
378
382
|
|
|
379
383
|
@trace.info({ depth: null })
|
|
380
|
-
private _automergePeers() {
|
|
384
|
+
private _automergePeers(): PeerId[] {
|
|
381
385
|
return this._repo.peers;
|
|
382
386
|
}
|
|
383
387
|
|
|
@@ -393,9 +397,12 @@ export class AutomergeHost extends Resource {
|
|
|
393
397
|
}
|
|
394
398
|
|
|
395
399
|
private async _getContainingSpaceForDocument(documentId: string): Promise<PublicKey | null> {
|
|
396
|
-
const
|
|
397
|
-
if (
|
|
398
|
-
|
|
400
|
+
const handle = this._repo.handles[documentId as any];
|
|
401
|
+
if (handle.state === 'loading') {
|
|
402
|
+
await handle.whenReady();
|
|
403
|
+
}
|
|
404
|
+
if (handle && handle.isReady() && handle.doc()) {
|
|
405
|
+
const spaceKeyHex = DatabaseDirectory.getSpaceKey(handle.doc());
|
|
399
406
|
if (spaceKeyHex) {
|
|
400
407
|
return PublicKey.from(spaceKeyHex);
|
|
401
408
|
}
|
|
@@ -420,9 +427,10 @@ export class AutomergeHost extends Resource {
|
|
|
420
427
|
async flush({ documentIds }: FlushRequest = {}): Promise<void> {
|
|
421
428
|
// Note: Sync protocol for client and services ensures that all handles should have all changes.
|
|
422
429
|
|
|
423
|
-
const loadedDocuments = documentIds?.filter(
|
|
424
|
-
|
|
425
|
-
|
|
430
|
+
const loadedDocuments = documentIds?.filter((documentId): documentId is DocumentId => {
|
|
431
|
+
const handle = this._repo.handles[documentId as DocumentId];
|
|
432
|
+
return handle && handle.isReady();
|
|
433
|
+
});
|
|
426
434
|
await this._repo.flush(loadedDocuments);
|
|
427
435
|
}
|
|
428
436
|
|
|
@@ -431,9 +439,9 @@ export class AutomergeHost extends Resource {
|
|
|
431
439
|
const storeRequestIds: DocumentId[] = [];
|
|
432
440
|
const storeResultIndices: number[] = [];
|
|
433
441
|
for (const documentId of documentIds) {
|
|
434
|
-
const
|
|
435
|
-
if (doc) {
|
|
436
|
-
result.push(getHeads(doc));
|
|
442
|
+
const handle = this._repo.handles[documentId];
|
|
443
|
+
if (handle && handle.isReady() && handle.doc()) {
|
|
444
|
+
result.push(getHeads(handle.doc()!));
|
|
437
445
|
} else {
|
|
438
446
|
storeRequestIds.push(documentId);
|
|
439
447
|
storeResultIndices.push(result.length);
|
|
@@ -461,7 +469,7 @@ export class AutomergeHost extends Resource {
|
|
|
461
469
|
return this._collectionSynchronizer.getRemoteCollectionStates(collectionId);
|
|
462
470
|
}
|
|
463
471
|
|
|
464
|
-
refreshCollection(collectionId: string) {
|
|
472
|
+
refreshCollection(collectionId: string): void {
|
|
465
473
|
this._collectionSynchronizer.refreshCollection(collectionId);
|
|
466
474
|
}
|
|
467
475
|
|
|
@@ -495,7 +503,7 @@ export class AutomergeHost extends Resource {
|
|
|
495
503
|
/**
|
|
496
504
|
* Update the local collection state based on the locally stored document heads.
|
|
497
505
|
*/
|
|
498
|
-
async updateLocalCollectionState(collectionId: string, documentIds: DocumentId[]) {
|
|
506
|
+
async updateLocalCollectionState(collectionId: string, documentIds: DocumentId[]): Promise<void> {
|
|
499
507
|
const heads = await this.getHeads(documentIds);
|
|
500
508
|
const documents: Record<DocumentId, Heads> = Object.fromEntries(
|
|
501
509
|
heads.map((heads, index) => [documentIds[index], heads ?? []]),
|
|
@@ -503,35 +511,35 @@ export class AutomergeHost extends Resource {
|
|
|
503
511
|
this._collectionSynchronizer.setLocalCollectionState(collectionId, { documents });
|
|
504
512
|
}
|
|
505
513
|
|
|
506
|
-
async clearLocalCollectionState(collectionId: string) {
|
|
514
|
+
async clearLocalCollectionState(collectionId: string): Promise<void> {
|
|
507
515
|
this._collectionSynchronizer.clearLocalCollectionState(collectionId);
|
|
508
516
|
}
|
|
509
517
|
|
|
510
|
-
private _onCollectionStateQueried(collectionId: string, peerId: PeerId) {
|
|
518
|
+
private _onCollectionStateQueried(collectionId: string, peerId: PeerId): void {
|
|
511
519
|
this._collectionSynchronizer.onCollectionStateQueried(collectionId, peerId);
|
|
512
520
|
}
|
|
513
521
|
|
|
514
|
-
private _onCollectionStateReceived(collectionId: string, peerId: PeerId, state: unknown) {
|
|
522
|
+
private _onCollectionStateReceived(collectionId: string, peerId: PeerId, state: unknown): void {
|
|
515
523
|
this._collectionSynchronizer.onRemoteStateReceived(collectionId, peerId, decodeCollectionState(state));
|
|
516
524
|
}
|
|
517
525
|
|
|
518
|
-
private _queryCollectionState(collectionId: string, peerId: PeerId) {
|
|
526
|
+
private _queryCollectionState(collectionId: string, peerId: PeerId): void {
|
|
519
527
|
this._echoNetworkAdapter.queryCollectionState(collectionId, peerId);
|
|
520
528
|
}
|
|
521
529
|
|
|
522
|
-
private _sendCollectionState(collectionId: string, peerId: PeerId, state: CollectionState) {
|
|
530
|
+
private _sendCollectionState(collectionId: string, peerId: PeerId, state: CollectionState): void {
|
|
523
531
|
this._echoNetworkAdapter.sendCollectionState(collectionId, peerId, encodeCollectionState(state));
|
|
524
532
|
}
|
|
525
533
|
|
|
526
|
-
private _onPeerConnected(peerId: PeerId) {
|
|
534
|
+
private _onPeerConnected(peerId: PeerId): void {
|
|
527
535
|
this._collectionSynchronizer.onConnectionOpen(peerId);
|
|
528
536
|
}
|
|
529
537
|
|
|
530
|
-
private _onPeerDisconnected(peerId: PeerId) {
|
|
538
|
+
private _onPeerDisconnected(peerId: PeerId): void {
|
|
531
539
|
this._collectionSynchronizer.onConnectionClosed(peerId);
|
|
532
540
|
}
|
|
533
541
|
|
|
534
|
-
private _onRemoteCollectionStateUpdated(collectionId: string, peerId: PeerId) {
|
|
542
|
+
private _onRemoteCollectionStateUpdated(collectionId: string, peerId: PeerId): void {
|
|
535
543
|
const localState = this._collectionSynchronizer.getLocalCollectionState(collectionId);
|
|
536
544
|
const remoteState = this._collectionSynchronizer.getRemoteCollectionStates(collectionId).get(peerId);
|
|
537
545
|
|
|
@@ -555,11 +563,11 @@ export class AutomergeHost extends Resource {
|
|
|
555
563
|
|
|
556
564
|
// Load the documents so they will start syncing.
|
|
557
565
|
for (const documentId of toReplicate) {
|
|
558
|
-
this._repo.
|
|
566
|
+
this._repo.findWithProgress(documentId);
|
|
559
567
|
}
|
|
560
568
|
}
|
|
561
569
|
|
|
562
|
-
private _onHeadsChanged(documentId: DocumentId, heads: Heads) {
|
|
570
|
+
private _onHeadsChanged(documentId: DocumentId, heads: Heads): void {
|
|
563
571
|
const collectionsChanged = new Set<CollectionId>();
|
|
564
572
|
for (const collectionId of this._collectionSynchronizer.getRegisteredCollectionIds()) {
|
|
565
573
|
const state = this._collectionSynchronizer.getLocalCollectionState(collectionId);
|
|
@@ -576,24 +584,14 @@ export class AutomergeHost extends Resource {
|
|
|
576
584
|
}
|
|
577
585
|
}
|
|
578
586
|
|
|
579
|
-
|
|
580
|
-
// experimental_spaceKey is set on old documents, new ones are created with doc.access.spaceKey
|
|
581
|
-
const rawSpaceKey = doc.access?.spaceKey ?? (doc as any).experimental_spaceKey;
|
|
582
|
-
if (rawSpaceKey == null) {
|
|
583
|
-
return null;
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
return String(rawSpaceKey);
|
|
587
|
-
};
|
|
588
|
-
|
|
589
|
-
const waitForHeads = async (handle: DocHandle<SpaceDoc>, heads: Heads) => {
|
|
587
|
+
const waitForHeads = async (handle: DocHandle<DatabaseDirectory>, heads: Heads) => {
|
|
590
588
|
const unavailableHeads = new Set(heads);
|
|
591
589
|
|
|
592
590
|
await handle.whenReady();
|
|
593
|
-
await Event.wrap<DocHandleChangePayload<
|
|
591
|
+
await Event.wrap<DocHandleChangePayload<DatabaseDirectory>>(handle, 'change').waitForCondition(() => {
|
|
594
592
|
// Check if unavailable heads became available.
|
|
595
593
|
for (const changeHash of unavailableHeads.values()) {
|
|
596
|
-
if (changeIsPresentInDoc(handle.
|
|
594
|
+
if (changeIsPresentInDoc(handle.doc()!, changeHash)) {
|
|
597
595
|
unavailableHeads.delete(changeHash);
|
|
598
596
|
}
|
|
599
597
|
}
|