@dxos/echo-pipeline 0.4.7-main.ea67fec → 0.4.7-main.f4b92be

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.
@@ -3076,14 +3076,48 @@ var MeshNetworkAdapter = class extends NetworkAdapter2 {
3076
3076
  };
3077
3077
 
3078
3078
  // packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts
3079
- import { next as automerge } from "@dxos/automerge/automerge";
3079
+ import { next as automerge, getHeads } from "@dxos/automerge/automerge";
3080
3080
  import { Repo } from "@dxos/automerge/automerge-repo";
3081
3081
  import { IndexedDBStorageAdapter } from "@dxos/automerge/automerge-repo-storage-indexeddb";
3082
+ import { Context as Context6, cancelWithContext as cancelWithContext2 } from "@dxos/context";
3082
3083
  import { PublicKey as PublicKey8 } from "@dxos/keys";
3083
3084
  import { log as log15 } from "@dxos/log";
3085
+ import { idCodec } from "@dxos/protocols";
3084
3086
  import { StorageType } from "@dxos/random-access-storage";
3085
3087
  import { trace as trace6 } from "@dxos/tracing";
3086
3088
  import { ComplexMap as ComplexMap7, ComplexSet, defaultMap, mapValues } from "@dxos/util";
3089
+
3090
+ // packages/core/echo/echo-pipeline/src/automerge/automerge-storage–wrapper.ts
3091
+ var AutomergeStorageWrapper = class {
3092
+ constructor({ storage, callbacks }) {
3093
+ this._storage = storage;
3094
+ this._callbacks = callbacks;
3095
+ }
3096
+ async load(key) {
3097
+ return this._storage.load(key);
3098
+ }
3099
+ async save(key, value) {
3100
+ await this._callbacks.beforeSave?.(key);
3101
+ await this._storage.save(key, value);
3102
+ await this._callbacks.afterSave?.(key);
3103
+ }
3104
+ async remove(key) {
3105
+ return this._storage.remove(key);
3106
+ }
3107
+ async loadRange(keyPrefix) {
3108
+ return this._storage.loadRange(keyPrefix);
3109
+ }
3110
+ async removeRange(keyPrefix) {
3111
+ return this._storage.removeRange(keyPrefix);
3112
+ }
3113
+ async close() {
3114
+ if (this._storage instanceof AutomergeStorageAdapter) {
3115
+ return this._storage.close();
3116
+ }
3117
+ }
3118
+ };
3119
+
3120
+ // packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts
3087
3121
  function _ts_decorate9(decorators, target, key, desc) {
3088
3122
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3089
3123
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -3096,15 +3130,26 @@ function _ts_decorate9(decorators, target, key, desc) {
3096
3130
  }
3097
3131
  var __dxlog_file16 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
3098
3132
  var AutomergeHost = class {
3099
- constructor(storageDirectory) {
3133
+ constructor({ directory, metadata }) {
3134
+ this._ctx = new Context6();
3100
3135
  /**
3101
3136
  * spaceKey -> deviceKey[]
3102
3137
  */
3103
3138
  this._authorizedDevices = new ComplexMap7(PublicKey8.hash);
3139
+ this._updatingMetadata = /* @__PURE__ */ new Map();
3104
3140
  this._requestedDocs = /* @__PURE__ */ new Set();
3141
+ this._metadata = metadata;
3105
3142
  this._meshNetwork = new MeshNetworkAdapter();
3106
3143
  this._clientNetwork = new LocalHostNetworkAdapter();
3107
- this._storage = storageDirectory.type === StorageType.IDB ? new IndexedDBStorageAdapter(storageDirectory.path, "data") : new AutomergeStorageAdapter(storageDirectory);
3144
+ this._storage = new AutomergeStorageWrapper({
3145
+ storage: (
3146
+ // TODO(mykola): Delete specific handling of IDB storage.
3147
+ directory.type === StorageType.IDB ? new IndexedDBStorageAdapter(directory.path, "data") : new AutomergeStorageAdapter(directory)
3148
+ ),
3149
+ callbacks: {
3150
+ beforeSave: (params) => this._beforeSave(params)
3151
+ }
3152
+ });
3108
3153
  this._peerId = `host-${PublicKey8.random().toHex()}`;
3109
3154
  this._repo = new Repo({
3110
3155
  peerId: this._peerId,
@@ -3131,7 +3176,7 @@ var AutomergeHost = class {
3131
3176
  isRequested
3132
3177
  }, {
3133
3178
  F: __dxlog_file16,
3134
- L: 69,
3179
+ L: 96,
3135
3180
  S: this,
3136
3181
  C: (f, a) => f(...a)
3137
3182
  });
@@ -3145,7 +3190,7 @@ var AutomergeHost = class {
3145
3190
  documentId
3146
3191
  }, {
3147
3192
  F: __dxlog_file16,
3148
- L: 77,
3193
+ L: 104,
3149
3194
  S: this,
3150
3195
  C: (f, a) => f(...a)
3151
3196
  });
@@ -3160,7 +3205,7 @@ var AutomergeHost = class {
3160
3205
  documentId
3161
3206
  }, {
3162
3207
  F: __dxlog_file16,
3163
- L: 87,
3208
+ L: 114,
3164
3209
  S: this,
3165
3210
  C: (f, a) => f(...a)
3166
3211
  });
@@ -3177,7 +3222,7 @@ var AutomergeHost = class {
3177
3222
  isAuthorized
3178
3223
  }, {
3179
3224
  F: __dxlog_file16,
3180
- L: 93,
3225
+ L: 120,
3181
3226
  S: this,
3182
3227
  C: (f, a) => f(...a)
3183
3228
  });
@@ -3185,7 +3230,7 @@ var AutomergeHost = class {
3185
3230
  } catch (err) {
3186
3231
  log15.catch(err, void 0, {
3187
3232
  F: __dxlog_file16,
3188
- L: 103,
3233
+ L: 130,
3189
3234
  S: this,
3190
3235
  C: (f, a) => f(...a)
3191
3236
  });
@@ -3195,10 +3240,61 @@ var AutomergeHost = class {
3195
3240
  });
3196
3241
  this._clientNetwork.ready();
3197
3242
  this._meshNetwork.ready();
3243
+ {
3244
+ const listener = ({ handle }) => this._onDocument(handle);
3245
+ this._repo.on("document", listener);
3246
+ this._ctx.onDispose(() => {
3247
+ this._repo.off("document", listener);
3248
+ });
3249
+ }
3198
3250
  }
3199
3251
  get repo() {
3200
3252
  return this._repo;
3201
3253
  }
3254
+ async _beforeSave(path) {
3255
+ const id = path[0];
3256
+ if (this._updatingMetadata.has(id)) {
3257
+ return this._updatingMetadata.get(id);
3258
+ }
3259
+ }
3260
+ _onDocument(handle) {
3261
+ const listener = (event) => this._onUpdate(event);
3262
+ handle.on("change", listener);
3263
+ this._ctx.onDispose(() => {
3264
+ handle.off("change", listener);
3265
+ });
3266
+ }
3267
+ _onUpdate(event) {
3268
+ const spaceKey = event.doc.access?.spaceKey;
3269
+ if (!spaceKey) {
3270
+ return;
3271
+ }
3272
+ const objectIds = getInlineChanges(event);
3273
+ if (objectIds.length === 0) {
3274
+ return;
3275
+ }
3276
+ const heads = getHeads(event.doc);
3277
+ const lastAvailableHash = heads.at(-1);
3278
+ if (!lastAvailableHash) {
3279
+ return;
3280
+ }
3281
+ const markingDirtyPromise = Promise.all(objectIds.map(async (objectId) => {
3282
+ await cancelWithContext2(this._ctx, this._metadata.markDirty(idCodec.encode({
3283
+ documentId: event.handle.documentId,
3284
+ objectId
3285
+ }), lastAvailableHash));
3286
+ })).then(() => {
3287
+ this._updatingMetadata.delete(event.handle.documentId);
3288
+ }).catch((err) => {
3289
+ !this._ctx.disposed && log15.catch(err, void 0, {
3290
+ F: __dxlog_file16,
3291
+ L: 198,
3292
+ S: this,
3293
+ C: (f, a) => f(...a)
3294
+ });
3295
+ });
3296
+ this._updatingMetadata.set(event.handle.documentId, markingDirtyPromise);
3297
+ }
3202
3298
  _automergeDocs() {
3203
3299
  return mapValues(this._repo.handles, (handle) => ({
3204
3300
  state: handle.state,
@@ -3225,8 +3321,9 @@ var AutomergeHost = class {
3225
3321
  return this._repo.peers;
3226
3322
  }
3227
3323
  async close() {
3228
- this._storage instanceof AutomergeStorageAdapter && await this._storage.close();
3324
+ await this._storage.close();
3229
3325
  await this._clientNetwork.close();
3326
+ await this._ctx.dispose();
3230
3327
  }
3231
3328
  //
3232
3329
  // Methods for client-services.
@@ -3252,7 +3349,7 @@ var AutomergeHost = class {
3252
3349
  deviceKey
3253
3350
  }, {
3254
3351
  F: __dxlog_file16,
3255
- L: 177,
3352
+ L: 266,
3256
3353
  S: this,
3257
3354
  C: (f, a) => f(...a)
3258
3355
  });
@@ -3275,6 +3372,24 @@ _ts_decorate9([
3275
3372
  AutomergeHost = _ts_decorate9([
3276
3373
  trace6.resource()
3277
3374
  ], AutomergeHost);
3375
+ var getInlineChanges = (event) => {
3376
+ const inlineChangedObjectIds = /* @__PURE__ */ new Set();
3377
+ for (const { path } of event.patches) {
3378
+ if (path.length < 2) {
3379
+ continue;
3380
+ }
3381
+ switch (path[0]) {
3382
+ case "objects":
3383
+ if (path.length >= 2) {
3384
+ inlineChangedObjectIds.add(path[1]);
3385
+ }
3386
+ break;
3387
+ }
3388
+ }
3389
+ return [
3390
+ ...inlineChangedObjectIds
3391
+ ];
3392
+ };
3278
3393
 
3279
3394
  export {
3280
3395
  codec,
@@ -3306,4 +3421,4 @@ export {
3306
3421
  MeshNetworkAdapter,
3307
3422
  AutomergeHost
3308
3423
  };
3309
- //# sourceMappingURL=chunk-2AAHNMOH.mjs.map
3424
+ //# sourceMappingURL=chunk-UIMWNUNO.mjs.map