@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.
@@ -26,8 +26,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
26
26
  mod
27
27
  ));
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var chunk_CZ5KWO66_exports = {};
30
- __export(chunk_CZ5KWO66_exports, {
29
+ var chunk_Z3IT3GUD_exports = {};
30
+ __export(chunk_Z3IT3GUD_exports, {
31
31
  AuthExtension: () => AuthExtension,
32
32
  AuthStatus: () => AuthStatus,
33
33
  AutomergeHost: () => AutomergeHost,
@@ -57,7 +57,7 @@ __export(chunk_CZ5KWO66_exports, {
57
57
  startAfter: () => startAfter,
58
58
  valueEncoding: () => valueEncoding
59
59
  });
60
- module.exports = __toCommonJS(chunk_CZ5KWO66_exports);
60
+ module.exports = __toCommonJS(chunk_Z3IT3GUD_exports);
61
61
  var import_hypercore = require("@dxos/hypercore");
62
62
  var import_protocols = require("@dxos/protocols");
63
63
  var import_invariant = require("@dxos/invariant");
@@ -166,8 +166,10 @@ var import_teleport_extension_automerge_replicator = require("@dxos/teleport-ext
166
166
  var import_automerge = require("@dxos/automerge/automerge");
167
167
  var import_automerge_repo4 = require("@dxos/automerge/automerge-repo");
168
168
  var import_automerge_repo_storage_indexeddb = require("@dxos/automerge/automerge-repo-storage-indexeddb");
169
+ var import_context7 = require("@dxos/context");
169
170
  var import_keys8 = require("@dxos/keys");
170
171
  var import_log15 = require("@dxos/log");
172
+ var import_protocols8 = require("@dxos/protocols");
171
173
  var import_random_access_storage = require("@dxos/random-access-storage");
172
174
  var import_tracing5 = require("@dxos/tracing");
173
175
  var import_util11 = require("@dxos/util");
@@ -3086,6 +3088,34 @@ var MeshNetworkAdapter = class extends import_automerge_repo3.NetworkAdapter {
3086
3088
  return extension;
3087
3089
  }
3088
3090
  };
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
+ };
3089
3119
  function _ts_decorate9(decorators, target, key, desc) {
3090
3120
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3091
3121
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -3098,12 +3128,23 @@ function _ts_decorate9(decorators, target, key, desc) {
3098
3128
  }
3099
3129
  var __dxlog_file16 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
3100
3130
  var AutomergeHost = class {
3101
- constructor(storageDirectory) {
3131
+ constructor({ directory, metadata }) {
3132
+ this._ctx = new import_context7.Context();
3102
3133
  this._authorizedDevices = new import_util11.ComplexMap(import_keys8.PublicKey.hash);
3134
+ this._updatingMetadata = /* @__PURE__ */ new Map();
3103
3135
  this._requestedDocs = /* @__PURE__ */ new Set();
3136
+ this._metadata = metadata;
3104
3137
  this._meshNetwork = new MeshNetworkAdapter();
3105
3138
  this._clientNetwork = new LocalHostNetworkAdapter();
3106
- this._storage = storageDirectory.type === import_random_access_storage.StorageType.IDB ? new import_automerge_repo_storage_indexeddb.IndexedDBStorageAdapter(storageDirectory.path, "data") : new AutomergeStorageAdapter(storageDirectory);
3139
+ this._storage = new AutomergeStorageWrapper({
3140
+ storage: (
3141
+ // TODO(mykola): Delete specific handling of IDB storage.
3142
+ directory.type === import_random_access_storage.StorageType.IDB ? new import_automerge_repo_storage_indexeddb.IndexedDBStorageAdapter(directory.path, "data") : new AutomergeStorageAdapter(directory)
3143
+ ),
3144
+ callbacks: {
3145
+ beforeSave: (params) => this._beforeSave(params)
3146
+ }
3147
+ });
3107
3148
  this._peerId = `host-${import_keys8.PublicKey.random().toHex()}`;
3108
3149
  this._repo = new import_automerge_repo4.Repo({
3109
3150
  peerId: this._peerId,
@@ -3130,7 +3171,7 @@ var AutomergeHost = class {
3130
3171
  isRequested
3131
3172
  }, {
3132
3173
  F: __dxlog_file16,
3133
- L: 69,
3174
+ L: 96,
3134
3175
  S: this,
3135
3176
  C: (f, a) => f(...a)
3136
3177
  });
@@ -3144,7 +3185,7 @@ var AutomergeHost = class {
3144
3185
  documentId
3145
3186
  }, {
3146
3187
  F: __dxlog_file16,
3147
- L: 77,
3188
+ L: 104,
3148
3189
  S: this,
3149
3190
  C: (f, a) => f(...a)
3150
3191
  });
@@ -3159,7 +3200,7 @@ var AutomergeHost = class {
3159
3200
  documentId
3160
3201
  }, {
3161
3202
  F: __dxlog_file16,
3162
- L: 87,
3203
+ L: 114,
3163
3204
  S: this,
3164
3205
  C: (f, a) => f(...a)
3165
3206
  });
@@ -3176,7 +3217,7 @@ var AutomergeHost = class {
3176
3217
  isAuthorized
3177
3218
  }, {
3178
3219
  F: __dxlog_file16,
3179
- L: 93,
3220
+ L: 120,
3180
3221
  S: this,
3181
3222
  C: (f, a) => f(...a)
3182
3223
  });
@@ -3184,7 +3225,7 @@ var AutomergeHost = class {
3184
3225
  } catch (err) {
3185
3226
  import_log15.log.catch(err, void 0, {
3186
3227
  F: __dxlog_file16,
3187
- L: 103,
3228
+ L: 130,
3188
3229
  S: this,
3189
3230
  C: (f, a) => f(...a)
3190
3231
  });
@@ -3194,10 +3235,61 @@ var AutomergeHost = class {
3194
3235
  });
3195
3236
  this._clientNetwork.ready();
3196
3237
  this._meshNetwork.ready();
3238
+ {
3239
+ const listener = ({ handle }) => this._onDocument(handle);
3240
+ this._repo.on("document", listener);
3241
+ this._ctx.onDispose(() => {
3242
+ this._repo.off("document", listener);
3243
+ });
3244
+ }
3197
3245
  }
3198
3246
  get repo() {
3199
3247
  return this._repo;
3200
3248
  }
3249
+ async _beforeSave(path) {
3250
+ const id = path[0];
3251
+ if (this._updatingMetadata.has(id)) {
3252
+ return this._updatingMetadata.get(id);
3253
+ }
3254
+ }
3255
+ _onDocument(handle) {
3256
+ const listener = (event) => this._onUpdate(event);
3257
+ handle.on("change", listener);
3258
+ this._ctx.onDispose(() => {
3259
+ handle.off("change", listener);
3260
+ });
3261
+ }
3262
+ _onUpdate(event) {
3263
+ const spaceKey = event.doc.access?.spaceKey;
3264
+ if (!spaceKey) {
3265
+ return;
3266
+ }
3267
+ const objectIds = getInlineChanges(event);
3268
+ if (objectIds.length === 0) {
3269
+ return;
3270
+ }
3271
+ const heads = (0, import_automerge.getHeads)(event.doc);
3272
+ const lastAvailableHash = heads.at(-1);
3273
+ if (!lastAvailableHash) {
3274
+ return;
3275
+ }
3276
+ const markingDirtyPromise = Promise.all(objectIds.map(async (objectId) => {
3277
+ await (0, import_context7.cancelWithContext)(this._ctx, this._metadata.markDirty(import_protocols8.idCodec.encode({
3278
+ documentId: event.handle.documentId,
3279
+ objectId
3280
+ }), lastAvailableHash));
3281
+ })).then(() => {
3282
+ this._updatingMetadata.delete(event.handle.documentId);
3283
+ }).catch((err) => {
3284
+ !this._ctx.disposed && import_log15.log.catch(err, void 0, {
3285
+ F: __dxlog_file16,
3286
+ L: 198,
3287
+ S: this,
3288
+ C: (f, a) => f(...a)
3289
+ });
3290
+ });
3291
+ this._updatingMetadata.set(event.handle.documentId, markingDirtyPromise);
3292
+ }
3201
3293
  _automergeDocs() {
3202
3294
  return (0, import_util11.mapValues)(this._repo.handles, (handle) => ({
3203
3295
  state: handle.state,
@@ -3224,8 +3316,9 @@ var AutomergeHost = class {
3224
3316
  return this._repo.peers;
3225
3317
  }
3226
3318
  async close() {
3227
- this._storage instanceof AutomergeStorageAdapter && await this._storage.close();
3319
+ await this._storage.close();
3228
3320
  await this._clientNetwork.close();
3321
+ await this._ctx.dispose();
3229
3322
  }
3230
3323
  //
3231
3324
  // Methods for client-services.
@@ -3251,7 +3344,7 @@ var AutomergeHost = class {
3251
3344
  deviceKey
3252
3345
  }, {
3253
3346
  F: __dxlog_file16,
3254
- L: 177,
3347
+ L: 266,
3255
3348
  S: this,
3256
3349
  C: (f, a) => f(...a)
3257
3350
  });
@@ -3274,6 +3367,24 @@ _ts_decorate9([
3274
3367
  AutomergeHost = _ts_decorate9([
3275
3368
  import_tracing5.trace.resource()
3276
3369
  ], AutomergeHost);
3370
+ var getInlineChanges = (event) => {
3371
+ const inlineChangedObjectIds = /* @__PURE__ */ new Set();
3372
+ for (const { path } of event.patches) {
3373
+ if (path.length < 2) {
3374
+ continue;
3375
+ }
3376
+ switch (path[0]) {
3377
+ case "objects":
3378
+ if (path.length >= 2) {
3379
+ inlineChangedObjectIds.add(path[1]);
3380
+ }
3381
+ break;
3382
+ }
3383
+ }
3384
+ return [
3385
+ ...inlineChangedObjectIds
3386
+ ];
3387
+ };
3277
3388
  // Annotate the CommonJS export names for ESM import in node:
3278
3389
  0 && (module.exports = {
3279
3390
  AuthExtension,
@@ -3305,4 +3416,4 @@ AutomergeHost = _ts_decorate9([
3305
3416
  startAfter,
3306
3417
  valueEncoding
3307
3418
  });
3308
- //# sourceMappingURL=chunk-CZ5KWO66.cjs.map
3419
+ //# sourceMappingURL=chunk-Z3IT3GUD.cjs.map