@dxos/echo-pipeline 0.4.10-main.e6ba7bc → 0.4.10-main.e97ef17

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.
@@ -2,19 +2,11 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { Event } from '@dxos/async';
6
- import { type Doc, next as automerge, getBackend, type Heads } from '@dxos/automerge/automerge';
7
- import {
8
- type DocHandle,
9
- Repo,
10
- type DocumentId,
11
- type PeerId,
12
- type StorageAdapterInterface,
13
- type DocHandleChangePayload,
14
- } from '@dxos/automerge/automerge-repo';
5
+ import { asyncTimeout } from '@dxos/async';
6
+ import { next as automerge } from '@dxos/automerge/automerge';
7
+ import { Repo, type DocumentId, type PeerId, type StorageAdapterInterface } from '@dxos/automerge/automerge-repo';
15
8
  import { type Stream } from '@dxos/codec-protobuf';
16
9
  import { Context, type Lifecycle } from '@dxos/context';
17
- import { invariant } from '@dxos/invariant';
18
10
  import { PublicKey } from '@dxos/keys';
19
11
  import { log } from '@dxos/log';
20
12
  import {
@@ -32,7 +24,7 @@ import { LevelDBStorageAdapter, type StorageCallbacks } from './leveldb-storage-
32
24
  import { LocalHostNetworkAdapter } from './local-host-network-adapter';
33
25
  import { MeshNetworkAdapter } from './mesh-network-adapter';
34
26
  import { levelMigration } from './migrations';
35
- import { type SpaceDoc, type SubLevelDB } from './types';
27
+ import { type SubLevelDB } from './types';
36
28
 
37
29
  // TODO: Remove
38
30
  export type { DocumentId };
@@ -193,17 +185,16 @@ export class AutomergeHost {
193
185
  // Methods for client-services.
194
186
  //
195
187
 
196
- async flush({ states }: FlushRequest): Promise<void> {
188
+ async flush({ documentIds }: FlushRequest): Promise<void> {
197
189
  // Note: Wait for all requested documents to be loaded/synced from thin-client.
198
- await Promise.all(
199
- states?.map(async ({ heads, documentId }) => {
200
- invariant(heads, 'heads are required for flush');
201
- const handle = this.repo.handles[documentId as DocumentId] ?? this._repo.find(documentId as DocumentId);
202
- await waitForHeads(handle, heads);
203
- }) ?? [],
204
- );
205
-
206
- await this._repo.flush(states?.map(({ documentId }) => documentId as DocumentId));
190
+ await Promise.all(documentIds?.map((id) => this._repo.find(id as DocumentId).whenReady()) ?? []);
191
+
192
+ // TODO(dmaretskyi): Workaround until the flush issue gets resolved.
193
+ try {
194
+ await asyncTimeout(this._repo.flush(documentIds as DocumentId[]), 500);
195
+ } catch (err) {
196
+ log.warn('flush error', { documentIds, err });
197
+ }
207
198
  }
208
199
 
209
200
  syncRepo(request: SyncRepoRequest): Stream<SyncRepoResponse> {
@@ -241,26 +232,3 @@ export const getSpaceKeyFromDoc = (doc: any): string | null => {
241
232
 
242
233
  return String(rawSpaceKey);
243
234
  };
244
-
245
- const waitForHeads = async (handle: DocHandle<SpaceDoc>, heads: Heads) => {
246
- await handle.whenReady();
247
- const unavailableHeads = new Set(heads);
248
-
249
- await Event.wrap<DocHandleChangePayload<SpaceDoc>>(handle, 'change').waitForCondition(() => {
250
- // Check if unavailable heads became available.
251
- for (const changeHash of unavailableHeads.values()) {
252
- if (changeIsPresentInDoc(handle.docSync(), changeHash)) {
253
- unavailableHeads.delete(changeHash);
254
- }
255
- }
256
-
257
- if (unavailableHeads.size === 0) {
258
- return true;
259
- }
260
- return false;
261
- });
262
- };
263
-
264
- const changeIsPresentInDoc = (doc: Doc<any>, changeHash: string): boolean => {
265
- return !!getBackend(doc).getChangeByHash(changeHash);
266
- };
@@ -2,25 +2,19 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { expect } from 'chai';
6
-
7
- import { change, clone, from, getBackend, getHeads } from '@dxos/automerge/automerge';
8
5
  import { Repo } from '@dxos/automerge/automerge-repo';
9
6
  import { randomBytes } from '@dxos/crypto';
10
- import { describe, openAndClose, test } from '@dxos/test';
7
+ import { StorageType, createStorage } from '@dxos/random-access-storage';
8
+ import { describe, test } from '@dxos/test';
11
9
 
12
- import { LevelDBStorageAdapter } from './leveldb-storage-adapter';
13
- import { createTestLevel } from '../testing';
10
+ import { AutomergeStorageAdapter } from './automerge-storage-adapter';
14
11
 
15
12
  describe('AutomergeRepo', () => {
16
- test('flush', async () => {
17
- const level = createTestLevel();
18
- const storage = new LevelDBStorageAdapter({ db: level.sublevel('automerge') });
19
- await openAndClose(level, storage);
20
-
13
+ // Currently failing
14
+ test.skip('flush', async () => {
21
15
  const repo = new Repo({
22
16
  network: [],
23
- storage,
17
+ storage: new AutomergeStorageAdapter(createStorage({ type: StorageType.NODE }).createDirectory()),
24
18
  });
25
19
  const handle = repo.create<{ field?: string }>();
26
20
 
@@ -32,30 +26,4 @@ describe('AutomergeRepo', () => {
32
26
  await p;
33
27
  }
34
28
  });
35
-
36
- test('getChangeByHash', async () => {
37
- const level = createTestLevel();
38
- const storage = new LevelDBStorageAdapter({ db: level.sublevel('automerge') });
39
- await openAndClose(level, storage);
40
-
41
- const doc = from({ foo: 'bar' });
42
- const copy = clone(doc);
43
- const newDoc = change(copy, 'change', (doc: any) => {
44
- doc.foo = 'baz';
45
- });
46
-
47
- {
48
- const heads = getHeads(newDoc);
49
- const changes = heads.map((hash) => getBackend(newDoc).getChangeByHash(hash));
50
- expect(changes.length).to.equal(1);
51
- expect(changes[0]).to.not.be.null;
52
- }
53
-
54
- {
55
- const heads = getHeads(newDoc);
56
- const changes = heads.map((hash) => getBackend(doc).getChangeByHash(hash));
57
- expect(changes.length).to.equal(1);
58
- expect(changes[0]).to.be.null;
59
- }
60
- });
61
29
  });
@@ -8,5 +8,4 @@ import { PublicKey } from '@dxos/keys';
8
8
 
9
9
  import { type LevelDB } from '../automerge/types';
10
10
 
11
- export const createTestLevel = (path = `/tmp/dxos-${PublicKey.random().toHex()}`): LevelDB =>
12
- new Level<string, string>(path);
11
+ export const createTestLevel = (): LevelDB => new Level<string, string>(`/tmp/dxos-${PublicKey.random().toHex()}`);