@dxos/echo-pipeline 0.4.8-main.2d8e727 → 0.4.8-main.2e7285f

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.
@@ -13,7 +13,7 @@ import {
13
13
  } from '@dxos/automerge/automerge-repo';
14
14
  import { IndexedDBStorageAdapter } from '@dxos/automerge/automerge-repo-storage-indexeddb';
15
15
  import { type Stream } from '@dxos/codec-protobuf';
16
- import { Context, cancelWithContext } from '@dxos/context';
16
+ import { Context } from '@dxos/context';
17
17
  import { PublicKey } from '@dxos/keys';
18
18
  import { log } from '@dxos/log';
19
19
  import { idCodec } from '@dxos/protocols';
@@ -31,7 +31,7 @@ import { MeshNetworkAdapter } from './mesh-network-adapter';
31
31
  export type { DocumentId };
32
32
 
33
33
  export interface MetadataMethods {
34
- markDirty(id: string, lastAvailableHash: string): Promise<void>;
34
+ markDirty(idToLastHash: Map<string, string>): Promise<void>;
35
35
  }
36
36
 
37
37
  export type AutomergeHostParams = {
@@ -104,7 +104,7 @@ export class AutomergeHost {
104
104
  return false;
105
105
  }
106
106
 
107
- const authorizedDevices = this._authorizedDevices.get(spaceKey);
107
+ const authorizedDevices = this._authorizedDevices.get(PublicKey.from(spaceKey));
108
108
 
109
109
  // TODO(mykola): Hack, stop abusing `peerMetadata` field.
110
110
  const deviceKeyHex = (this.repo.peerMetadataByPeerId[peerId] as any)?.dxos_deviceKey;
@@ -162,8 +162,7 @@ export class AutomergeHost {
162
162
  }
163
163
 
164
164
  private _onUpdate(event: DocHandleChangePayload<any>) {
165
- const spaceKey = event.doc.access?.spaceKey;
166
- if (!spaceKey) {
165
+ if (this._metadata == null) {
167
166
  return;
168
167
  }
169
168
 
@@ -178,28 +177,16 @@ export class AutomergeHost {
178
177
  return;
179
178
  }
180
179
 
181
- const markingDirtyPromise = Promise.all(
182
- objectIds.map(async (objectId) => {
183
- const spaceKey = getSpaceKeyFromDoc(event.doc);
184
- if (!spaceKey) {
185
- return;
186
- }
187
- await cancelWithContext(
188
- this._ctx,
189
- this._metadata!.markDirty(
190
- idCodec.encode({ documentId: event.handle.documentId, objectId, spaceKey }),
191
- lastAvailableHash,
192
- ),
193
- );
194
- }),
195
- )
180
+ const encodedIds = objectIds.map((objectId) => idCodec.encode({ documentId: event.handle.documentId, objectId }));
181
+ const idToLastHash = new Map(encodedIds.map((id) => [id, lastAvailableHash]));
182
+ const markingDirtyPromise = this._metadata
183
+ .markDirty(idToLastHash)
196
184
  .then(() => {
197
185
  this._updatingMetadata.delete(event.handle.documentId);
198
186
  })
199
187
  .catch((err: Error) => {
200
- !this._ctx.disposed && log.catch(err);
188
+ this._ctx.disposed && log.catch(err);
201
189
  });
202
-
203
190
  this._updatingMetadata.set(event.handle.documentId, markingDirtyPromise);
204
191
  }
205
192
 
@@ -288,12 +275,12 @@ const getInlineChanges = (event: DocHandleChangePayload<any>) => {
288
275
  return [...inlineChangedObjectIds];
289
276
  };
290
277
 
291
- export const getSpaceKeyFromDoc = (doc: any) => {
278
+ export const getSpaceKeyFromDoc = (doc: any): string | null => {
292
279
  // experimental_spaceKey is set on old documents, new ones are created with doc.access.spaceKey
293
280
  const rawSpaceKey = doc.access?.spaceKey ?? doc.experimental_spaceKey;
294
- if (!rawSpaceKey) {
295
- return;
281
+ if (rawSpaceKey == null) {
282
+ return null;
296
283
  }
297
284
 
298
- return rawSpaceKey;
285
+ return String(rawSpaceKey);
299
286
  };
@@ -10,7 +10,7 @@ import {
10
10
  type SpecificCredential,
11
11
  checkCredentialType,
12
12
  } from '@dxos/credentials';
13
- import { getStateMachineFromItem, ItemManager, TYPE_PROPERTIES } from '@dxos/echo-db';
13
+ import { ItemManager } from '@dxos/echo-db';
14
14
  import { type FeedWriter } from '@dxos/feed-store';
15
15
  import { invariant } from '@dxos/invariant';
16
16
  import { type PublicKey } from '@dxos/keys';
@@ -19,8 +19,6 @@ import { type ModelFactory } from '@dxos/model-factory';
19
19
  import { CancelledError, type DataPipelineProcessed } from '@dxos/protocols';
20
20
  import { type CreateEpochRequest } from '@dxos/protocols/proto/dxos/client/services';
21
21
  import { type DataMessage } from '@dxos/protocols/proto/dxos/echo/feed';
22
- import { type SpaceCache } from '@dxos/protocols/proto/dxos/echo/metadata';
23
- import { type ObjectSnapshot } from '@dxos/protocols/proto/dxos/echo/model/document';
24
22
  import { type SpaceSnapshot } from '@dxos/protocols/proto/dxos/echo/snapshot';
25
23
  import { type Credential, type Epoch } from '@dxos/protocols/proto/dxos/halo/credentials';
26
24
  import { Timeframe } from '@dxos/timeframe';
@@ -50,16 +48,6 @@ export type DataPipelineParams = {
50
48
  onPipelineCreated: (pipeline: Pipeline) => Promise<void>;
51
49
  };
52
50
 
53
- /**
54
- * Number of mutations since the last snapshot before we automatically create another snapshot.
55
- */
56
- const MESSAGES_PER_SNAPSHOT = 10;
57
-
58
- /**
59
- * Minimum time between automatic snapshots.
60
- */
61
- const AUTOMATIC_SNAPSHOT_DEBOUNCE_INTERVAL = 5_000;
62
-
63
51
  /**
64
52
  * Minimum time in MS between recording latest timeframe in metadata.
65
53
  */
@@ -194,7 +182,6 @@ export class DataPipeline implements CredentialProcessor {
194
182
 
195
183
  // NOTE: Make sure the processing is stopped BEFORE we save the snapshot.
196
184
  try {
197
- await this._saveCache();
198
185
  if (this._pipeline) {
199
186
  await this._saveTargetTimeframe(this._pipeline.state.timeframe);
200
187
  }
@@ -293,28 +280,6 @@ export class DataPipeline implements CredentialProcessor {
293
280
  this._targetTimeframe = newTimeframe;
294
281
  }
295
282
 
296
- private async _saveCache() {
297
- const cache: SpaceCache = {};
298
-
299
- try {
300
- // Add properties to cache.
301
- const propertiesItem = this.itemManager.items.find(
302
- (item) =>
303
- item.modelMeta?.type === 'dxos.org/model/document' &&
304
- // TODO(burdon): Document?
305
- (getStateMachineFromItem(item)?.snapshot() as ObjectSnapshot).type === TYPE_PROPERTIES,
306
- );
307
- if (propertiesItem) {
308
- cache.properties = getStateMachineFromItem(propertiesItem)?.snapshot() as ObjectSnapshot;
309
- }
310
- } catch (err) {
311
- log.warn('Failed to cache properties', err);
312
- }
313
-
314
- // Save cache.
315
- await this._params.metadataStore.setCache(this._params.spaceKey, cache);
316
- }
317
-
318
283
  private async _noteTargetStateIfNeeded(timeframe: Timeframe) {
319
284
  if (!this._pipeline?.state.reachedTarget) {
320
285
  return;
@@ -327,13 +292,6 @@ export class DataPipeline implements CredentialProcessor {
327
292
 
328
293
  await this._saveTargetTimeframe(timeframe);
329
294
  }
330
-
331
- if (
332
- Date.now() - this._lastSnapshotSaveTime > AUTOMATIC_SNAPSHOT_DEBOUNCE_INTERVAL &&
333
- timeframe.totalMessages() - this._lastAutomaticSnapshotTimeframe.totalMessages() > MESSAGES_PER_SNAPSHOT
334
- ) {
335
- await this._saveCache();
336
- }
337
295
  }
338
296
 
339
297
  private async _processEpochInSeparateTask(epoch: SpecificCredential<Epoch>) {
@@ -419,7 +377,6 @@ export class DataPipeline implements CredentialProcessor {
419
377
 
420
378
  private async _flush() {
421
379
  try {
422
- await this._saveCache();
423
380
  if (this._pipeline) {
424
381
  await this._saveTargetTimeframe(this._pipeline.state.timeframe);
425
382
  }