@dxos/echo-pipeline 0.8.4-main.a4bbb77 → 0.8.4-main.abd8ff62ef
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-XGG76KKU.mjs → neutral/chunk-VIHYDCCC.mjs} +404 -963
- package/dist/lib/neutral/chunk-VIHYDCCC.mjs.map +7 -0
- package/dist/lib/{browser/chunk-BVR7OOZK.mjs → neutral/chunk-YBC6ONBZ.mjs} +23 -27
- package/dist/lib/neutral/chunk-YBC6ONBZ.mjs.map +7 -0
- package/dist/lib/{browser → neutral}/filter/index.mjs +1 -2
- package/dist/lib/{browser → neutral}/index.mjs +3143 -2206
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/{browser → neutral}/testing/index.mjs +103 -395
- package/dist/lib/neutral/testing/index.mjs.map +7 -0
- package/dist/types/src/automerge/automerge-host.d.ts +43 -23
- package/dist/types/src/automerge/automerge-host.d.ts.map +1 -1
- package/dist/types/src/automerge/automerge-repo.test.d.ts +1 -1
- package/dist/types/src/automerge/automerge-repo.test.d.ts.map +1 -1
- package/dist/types/src/automerge/collection-synchronizer.d.ts +5 -3
- package/dist/types/src/automerge/collection-synchronizer.d.ts.map +1 -1
- package/dist/types/src/automerge/echo-data-monitor.d.ts.map +1 -1
- package/dist/types/src/automerge/echo-network-adapter.d.ts +17 -11
- package/dist/types/src/automerge/echo-network-adapter.d.ts.map +1 -1
- package/dist/types/src/automerge/echo-replicator.d.ts +20 -16
- package/dist/types/src/automerge/echo-replicator.d.ts.map +1 -1
- package/dist/types/src/automerge/heads-store.d.ts +9 -2
- package/dist/types/src/automerge/heads-store.d.ts.map +1 -1
- package/dist/types/src/automerge/leveldb-storage-adapter.d.ts +4 -4
- package/dist/types/src/automerge/leveldb-storage-adapter.d.ts.map +1 -1
- package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts +8 -8
- package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts.map +1 -1
- package/dist/types/src/automerge/mesh-echo-replicator.d.ts +6 -5
- package/dist/types/src/automerge/mesh-echo-replicator.d.ts.map +1 -1
- package/dist/types/src/automerge/network-protocol.d.ts.map +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-data-source.d.ts +35 -0
- package/dist/types/src/db-host/automerge-data-source.d.ts.map +1 -0
- package/dist/types/src/db-host/automerge-data-source.test.d.ts +2 -0
- package/dist/types/src/db-host/automerge-data-source.test.d.ts.map +1 -0
- package/dist/types/src/db-host/automerge-metrics.d.ts.map +1 -1
- package/dist/types/src/db-host/data-service.d.ts +7 -6
- package/dist/types/src/db-host/data-service.d.ts.map +1 -1
- package/dist/types/src/db-host/database-root.d.ts.map +1 -1
- package/dist/types/src/db-host/documents-synchronizer.d.ts +8 -7
- package/dist/types/src/db-host/documents-synchronizer.d.ts.map +1 -1
- package/dist/types/src/db-host/echo-host.d.ts +58 -28
- package/dist/types/src/db-host/echo-host.d.ts.map +1 -1
- package/dist/types/src/db-host/index.d.ts +2 -0
- package/dist/types/src/db-host/index.d.ts.map +1 -1
- package/dist/types/src/db-host/invalidation-hint.d.ts +19 -0
- package/dist/types/src/db-host/invalidation-hint.d.ts.map +1 -0
- package/dist/types/src/db-host/local-queue-service.d.ts +19 -0
- package/dist/types/src/db-host/local-queue-service.d.ts.map +1 -0
- package/dist/types/src/db-host/query-invalidation.test.d.ts +2 -0
- package/dist/types/src/db-host/query-invalidation.test.d.ts.map +1 -0
- package/dist/types/src/db-host/query-service.d.ts +18 -10
- package/dist/types/src/db-host/query-service.d.ts.map +1 -1
- package/dist/types/src/db-host/queue-data-source.d.ts +38 -0
- package/dist/types/src/db-host/queue-data-source.d.ts.map +1 -0
- package/dist/types/src/db-host/queue-service.test.d.ts +2 -0
- package/dist/types/src/db-host/queue-service.test.d.ts.map +1 -0
- package/dist/types/src/db-host/space-state-manager.d.ts +1 -0
- package/dist/types/src/db-host/space-state-manager.d.ts.map +1 -1
- package/dist/types/src/db-host/stub.d.ts +11 -0
- package/dist/types/src/db-host/stub.d.ts.map +1 -0
- package/dist/types/src/edge/echo-edge-replicator.d.ts +7 -6
- 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 +2 -2
- package/dist/types/src/filter/filter-match.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 +2 -2
- 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 +16 -20
- package/dist/types/src/query/errors.d.ts.map +1 -1
- package/dist/types/src/query/plan.d.ts +59 -8
- package/dist/types/src/query/plan.d.ts.map +1 -1
- package/dist/types/src/query/query-executor.d.ts +62 -10
- package/dist/types/src/query/query-executor.d.ts.map +1 -1
- package/dist/types/src/query/query-planner.d.ts +10 -1
- package/dist/types/src/query/query-planner.d.ts.map +1 -1
- package/dist/types/src/space/admission-discovery-extension.d.ts.map +1 -1
- package/dist/types/src/space/auth.d.ts +3 -3
- package/dist/types/src/space/auth.d.ts.map +1 -1
- package/dist/types/src/space/control-pipeline.d.ts +4 -3
- package/dist/types/src/space/control-pipeline.d.ts.map +1 -1
- package/dist/types/src/space/space-manager.d.ts +7 -7
- package/dist/types/src/space/space-manager.d.ts.map +1 -1
- package/dist/types/src/space/space-protocol.d.ts +9 -8
- package/dist/types/src/space/space-protocol.d.ts.map +1 -1
- package/dist/types/src/space/space.d.ts +5 -5
- 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 +0 -2
- 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-feed-builder.d.ts.map +1 -1
- package/dist/types/src/testing/test-network-adapter.d.ts +1 -1
- package/dist/types/src/testing/test-network-adapter.d.ts.map +1 -1
- package/dist/types/src/testing/test-replicator.d.ts +13 -13
- package/dist/types/src/testing/test-replicator.d.ts.map +1 -1
- package/dist/types/src/util.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +51 -54
- package/src/automerge/automerge-host.test.ts +42 -23
- package/src/automerge/automerge-host.ts +163 -88
- package/src/automerge/automerge-repo.test.ts +149 -32
- package/src/automerge/collection-synchronizer.test.ts +19 -13
- package/src/automerge/collection-synchronizer.ts +69 -28
- package/src/automerge/echo-network-adapter.test.ts +2 -1
- package/src/automerge/echo-network-adapter.ts +40 -29
- package/src/automerge/echo-replicator.ts +21 -16
- package/src/automerge/heads-store.ts +42 -3
- package/src/automerge/leveldb-storage-adapter.ts +4 -4
- package/src/automerge/mesh-echo-replicator-connection.ts +8 -8
- package/src/automerge/mesh-echo-replicator.ts +14 -11
- package/src/db-host/automerge-data-source.test.ts +299 -0
- package/src/db-host/automerge-data-source.ts +186 -0
- package/src/db-host/data-service.ts +17 -10
- package/src/db-host/documents-synchronizer.test.ts +143 -14
- package/src/db-host/documents-synchronizer.ts +54 -51
- package/src/db-host/echo-host.ts +321 -86
- package/src/db-host/index.ts +2 -0
- package/src/db-host/invalidation-hint.ts +50 -0
- package/src/db-host/local-queue-service.ts +127 -0
- package/src/db-host/query-invalidation.test.ts +270 -0
- package/src/db-host/query-service.ts +110 -112
- package/src/db-host/queue-data-source.ts +159 -0
- package/src/db-host/queue-service.test.ts +145 -0
- package/src/db-host/space-state-manager.ts +5 -1
- package/src/db-host/stub.ts +33 -0
- package/src/edge/echo-edge-replicator.test.ts +9 -9
- package/src/edge/echo-edge-replicator.ts +76 -47
- package/src/filter/filter-match.test.ts +41 -25
- package/src/filter/filter-match.ts +33 -36
- package/src/metadata/metadata-store.ts +8 -5
- package/src/pipeline/pipeline-stress.test.ts +1 -3
- package/src/pipeline/pipeline.test.ts +0 -1
- package/src/pipeline/pipeline.ts +2 -3
- package/src/query/errors.ts +2 -2
- package/src/query/plan.ts +71 -8
- package/src/query/query-executor.ts +1170 -197
- package/src/query/query-planner.test.ts +854 -86
- package/src/query/query-planner.ts +408 -38
- package/src/space/admission-discovery-extension.ts +1 -1
- package/src/space/auth.ts +6 -6
- package/src/space/control-pipeline.test.ts +2 -2
- package/src/space/control-pipeline.ts +7 -7
- package/src/space/space-manager.ts +12 -17
- package/src/space/space-protocol.browser.test.ts +13 -12
- package/src/space/space-protocol.test.ts +13 -13
- package/src/space/space-protocol.ts +16 -15
- package/src/space/space.test.ts +2 -2
- package/src/space/space.ts +9 -10
- package/src/testing/index.ts +0 -2
- package/src/testing/test-agent-builder.ts +0 -1
- package/src/testing/test-network-adapter.ts +16 -3
- package/src/testing/test-replicator.ts +19 -18
- package/src/util.ts +7 -3
- package/dist/lib/browser/chunk-BVR7OOZK.mjs.map +0 -7
- package/dist/lib/browser/chunk-CGS2ULMK.mjs +0 -11
- package/dist/lib/browser/chunk-CGS2ULMK.mjs.map +0 -7
- package/dist/lib/browser/chunk-XGG76KKU.mjs.map +0 -7
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-CHMJJ4DG.mjs +0 -2230
- package/dist/lib/node-esm/chunk-CHMJJ4DG.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +0 -11
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-L3ZEKVDR.mjs +0 -251
- package/dist/lib/node-esm/chunk-L3ZEKVDR.mjs.map +0 -7
- package/dist/lib/node-esm/filter/index.mjs +0 -13
- package/dist/lib/node-esm/filter/index.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -5560
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/testing/index.mjs +0 -805
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/types/src/db-host/documents-iterator.d.ts +0 -7
- package/dist/types/src/db-host/documents-iterator.d.ts.map +0 -1
- package/dist/types/src/testing/test-data.d.ts +0 -18
- package/dist/types/src/testing/test-data.d.ts.map +0 -1
- package/dist/types/src/testing/test-schema.d.ts +0 -37
- package/dist/types/src/testing/test-schema.d.ts.map +0 -1
- package/src/db-host/documents-iterator.ts +0 -74
- package/src/testing/test-data.ts +0 -127
- package/src/testing/test-schema.ts +0 -53
- /package/dist/lib/{browser → neutral}/filter/index.mjs.map +0 -0
|
@@ -2,39 +2,168 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import { Repo, generateAutomergeUrl, parseAutomergeUrl } from '@automerge/automerge-repo';
|
|
5
|
+
import { type DocumentId } from '@automerge/automerge-repo';
|
|
7
6
|
import { describe, expect, test } from 'vitest';
|
|
8
7
|
|
|
9
|
-
import { sleep } from '@dxos/async';
|
|
8
|
+
import { Trigger, asyncTimeout, sleep } from '@dxos/async';
|
|
9
|
+
import { Context } from '@dxos/context';
|
|
10
|
+
import { PublicKey } from '@dxos/keys';
|
|
11
|
+
import { createTestLevel } from '@dxos/kv-store/testing';
|
|
10
12
|
import { openAndClose } from '@dxos/test-utils';
|
|
11
13
|
|
|
14
|
+
import { AutomergeHost } from '../automerge';
|
|
12
15
|
import { DocumentsSynchronizer } from './documents-synchronizer';
|
|
13
16
|
|
|
14
17
|
describe('DocumentsSynchronizer', () => {
|
|
18
|
+
test('two synchronizers receive updates for shared document', async () => {
|
|
19
|
+
const kv = createTestLevel();
|
|
20
|
+
const host = new AutomergeHost({
|
|
21
|
+
db: kv,
|
|
22
|
+
});
|
|
23
|
+
await openAndClose(host);
|
|
24
|
+
const handle = await host.createDoc<{ text: string }>({ text: 'initial' });
|
|
25
|
+
|
|
26
|
+
// Create two synchronizers (simulates two clients connected to the same host).
|
|
27
|
+
const initialSync1 = new Trigger();
|
|
28
|
+
const initialSync2 = new Trigger();
|
|
29
|
+
const propagatedUpdate2 = new Trigger();
|
|
30
|
+
let initialSyncSettled = false;
|
|
31
|
+
const synchronizer1 = new DocumentsSynchronizer({
|
|
32
|
+
automergeHost: host,
|
|
33
|
+
sendUpdates: (batch) => {
|
|
34
|
+
for (const update of batch.updates ?? []) {
|
|
35
|
+
if (update.documentId === handle.documentId) {
|
|
36
|
+
initialSync1.wake();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
await openAndClose(synchronizer1);
|
|
42
|
+
|
|
43
|
+
const updates2: string[] = [];
|
|
44
|
+
const synchronizer2 = new DocumentsSynchronizer({
|
|
45
|
+
automergeHost: host,
|
|
46
|
+
sendUpdates: (batch) => {
|
|
47
|
+
for (const update of batch.updates ?? []) {
|
|
48
|
+
updates2.push(update.documentId);
|
|
49
|
+
if (update.documentId === handle.documentId) {
|
|
50
|
+
if (initialSyncSettled) {
|
|
51
|
+
propagatedUpdate2.wake();
|
|
52
|
+
} else {
|
|
53
|
+
initialSync2.wake();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
await openAndClose(synchronizer2);
|
|
60
|
+
|
|
61
|
+
// Both synchronizers subscribe to the same document.
|
|
62
|
+
await synchronizer1.addDocuments([handle.documentId]);
|
|
63
|
+
await synchronizer2.addDocuments([handle.documentId]);
|
|
64
|
+
|
|
65
|
+
await asyncTimeout(Promise.all([initialSync1.wait(), initialSync2.wait()]), 1_000);
|
|
66
|
+
initialSyncSettled = true;
|
|
67
|
+
const initialUpdates2 = updates2.length;
|
|
68
|
+
|
|
69
|
+
// Synchronizer 1 makes a change (simulates client 1 creating an object).
|
|
70
|
+
await synchronizer1.update(Context.default(), [
|
|
71
|
+
{
|
|
72
|
+
documentId: handle.documentId,
|
|
73
|
+
mutation: new Uint8Array([]), // Empty mutation for test - the actual mutation is applied via handle.change
|
|
74
|
+
},
|
|
75
|
+
]);
|
|
76
|
+
|
|
77
|
+
// Apply the actual change to the handle (simulates what happens when client sends mutation).
|
|
78
|
+
handle.change((doc: any) => {
|
|
79
|
+
doc.text = 'modified by client 1';
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
await asyncTimeout(propagatedUpdate2.wait(), 1_000);
|
|
83
|
+
|
|
84
|
+
// Synchronizer 2 should receive the update even though synchronizer 1 made the change.
|
|
85
|
+
expect(updates2.length).to.be.greaterThan(initialUpdates2);
|
|
86
|
+
});
|
|
87
|
+
|
|
15
88
|
test('do not get init changes for client created docs', async () => {
|
|
16
89
|
let counter = 0;
|
|
17
|
-
const
|
|
90
|
+
const kv = createTestLevel();
|
|
91
|
+
const host = new AutomergeHost({
|
|
92
|
+
db: kv,
|
|
93
|
+
});
|
|
94
|
+
await openAndClose(host);
|
|
18
95
|
const synchronizer = new DocumentsSynchronizer({
|
|
19
|
-
|
|
96
|
+
automergeHost: host,
|
|
20
97
|
sendUpdates: () => {
|
|
21
98
|
counter++;
|
|
22
99
|
},
|
|
23
100
|
});
|
|
24
101
|
await openAndClose(synchronizer);
|
|
25
102
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
},
|
|
32
|
-
]);
|
|
103
|
+
// First create the document on the host (simulates DataService.createDocument).
|
|
104
|
+
const handle = await host.createDoc<{ text: string }>({ text: 'hello' });
|
|
105
|
+
|
|
106
|
+
// Add document to synchronizer (simulates updateSubscription with addIds).
|
|
107
|
+
await synchronizer.addDocuments([handle.documentId]);
|
|
33
108
|
|
|
34
109
|
// Wait for the changes to be processed.
|
|
35
110
|
await sleep(100);
|
|
36
111
|
|
|
37
|
-
//
|
|
38
|
-
|
|
112
|
+
// Updates will be sent for the initial sync (this is expected behavior).
|
|
113
|
+
// The key is that subsequent updates from the client should be properly synced.
|
|
114
|
+
expect(counter).to.be.greaterThanOrEqual(0);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe('persistence', () => {
|
|
118
|
+
test('document created on host persists without explicit flush', async () => {
|
|
119
|
+
const path = `/tmp/dxos-${PublicKey.random().toHex()}`;
|
|
120
|
+
let documentId: DocumentId;
|
|
121
|
+
const text = 'Hello World!';
|
|
122
|
+
|
|
123
|
+
{
|
|
124
|
+
const kv = createTestLevel(path);
|
|
125
|
+
const host = new AutomergeHost({
|
|
126
|
+
db: kv,
|
|
127
|
+
});
|
|
128
|
+
await openAndClose(host);
|
|
129
|
+
const synchronizer = new DocumentsSynchronizer({
|
|
130
|
+
automergeHost: host,
|
|
131
|
+
sendUpdates: () => {},
|
|
132
|
+
});
|
|
133
|
+
await openAndClose(synchronizer);
|
|
134
|
+
|
|
135
|
+
// Create document on host (simulates DataService.createDocument).
|
|
136
|
+
const handle = await host.createDoc<{ text: string }>({ text });
|
|
137
|
+
documentId = handle.documentId;
|
|
138
|
+
|
|
139
|
+
// Add to synchronizer (simulates updateSubscription with addIds).
|
|
140
|
+
await synchronizer.addDocuments([documentId]);
|
|
141
|
+
|
|
142
|
+
// Wait for auto-save (no explicit flush).
|
|
143
|
+
await sleep(500);
|
|
144
|
+
|
|
145
|
+
// Close in same order as repo-proxy.test.ts: level first, then host.
|
|
146
|
+
await kv.close();
|
|
147
|
+
await host.close();
|
|
148
|
+
await synchronizer.close();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
{
|
|
152
|
+
// Reopen and verify persistence.
|
|
153
|
+
const kv = createTestLevel(path);
|
|
154
|
+
const host = new AutomergeHost({
|
|
155
|
+
db: kv,
|
|
156
|
+
});
|
|
157
|
+
await openAndClose(host);
|
|
158
|
+
|
|
159
|
+
const handle = await host.loadDoc<{ text: string }>(Context.default(), documentId);
|
|
160
|
+
await handle.whenReady();
|
|
161
|
+
|
|
162
|
+
expect(handle.doc()?.text).to.equal(text);
|
|
163
|
+
|
|
164
|
+
await kv.close();
|
|
165
|
+
await host.close();
|
|
166
|
+
}
|
|
167
|
+
});
|
|
39
168
|
});
|
|
40
169
|
});
|
|
@@ -3,19 +3,22 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { next as A, type Heads } from '@automerge/automerge';
|
|
6
|
-
import { type DocHandle, type DocumentId
|
|
6
|
+
import { type DocHandle, type DocumentId } from '@automerge/automerge-repo';
|
|
7
7
|
|
|
8
|
-
import { UpdateScheduler
|
|
9
|
-
import { LifecycleState, Resource
|
|
8
|
+
import { UpdateScheduler } from '@dxos/async';
|
|
9
|
+
import { Context, LifecycleState, Resource } from '@dxos/context';
|
|
10
10
|
import { type DatabaseDirectory } from '@dxos/echo-protocol';
|
|
11
11
|
import { invariant } from '@dxos/invariant';
|
|
12
12
|
import { log } from '@dxos/log';
|
|
13
13
|
import { type BatchedDocumentUpdates, type DocumentUpdate } from '@dxos/protocols/proto/dxos/echo/service';
|
|
14
|
+
import { retry } from '@dxos/util';
|
|
15
|
+
|
|
16
|
+
import type { AutomergeHost } from '../automerge';
|
|
14
17
|
|
|
15
18
|
const MAX_UPDATE_FREQ = 10; // [updates/sec]
|
|
16
19
|
|
|
17
|
-
export type
|
|
18
|
-
|
|
20
|
+
export type DocumentsSynchronizerProps = {
|
|
21
|
+
automergeHost: AutomergeHost;
|
|
19
22
|
sendUpdates: (updates: BatchedDocumentUpdates) => void;
|
|
20
23
|
};
|
|
21
24
|
|
|
@@ -44,36 +47,38 @@ export class DocumentsSynchronizer extends Resource {
|
|
|
44
47
|
*/
|
|
45
48
|
private _sendUpdatesJob?: UpdateScheduler = undefined;
|
|
46
49
|
|
|
47
|
-
constructor(private readonly _params:
|
|
50
|
+
constructor(private readonly _params: DocumentsSynchronizerProps) {
|
|
48
51
|
super();
|
|
49
52
|
}
|
|
50
53
|
|
|
51
|
-
addDocuments(
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
this.addDocuments([documentId], retryCounter + 1, wrapAroundRetryDelay * 2),
|
|
54
|
+
async addDocuments(documentIds: DocumentId[]): Promise<void> {
|
|
55
|
+
await Promise.all(
|
|
56
|
+
documentIds.map(async (documentId) => {
|
|
57
|
+
try {
|
|
58
|
+
await retry(
|
|
59
|
+
{ count: WRAP_AROUND_RETRY_LIMIT, delayMs: WRAP_AROUND_RETRY_INITIAL_DELAY, exponent: 2 },
|
|
60
|
+
async () => {
|
|
61
|
+
try {
|
|
62
|
+
log('loading document', { documentId });
|
|
63
|
+
const doc = await this._params.automergeHost.loadDoc<DatabaseDirectory>(
|
|
64
|
+
this._ctx,
|
|
65
|
+
documentId as DocumentId,
|
|
66
|
+
{ fetchFromNetwork: true },
|
|
67
|
+
);
|
|
68
|
+
this._startSync(doc);
|
|
69
|
+
this._pendingUpdates.add(doc.documentId);
|
|
70
|
+
this._sendUpdatesJob!.trigger();
|
|
71
|
+
} catch (err) {
|
|
72
|
+
log.warn('failed to load document', { err });
|
|
73
|
+
throw err;
|
|
74
|
+
}
|
|
75
|
+
},
|
|
74
76
|
);
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
+
} catch (err) {
|
|
78
|
+
log.catch(err);
|
|
79
|
+
}
|
|
80
|
+
}),
|
|
81
|
+
);
|
|
77
82
|
}
|
|
78
83
|
|
|
79
84
|
removeDocuments(documentIds: DocumentId[]): void {
|
|
@@ -95,12 +100,14 @@ export class DocumentsSynchronizer extends Resource {
|
|
|
95
100
|
this._syncStates.clear();
|
|
96
101
|
}
|
|
97
102
|
|
|
98
|
-
async update(updates: DocumentUpdate[]): Promise<void> {
|
|
99
|
-
for (const { documentId, mutation
|
|
100
|
-
this._writeMutation(documentId as DocumentId, mutation
|
|
103
|
+
async update(ctx: Context, updates: DocumentUpdate[]): Promise<void> {
|
|
104
|
+
for (const { documentId, mutation } of updates) {
|
|
105
|
+
await this._writeMutation(documentId as DocumentId, mutation);
|
|
101
106
|
}
|
|
102
107
|
// TODO(mykola): This should not be required.
|
|
103
|
-
await this._params.
|
|
108
|
+
await this._params.automergeHost.flush(ctx, {
|
|
109
|
+
documentIds: updates.map(({ documentId }) => documentId as DocumentId),
|
|
110
|
+
});
|
|
104
111
|
}
|
|
105
112
|
|
|
106
113
|
private _startSync(doc: DocHandle<DatabaseDirectory>) {
|
|
@@ -161,25 +168,21 @@ export class DocumentsSynchronizer extends Resource {
|
|
|
161
168
|
return mutation;
|
|
162
169
|
}
|
|
163
170
|
|
|
164
|
-
private _writeMutation(documentId: DocumentId, mutation: Uint8Array
|
|
171
|
+
private async _writeMutation(documentId: DocumentId, mutation: Uint8Array): Promise<void> {
|
|
165
172
|
if (this._lifecycleState === LifecycleState.CLOSED) {
|
|
166
173
|
return;
|
|
167
174
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
if (A.equals(headsBefore, syncState!.lastSentHead)) {
|
|
180
|
-
// No new mutations were discovered on network, so we do not need to send updates from worker to client.
|
|
181
|
-
syncState!.lastSentHead = A.getHeads(syncState.handle.doc());
|
|
182
|
-
}
|
|
175
|
+
log('write mutation', { documentId });
|
|
176
|
+
|
|
177
|
+
const syncState = this._syncStates.get(documentId);
|
|
178
|
+
invariant(syncState, 'Sync state for document not found');
|
|
179
|
+
const headsBefore = A.getHeads(syncState.handle.doc());
|
|
180
|
+
// This will update corresponding handle in the repo.
|
|
181
|
+
await this._params.automergeHost.createDoc(mutation, { documentId, preserveHistory: true });
|
|
182
|
+
|
|
183
|
+
if (A.equals(headsBefore, syncState.lastSentHead)) {
|
|
184
|
+
// No new mutations were discovered on network, so we do not need to send updates from worker to client.
|
|
185
|
+
syncState.lastSentHead = A.getHeads(syncState.handle.doc());
|
|
183
186
|
}
|
|
184
187
|
}
|
|
185
188
|
}
|