@liveblocks/core 3.23.1-exp1 → 3.23.1-exp2

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/index.js CHANGED
@@ -6,7 +6,7 @@ var __export = (target, all) => {
6
6
 
7
7
  // src/version.ts
8
8
  var PKG_NAME = "@liveblocks/core";
9
- var PKG_VERSION = "3.23.1-exp1";
9
+ var PKG_VERSION = "3.23.1-exp2";
10
10
  var PKG_FORMAT = "esm";
11
11
 
12
12
  // src/dupe-detection.ts
@@ -1107,7 +1107,6 @@ function* nodeStreamToCompactNodes(nodes) {
1107
1107
 
1108
1108
  // src/internal.ts
1109
1109
  var kInternal = /* @__PURE__ */ Symbol();
1110
- var kStorageUpdateSource = /* @__PURE__ */ Symbol();
1111
1110
 
1112
1111
  // src/lib/position.ts
1113
1112
  var MIN_CODE = 32;
@@ -1288,6 +1287,18 @@ function asPos(str) {
1288
1287
  return isPos(str) ? str : convertToPos(str);
1289
1288
  }
1290
1289
 
1290
+ // src/crdts/StorageUpdates.ts
1291
+ var REMOTE = freeze({ origin: "remote" });
1292
+ var LOCAL_EDIT = freeze({ origin: "local", via: "edit" });
1293
+ var LOCAL_UNDO = freeze({ origin: "local", via: "undo" });
1294
+ var LOCAL_REDO = freeze({ origin: "local", via: "redo" });
1295
+ function toUpdateSource(source) {
1296
+ return source.origin === "remote" ? source : (
1297
+ // Removes `optimistic` field, which is not public
1298
+ { origin: "local", via: source.via }
1299
+ );
1300
+ }
1301
+
1291
1302
  // src/crdts/UnacknowledgedOps.ts
1292
1303
  var UnacknowledgedOps = class {
1293
1304
  // opId -> op
@@ -1496,11 +1507,14 @@ var AbstractCrdt = class {
1496
1507
  }
1497
1508
  }
1498
1509
  /** @internal */
1499
- _apply(op, _isLocal) {
1510
+ _apply(op, source) {
1500
1511
  switch (op.type) {
1501
1512
  case OpCode.DELETE_CRDT: {
1502
1513
  if (this.parent.type === "HasParent") {
1503
- return this.parent.node._detachChild(crdtAsLiveNode(this));
1514
+ return this.parent.node._detachChild(
1515
+ crdtAsLiveNode(this),
1516
+ toUpdateSource(source)
1517
+ );
1504
1518
  }
1505
1519
  return { modified: false };
1506
1520
  }
@@ -1690,8 +1704,8 @@ var LiveFile = class _LiveFile extends AbstractCrdt {
1690
1704
  throw new Error("A LiveFile node cannot have children");
1691
1705
  }
1692
1706
  /** @internal */
1693
- _apply(op, isLocal) {
1694
- return super._apply(op, isLocal);
1707
+ _apply(op, source) {
1708
+ return super._apply(op, source);
1695
1709
  }
1696
1710
  /** @internal */
1697
1711
  _toTreeNode(key) {
@@ -6911,8 +6925,8 @@ var LiveRegister = class _LiveRegister extends AbstractCrdt {
6911
6925
  throw new Error("Method not implemented.");
6912
6926
  }
6913
6927
  /** @internal */
6914
- _apply(op, isLocal) {
6915
- return super._apply(op, isLocal);
6928
+ _apply(op, source) {
6929
+ return super._apply(op, source);
6916
6930
  }
6917
6931
  /** @internal */
6918
6932
  _toTreeNode(key) {
@@ -7073,7 +7087,7 @@ var LiveList = class _LiveList extends AbstractCrdt {
7073
7087
  item._detach();
7074
7088
  }
7075
7089
  }
7076
- #applySetRemote(op) {
7090
+ #applyRemoteSet(op) {
7077
7091
  if (this._pool === void 0) {
7078
7092
  throw new Error("Can't attach child if managed pool is not present");
7079
7093
  }
@@ -7091,9 +7105,11 @@ var LiveList = class _LiveList extends AbstractCrdt {
7091
7105
  itemWithSamePosition._detach();
7092
7106
  this.#items.add(child);
7093
7107
  return {
7094
- modified: makeUpdate(this, [
7095
- setDelta(indexOfItemWithSamePosition, child)
7096
- ]),
7108
+ modified: makeUpdate(
7109
+ this,
7110
+ [setDelta(indexOfItemWithSamePosition, child)],
7111
+ REMOTE
7112
+ ),
7097
7113
  reverse: []
7098
7114
  };
7099
7115
  } else {
@@ -7104,20 +7120,22 @@ var LiveList = class _LiveList extends AbstractCrdt {
7104
7120
  setDelta(indexOfItemWithSamePosition, child)
7105
7121
  ];
7106
7122
  const deleteDelta2 = this.#detachItemAssociatedToSetOperation(
7107
- op.deletedId
7123
+ op.deletedId,
7124
+ REMOTE
7108
7125
  );
7109
7126
  if (deleteDelta2) {
7110
7127
  delta.push(deleteDelta2);
7111
7128
  }
7112
7129
  return {
7113
- modified: makeUpdate(this, delta),
7130
+ modified: makeUpdate(this, delta, REMOTE),
7114
7131
  reverse: []
7115
7132
  };
7116
7133
  }
7117
7134
  } else {
7118
7135
  const updates = [];
7119
7136
  const deleteDelta2 = this.#detachItemAssociatedToSetOperation(
7120
- op.deletedId
7137
+ op.deletedId,
7138
+ REMOTE
7121
7139
  );
7122
7140
  if (deleteDelta2) {
7123
7141
  updates.push(deleteDelta2);
@@ -7126,29 +7144,32 @@ var LiveList = class _LiveList extends AbstractCrdt {
7126
7144
  updates.push(insertDelta(this._indexOfPosition(key), child));
7127
7145
  return {
7128
7146
  reverse: [],
7129
- modified: makeUpdate(this, updates)
7147
+ modified: makeUpdate(this, updates, REMOTE)
7130
7148
  };
7131
7149
  }
7132
7150
  }
7133
- #applySetAck(op) {
7151
+ #applySetAck(op, source) {
7134
7152
  if (this._pool === void 0) {
7135
7153
  throw new Error("Can't attach child if managed pool is not present");
7136
7154
  }
7137
7155
  const delta = [];
7138
- const deletedDelta = this.#detachItemAssociatedToSetOperation(op.deletedId);
7156
+ const deletedDelta = this.#detachItemAssociatedToSetOperation(
7157
+ op.deletedId,
7158
+ source
7159
+ );
7139
7160
  if (deletedDelta) {
7140
7161
  delta.push(deletedDelta);
7141
7162
  }
7142
7163
  const unacknowledgedOpId = this.#unacknowledgedSetOpIdAt(op.parentKey);
7143
7164
  if (unacknowledgedOpId !== void 0 && unacknowledgedOpId !== op.opId) {
7144
- return delta.length === 0 ? { modified: false } : { modified: makeUpdate(this, delta), reverse: [] };
7165
+ return delta.length === 0 ? { modified: false } : { modified: makeUpdate(this, delta, source), reverse: [] };
7145
7166
  }
7146
7167
  const indexOfItemWithSamePosition = this._indexOfPosition(op.parentKey);
7147
7168
  const existingItem = this.#items.find((item) => item._id === op.id);
7148
7169
  if (existingItem !== void 0) {
7149
7170
  if (existingItem._parentKey === op.parentKey) {
7150
7171
  return {
7151
- modified: delta.length > 0 ? makeUpdate(this, delta) : false,
7172
+ modified: delta.length > 0 ? makeUpdate(this, delta, source) : false,
7152
7173
  reverse: []
7153
7174
  };
7154
7175
  }
@@ -7166,7 +7187,7 @@ var LiveList = class _LiveList extends AbstractCrdt {
7166
7187
  delta.push(moveDelta(prevIndex, newIndex, existingItem));
7167
7188
  }
7168
7189
  return {
7169
- modified: delta.length > 0 ? makeUpdate(this, delta) : false,
7190
+ modified: delta.length > 0 ? makeUpdate(this, delta, source) : false,
7170
7191
  reverse: []
7171
7192
  };
7172
7193
  } else {
@@ -7176,11 +7197,15 @@ var LiveList = class _LiveList extends AbstractCrdt {
7176
7197
  this.#implicitlyDeletedItems.delete(orphan);
7177
7198
  const recreatedItemIndex = this.#insert(orphan);
7178
7199
  return {
7179
- modified: makeUpdate(this, [
7180
- // If there is an item at this position, update is a set, else it's an insert
7181
- indexOfItemWithSamePosition === -1 ? insertDelta(recreatedItemIndex, orphan) : setDelta(recreatedItemIndex, orphan),
7182
- ...delta
7183
- ]),
7200
+ modified: makeUpdate(
7201
+ this,
7202
+ [
7203
+ // If there is an item at this position, update is a set, else it's an insert
7204
+ indexOfItemWithSamePosition === -1 ? insertDelta(recreatedItemIndex, orphan) : setDelta(recreatedItemIndex, orphan),
7205
+ ...delta
7206
+ ],
7207
+ source
7208
+ ),
7184
7209
  reverse: []
7185
7210
  };
7186
7211
  } else {
@@ -7195,11 +7220,15 @@ var LiveList = class _LiveList extends AbstractCrdt {
7195
7220
  op.parentKey
7196
7221
  );
7197
7222
  return {
7198
- modified: makeUpdate(this, [
7199
- // If there is an item at this position, update is a set, else it's an insert
7200
- indexOfItemWithSamePosition === -1 ? insertDelta(newIndex, newItem) : setDelta(newIndex, newItem),
7201
- ...delta
7202
- ]),
7223
+ modified: makeUpdate(
7224
+ this,
7225
+ [
7226
+ // If there is an item at this position, update is a set, else it's an insert
7227
+ indexOfItemWithSamePosition === -1 ? insertDelta(newIndex, newItem) : setDelta(newIndex, newItem),
7228
+ ...delta
7229
+ ],
7230
+ source
7231
+ ),
7203
7232
  reverse: []
7204
7233
  };
7205
7234
  }
@@ -7208,7 +7237,7 @@ var LiveList = class _LiveList extends AbstractCrdt {
7208
7237
  /**
7209
7238
  * Returns the update delta of the deletion or null
7210
7239
  */
7211
- #detachItemAssociatedToSetOperation(deletedId) {
7240
+ #detachItemAssociatedToSetOperation(deletedId, source) {
7212
7241
  if (deletedId === void 0 || this._pool === void 0) {
7213
7242
  return null;
7214
7243
  }
@@ -7216,7 +7245,7 @@ var LiveList = class _LiveList extends AbstractCrdt {
7216
7245
  if (deletedItem === void 0) {
7217
7246
  return null;
7218
7247
  }
7219
- const result = this._detachChild(deletedItem);
7248
+ const result = this._detachChild(deletedItem, source);
7220
7249
  if (result.modified === false) {
7221
7250
  return null;
7222
7251
  }
@@ -7234,10 +7263,11 @@ var LiveList = class _LiveList extends AbstractCrdt {
7234
7263
  const { newItem, newIndex } = this.#createAttachItemAndSort(op, key);
7235
7264
  const bumpDeltas = this.#bumpUnackedPushesAbove(key);
7236
7265
  return {
7237
- modified: makeUpdate(this, [
7238
- insertDelta(newIndex, newItem),
7239
- ...bumpDeltas
7240
- ]),
7266
+ modified: makeUpdate(
7267
+ this,
7268
+ [insertDelta(newIndex, newItem), ...bumpDeltas],
7269
+ REMOTE
7270
+ ),
7241
7271
  reverse: []
7242
7272
  };
7243
7273
  }
@@ -7318,7 +7348,7 @@ var LiveList = class _LiveList extends AbstractCrdt {
7318
7348
  }
7319
7349
  return deltas;
7320
7350
  }
7321
- #applyInsertAck(op) {
7351
+ #applyInsertAck(op, source) {
7322
7352
  const existingItem = this.#items.find((item) => item._id === op.id);
7323
7353
  const key = asPos(op.parentKey);
7324
7354
  const itemIndexAtPosition = this._indexOfPosition(key);
@@ -7340,9 +7370,11 @@ var LiveList = class _LiveList extends AbstractCrdt {
7340
7370
  return { modified: false };
7341
7371
  }
7342
7372
  return {
7343
- modified: makeUpdate(this, [
7344
- moveDelta(oldPositionIndex, newIndex, existingItem)
7345
- ]),
7373
+ modified: makeUpdate(
7374
+ this,
7375
+ [moveDelta(oldPositionIndex, newIndex, existingItem)],
7376
+ source
7377
+ ),
7346
7378
  reverse: []
7347
7379
  };
7348
7380
  }
@@ -7354,7 +7386,7 @@ var LiveList = class _LiveList extends AbstractCrdt {
7354
7386
  this.#insert(orphan);
7355
7387
  const newIndex = this._indexOfPosition(key);
7356
7388
  return {
7357
- modified: makeUpdate(this, [insertDelta(newIndex, orphan)]),
7389
+ modified: makeUpdate(this, [insertDelta(newIndex, orphan)], source),
7358
7390
  reverse: []
7359
7391
  };
7360
7392
  } else {
@@ -7363,13 +7395,13 @@ var LiveList = class _LiveList extends AbstractCrdt {
7363
7395
  }
7364
7396
  const { newItem, newIndex } = this.#createAttachItemAndSort(op, key);
7365
7397
  return {
7366
- modified: makeUpdate(this, [insertDelta(newIndex, newItem)]),
7398
+ modified: makeUpdate(this, [insertDelta(newIndex, newItem)], source),
7367
7399
  reverse: []
7368
7400
  };
7369
7401
  }
7370
7402
  }
7371
7403
  }
7372
- #applyInsertUndoRedo(op) {
7404
+ #applyLocalInsert(op, source) {
7373
7405
  const { id, parentKey: key } = op;
7374
7406
  const child = creationOpToLiveNode(op);
7375
7407
  if (this._pool?.getNode(id) !== void 0) {
@@ -7388,11 +7420,11 @@ var LiveList = class _LiveList extends AbstractCrdt {
7388
7420
  this.#insert(child);
7389
7421
  const newIndex = this._indexOfPosition(newKey);
7390
7422
  return {
7391
- modified: makeUpdate(this, [insertDelta(newIndex, child)]),
7423
+ modified: makeUpdate(this, [insertDelta(newIndex, child)], source),
7392
7424
  reverse: [{ type: OpCode.DELETE_CRDT, id }]
7393
7425
  };
7394
7426
  }
7395
- #applySetUndoRedo(op) {
7427
+ #applyLocalSet(op, source) {
7396
7428
  const { id, parentKey: key } = op;
7397
7429
  const child = creationOpToLiveNode(op);
7398
7430
  if (this._pool?.getNode(id) !== void 0) {
@@ -7414,46 +7446,48 @@ var LiveList = class _LiveList extends AbstractCrdt {
7414
7446
  );
7415
7447
  const delta = [setDelta(indexOfItemWithSameKey, child)];
7416
7448
  const deletedDelta = this.#detachItemAssociatedToSetOperation(
7417
- op.deletedId
7449
+ op.deletedId,
7450
+ source
7418
7451
  );
7419
7452
  if (deletedDelta) {
7420
7453
  delta.push(deletedDelta);
7421
7454
  }
7422
7455
  return {
7423
- modified: makeUpdate(this, delta),
7456
+ modified: makeUpdate(this, delta, source),
7424
7457
  reverse
7425
7458
  };
7426
7459
  } else {
7427
7460
  this.#insert(child);
7428
- this.#detachItemAssociatedToSetOperation(op.deletedId);
7461
+ this.#detachItemAssociatedToSetOperation(op.deletedId, source);
7429
7462
  const newIndex = this._indexOfPosition(newKey);
7430
7463
  return {
7431
7464
  reverse: [{ type: OpCode.DELETE_CRDT, id }],
7432
- modified: makeUpdate(this, [insertDelta(newIndex, child)])
7465
+ modified: makeUpdate(this, [insertDelta(newIndex, child)], source)
7433
7466
  };
7434
7467
  }
7435
7468
  }
7436
7469
  /** @internal */
7437
- _attachChild(op, source) {
7470
+ _attachChild(op, opSource) {
7471
+ const source = toUpdateSource(opSource);
7438
7472
  if (this._pool === void 0) {
7439
7473
  throw new Error("Can't attach child if managed pool is not present");
7440
7474
  }
7441
7475
  let result;
7442
7476
  if (op.intent === "set") {
7443
- if (source === 1 /* THEIRS */) {
7444
- result = this.#applySetRemote(op);
7445
- } else if (source === 2 /* OURS */) {
7446
- result = this.#applySetAck(op);
7477
+ if (opSource.origin === "remote") {
7478
+ result = this.#applyRemoteSet(op);
7479
+ } else if (!opSource.optimistic) {
7480
+ result = this.#applySetAck(op, source);
7447
7481
  } else {
7448
- result = this.#applySetUndoRedo(op);
7482
+ result = this.#applyLocalSet(op, source);
7449
7483
  }
7450
7484
  } else {
7451
- if (source === 1 /* THEIRS */) {
7485
+ if (opSource.origin === "remote") {
7452
7486
  result = this.#applyRemoteInsert(op);
7453
- } else if (source === 2 /* OURS */) {
7454
- result = this.#applyInsertAck(op);
7487
+ } else if (!opSource.optimistic) {
7488
+ result = this.#applyInsertAck(op, source);
7455
7489
  } else {
7456
- result = this.#applyInsertUndoRedo(op);
7490
+ result = this.#applyLocalInsert(op, source);
7457
7491
  }
7458
7492
  }
7459
7493
  if (result.modified !== false) {
@@ -7462,7 +7496,7 @@ var LiveList = class _LiveList extends AbstractCrdt {
7462
7496
  return result;
7463
7497
  }
7464
7498
  /** @internal */
7465
- _detachChild(child) {
7499
+ _detachChild(child, source) {
7466
7500
  if (child) {
7467
7501
  const parentKey = nn(child._parentKey);
7468
7502
  const reverse = child._toOps(nn(this._id), parentKey);
@@ -7477,19 +7511,23 @@ var LiveList = class _LiveList extends AbstractCrdt {
7477
7511
  this.invalidate();
7478
7512
  child._detach();
7479
7513
  return {
7480
- modified: makeUpdate(this, [deleteDelta(indexToDelete, previousNode)]),
7514
+ modified: makeUpdate(
7515
+ this,
7516
+ [deleteDelta(indexToDelete, previousNode)],
7517
+ source
7518
+ ),
7481
7519
  reverse
7482
7520
  };
7483
7521
  }
7484
7522
  return { modified: false };
7485
7523
  }
7486
- #applySetChildKeyRemote(newKey, child) {
7524
+ #applyRemoteSetChildKey(newKey, child) {
7487
7525
  if (this.#implicitlyDeletedItems.has(child)) {
7488
7526
  this.#implicitlyDeletedItems.delete(child);
7489
7527
  child._setParentLink(this, newKey);
7490
7528
  const newIndex = this.#insert(child);
7491
7529
  return {
7492
- modified: makeUpdate(this, [insertDelta(newIndex, child)]),
7530
+ modified: makeUpdate(this, [insertDelta(newIndex, child)], REMOTE),
7493
7531
  reverse: []
7494
7532
  };
7495
7533
  }
@@ -7510,7 +7548,11 @@ var LiveList = class _LiveList extends AbstractCrdt {
7510
7548
  };
7511
7549
  }
7512
7550
  return {
7513
- modified: makeUpdate(this, [moveDelta(previousIndex, newIndex, child)]),
7551
+ modified: makeUpdate(
7552
+ this,
7553
+ [moveDelta(previousIndex, newIndex, child)],
7554
+ REMOTE
7555
+ ),
7514
7556
  reverse: []
7515
7557
  };
7516
7558
  } else {
@@ -7527,12 +7569,16 @@ var LiveList = class _LiveList extends AbstractCrdt {
7527
7569
  };
7528
7570
  }
7529
7571
  return {
7530
- modified: makeUpdate(this, [moveDelta(previousIndex, newIndex, child)]),
7572
+ modified: makeUpdate(
7573
+ this,
7574
+ [moveDelta(previousIndex, newIndex, child)],
7575
+ REMOTE
7576
+ ),
7531
7577
  reverse: []
7532
7578
  };
7533
7579
  }
7534
7580
  }
7535
- #applySetChildKeyAck(newKey, child) {
7581
+ #applySetChildKeyAck(newKey, child, source) {
7536
7582
  const previousKey = nn(child._parentKey);
7537
7583
  if (this.#implicitlyDeletedItems.has(child)) {
7538
7584
  const existingItemIndex = this._indexOfPosition(newKey);
@@ -7551,7 +7597,7 @@ var LiveList = class _LiveList extends AbstractCrdt {
7551
7597
  child._setParentLink(this, newKey);
7552
7598
  const newIndex = this.#insert(child);
7553
7599
  return {
7554
- modified: makeUpdate(this, [insertDelta(newIndex, child)]),
7600
+ modified: makeUpdate(this, [insertDelta(newIndex, child)], source),
7555
7601
  reverse: []
7556
7602
  };
7557
7603
  } else {
@@ -7579,15 +7625,17 @@ var LiveList = class _LiveList extends AbstractCrdt {
7579
7625
  };
7580
7626
  } else {
7581
7627
  return {
7582
- modified: makeUpdate(this, [
7583
- moveDelta(previousIndex, newIndex, child)
7584
- ]),
7628
+ modified: makeUpdate(
7629
+ this,
7630
+ [moveDelta(previousIndex, newIndex, child)],
7631
+ source
7632
+ ),
7585
7633
  reverse: []
7586
7634
  };
7587
7635
  }
7588
7636
  }
7589
7637
  }
7590
- #applySetChildKeyUndoRedo(newKey, child) {
7638
+ #applyLocalSetChildKey(newKey, child, source) {
7591
7639
  const previousKey = nn(child._parentKey);
7592
7640
  const previousIndex = this.#items.findIndex((item) => item === child);
7593
7641
  const existingItemIndex = this._indexOfPosition(newKey);
@@ -7606,7 +7654,11 @@ var LiveList = class _LiveList extends AbstractCrdt {
7606
7654
  };
7607
7655
  }
7608
7656
  return {
7609
- modified: makeUpdate(this, [moveDelta(previousIndex, newIndex, child)]),
7657
+ modified: makeUpdate(
7658
+ this,
7659
+ [moveDelta(previousIndex, newIndex, child)],
7660
+ source
7661
+ ),
7610
7662
  reverse: [
7611
7663
  {
7612
7664
  type: OpCode.SET_PARENT_KEY,
@@ -7617,18 +7669,19 @@ var LiveList = class _LiveList extends AbstractCrdt {
7617
7669
  };
7618
7670
  }
7619
7671
  /** @internal */
7620
- _setChildKey(newKey, child, source) {
7621
- if (source === 1 /* THEIRS */) {
7622
- return this.#applySetChildKeyRemote(newKey, child);
7623
- } else if (source === 2 /* OURS */) {
7624
- return this.#applySetChildKeyAck(newKey, child);
7672
+ _setChildKey(newKey, child, opSource) {
7673
+ const source = toUpdateSource(opSource);
7674
+ if (opSource.origin === "remote") {
7675
+ return this.#applyRemoteSetChildKey(newKey, child);
7676
+ } else if (!opSource.optimistic) {
7677
+ return this.#applySetChildKeyAck(newKey, child, source);
7625
7678
  } else {
7626
- return this.#applySetChildKeyUndoRedo(newKey, child);
7679
+ return this.#applyLocalSetChildKey(newKey, child, source);
7627
7680
  }
7628
7681
  }
7629
7682
  /** @internal */
7630
- _apply(op, isLocal) {
7631
- return super._apply(op, isLocal);
7683
+ _apply(op, source) {
7684
+ return super._apply(op, source);
7632
7685
  }
7633
7686
  /** @internal */
7634
7687
  _serialize() {
@@ -7689,7 +7742,7 @@ var LiveList = class _LiveList extends AbstractCrdt {
7689
7742
  intent === "push" ? addIntentToRootOp(ops, "push") : ops,
7690
7743
  [{ type: OpCode.DELETE_CRDT, id }],
7691
7744
  /* @__PURE__ */ new Map([
7692
- [this._id, makeUpdate(this, [insertDelta(index, value)])]
7745
+ [this._id, makeUpdate(this, [insertDelta(index, value)], LOCAL_EDIT)]
7693
7746
  ])
7694
7747
  );
7695
7748
  }
@@ -7730,7 +7783,10 @@ var LiveList = class _LiveList extends AbstractCrdt {
7730
7783
  this.#updateItemPositionAt(index, position);
7731
7784
  if (this._pool && this._id) {
7732
7785
  const storageUpdates = /* @__PURE__ */ new Map([
7733
- [this._id, makeUpdate(this, [moveDelta(index, targetIndex, item)])]
7786
+ [
7787
+ this._id,
7788
+ makeUpdate(this, [moveDelta(index, targetIndex, item)], LOCAL_EDIT)
7789
+ ]
7734
7790
  ]);
7735
7791
  this._pool.dispatch(
7736
7792
  [
@@ -7773,7 +7829,7 @@ var LiveList = class _LiveList extends AbstractCrdt {
7773
7829
  const storageUpdates = /* @__PURE__ */ new Map();
7774
7830
  storageUpdates.set(
7775
7831
  nn(this._id),
7776
- makeUpdate(this, [deleteDelta(index, item)])
7832
+ makeUpdate(this, [deleteDelta(index, item)], LOCAL_EDIT)
7777
7833
  );
7778
7834
  this._pool.dispatch(
7779
7835
  [
@@ -7813,7 +7869,10 @@ var LiveList = class _LiveList extends AbstractCrdt {
7813
7869
  this.#items.clear();
7814
7870
  this.invalidate();
7815
7871
  const storageUpdates = /* @__PURE__ */ new Map();
7816
- storageUpdates.set(nn(this._id), makeUpdate(this, updateDelta));
7872
+ storageUpdates.set(
7873
+ nn(this._id),
7874
+ makeUpdate(this, updateDelta, LOCAL_EDIT)
7875
+ );
7817
7876
  this._pool.dispatch(ops, reverseOps, storageUpdates);
7818
7877
  } else {
7819
7878
  for (const item of this.#items) {
@@ -7843,7 +7902,10 @@ var LiveList = class _LiveList extends AbstractCrdt {
7843
7902
  const id = this._pool.generateId();
7844
7903
  value._attach(id, this._pool);
7845
7904
  const storageUpdates = /* @__PURE__ */ new Map();
7846
- storageUpdates.set(this._id, makeUpdate(this, [setDelta(index, value)]));
7905
+ storageUpdates.set(
7906
+ this._id,
7907
+ makeUpdate(this, [setDelta(index, value)], LOCAL_EDIT)
7908
+ );
7847
7909
  const ops = addIntentToRootOp(
7848
7910
  value._toOpsWithOpId(this._id, position, this._pool),
7849
7911
  "set",
@@ -8016,11 +8078,12 @@ var LiveList = class _LiveList extends AbstractCrdt {
8016
8078
  );
8017
8079
  }
8018
8080
  };
8019
- function makeUpdate(liveList, deltaUpdates) {
8081
+ function makeUpdate(liveList, deltaUpdates, source) {
8020
8082
  return {
8021
8083
  node: liveList,
8022
8084
  type: "LiveList",
8023
- updates: deltaUpdates
8085
+ updates: deltaUpdates,
8086
+ source
8024
8087
  };
8025
8088
  }
8026
8089
  function setDelta(index, item) {
@@ -8139,7 +8202,9 @@ var LiveMap = class _LiveMap extends AbstractCrdt {
8139
8202
  if (this._pool.getNode(id) !== void 0) {
8140
8203
  return { modified: false };
8141
8204
  }
8142
- if (source === 2 /* OURS */) {
8205
+ if (source.origin === "remote") {
8206
+ this.#unacknowledgedSet.delete(key);
8207
+ } else if (!source.optimistic) {
8143
8208
  const lastUpdateOpId = this.#unacknowledgedSet.get(key);
8144
8209
  if (lastUpdateOpId === opId) {
8145
8210
  this.#unacknowledgedSet.delete(key);
@@ -8147,8 +8212,6 @@ var LiveMap = class _LiveMap extends AbstractCrdt {
8147
8212
  } else if (lastUpdateOpId !== void 0) {
8148
8213
  return { modified: false };
8149
8214
  }
8150
- } else if (source === 1 /* THEIRS */) {
8151
- this.#unacknowledgedSet.delete(key);
8152
8215
  }
8153
8216
  const previousValue = this.#map.get(key);
8154
8217
  let reverse;
@@ -8167,7 +8230,8 @@ var LiveMap = class _LiveMap extends AbstractCrdt {
8167
8230
  modified: {
8168
8231
  node: this,
8169
8232
  type: "LiveMap",
8170
- updates: { [key]: { type: "update" } }
8233
+ updates: { [key]: { type: "update" } },
8234
+ source: toUpdateSource(source)
8171
8235
  },
8172
8236
  reverse
8173
8237
  };
@@ -8180,7 +8244,7 @@ var LiveMap = class _LiveMap extends AbstractCrdt {
8180
8244
  }
8181
8245
  }
8182
8246
  /** @internal */
8183
- _detachChild(child) {
8247
+ _detachChild(child, source) {
8184
8248
  const id = nn(this._id);
8185
8249
  const parentKey = nn(child._parentKey);
8186
8250
  const reverse = child._toOps(id, parentKey);
@@ -8199,7 +8263,8 @@ var LiveMap = class _LiveMap extends AbstractCrdt {
8199
8263
  type: "delete",
8200
8264
  deletedItem: liveNodeToLson(child)
8201
8265
  }
8202
- }
8266
+ },
8267
+ source
8203
8268
  };
8204
8269
  return { modified: storageUpdate, reverse };
8205
8270
  }
@@ -8248,7 +8313,8 @@ var LiveMap = class _LiveMap extends AbstractCrdt {
8248
8313
  storageUpdates.set(this._id, {
8249
8314
  node: this,
8250
8315
  type: "LiveMap",
8251
- updates: { [key]: { type: "update" } }
8316
+ updates: { [key]: { type: "update" } },
8317
+ source: LOCAL_EDIT
8252
8318
  });
8253
8319
  const ops = item._toOpsWithOpId(this._id, key, this._pool);
8254
8320
  this.#unacknowledgedSet.set(key, nn(ops[0].opId));
@@ -8297,7 +8363,8 @@ var LiveMap = class _LiveMap extends AbstractCrdt {
8297
8363
  type: "delete",
8298
8364
  deletedItem: liveNodeToLson(item)
8299
8365
  }
8300
- }
8366
+ },
8367
+ source: LOCAL_EDIT
8301
8368
  });
8302
8369
  this._pool.dispatch(
8303
8370
  [
@@ -8662,7 +8729,7 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8662
8729
  }
8663
8730
  return { modified: false };
8664
8731
  }
8665
- if (source === 0 /* LOCAL */) {
8732
+ if (source.origin === "local" && source.optimistic) {
8666
8733
  this.#unackedOpsByKey.set(key, nn(opId));
8667
8734
  } else if (this.#unackedOpsByKey.get(key) === void 0) {
8668
8735
  } else if (this.#unackedOpsByKey.get(key) === opId) {
@@ -8700,12 +8767,13 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8700
8767
  modified: {
8701
8768
  node: this,
8702
8769
  type: "LiveObject",
8703
- updates: { [key]: { type: "update" } }
8770
+ updates: { [key]: { type: "update" } },
8771
+ source: toUpdateSource(source)
8704
8772
  }
8705
8773
  };
8706
8774
  }
8707
8775
  /** @internal */
8708
- _detachChild(child) {
8776
+ _detachChild(child, source) {
8709
8777
  if (child) {
8710
8778
  const id = nn(this._id);
8711
8779
  const parentKey = nn(child._parentKey);
@@ -8723,7 +8791,8 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8723
8791
  type: "LiveObject",
8724
8792
  updates: {
8725
8793
  [parentKey]: { type: "delete", deletedItem }
8726
- }
8794
+ },
8795
+ source
8727
8796
  };
8728
8797
  return { modified: storageUpdate, reverse };
8729
8798
  }
@@ -8739,13 +8808,13 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8739
8808
  }
8740
8809
  }
8741
8810
  /** @internal */
8742
- _apply(op, isLocal) {
8811
+ _apply(op, source) {
8743
8812
  if (op.type === OpCode.UPDATE_OBJECT) {
8744
- return this.#applyUpdate(op, isLocal);
8813
+ return this.#applyUpdate(op, source);
8745
8814
  } else if (op.type === OpCode.DELETE_OBJECT_KEY) {
8746
- return this.#applyDeleteObjectKey(op, isLocal);
8815
+ return this.#applyDeleteObjectKey(op, source);
8747
8816
  }
8748
- return super._apply(op, isLocal);
8817
+ return super._apply(op, source);
8749
8818
  }
8750
8819
  /** @internal */
8751
8820
  _serialize() {
@@ -8769,7 +8838,7 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8769
8838
  };
8770
8839
  }
8771
8840
  }
8772
- #applyUpdate(op, isLocal) {
8841
+ #applyUpdate(op, source) {
8773
8842
  let isModified = false;
8774
8843
  const id = nn(this._id);
8775
8844
  const reverse = [];
@@ -8797,7 +8866,7 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8797
8866
  if (value === void 0) {
8798
8867
  continue;
8799
8868
  }
8800
- if (isLocal) {
8869
+ if (source.origin === "local" && source.optimistic) {
8801
8870
  this.#unackedOpsByKey.set(key, nn(op.opId));
8802
8871
  } else if (this.#unackedOpsByKey.get(key) === void 0) {
8803
8872
  isModified = true;
@@ -8824,18 +8893,19 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8824
8893
  modified: {
8825
8894
  node: this,
8826
8895
  type: "LiveObject",
8827
- updates: updateDelta
8896
+ updates: updateDelta,
8897
+ source: toUpdateSource(source)
8828
8898
  },
8829
8899
  reverse
8830
8900
  } : { modified: false };
8831
8901
  }
8832
- #applyDeleteObjectKey(op, isLocal) {
8902
+ #applyDeleteObjectKey(op, source) {
8833
8903
  const key = op.key;
8834
8904
  const oldValue = this.#synced.get(key);
8835
8905
  if (oldValue === void 0) {
8836
8906
  return { modified: false };
8837
8907
  }
8838
- if (!isLocal && this.#unackedOpsByKey.get(key) !== void 0) {
8908
+ if (!(source.origin === "local" && source.optimistic) && this.#unackedOpsByKey.get(key) !== void 0) {
8839
8909
  return { modified: false };
8840
8910
  }
8841
8911
  const id = nn(this._id);
@@ -8861,7 +8931,8 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8861
8931
  type: "LiveObject",
8862
8932
  updates: {
8863
8933
  [op.key]: { type: "delete", deletedItem: oldValue }
8864
- }
8934
+ },
8935
+ source: toUpdateSource(source)
8865
8936
  },
8866
8937
  reverse
8867
8938
  };
@@ -8907,7 +8978,8 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8907
8978
  updates: {
8908
8979
  ...existing?.updates,
8909
8980
  [key]: { type: "update" }
8910
- }
8981
+ },
8982
+ source: LOCAL_EDIT
8911
8983
  });
8912
8984
  this._pool.dispatch(ops, reverse, storageUpdates);
8913
8985
  }
@@ -8941,7 +9013,8 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8941
9013
  type: "delete",
8942
9014
  deletedItem: oldValue2
8943
9015
  }
8944
- }
9016
+ },
9017
+ source: LOCAL_EDIT
8945
9018
  });
8946
9019
  return [[], [], storageUpdates2];
8947
9020
  }
@@ -8989,7 +9062,8 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8989
9062
  type: "LiveObject",
8990
9063
  updates: {
8991
9064
  [key]: { type: "delete", deletedItem: oldValue }
8992
- }
9065
+ },
9066
+ source: LOCAL_EDIT
8993
9067
  });
8994
9068
  return [ops, reverse, storageUpdates];
8995
9069
  }
@@ -9127,7 +9201,8 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
9127
9201
  storageUpdates.set(this._id, {
9128
9202
  node: this,
9129
9203
  type: "LiveObject",
9130
- updates: updateDelta
9204
+ updates: updateDelta,
9205
+ source: LOCAL_EDIT
9131
9206
  });
9132
9207
  this._pool.dispatch(ops, reverseOps, storageUpdates);
9133
9208
  }
@@ -9289,6 +9364,31 @@ function clipRange(index, length, contentLength) {
9289
9364
  );
9290
9365
  return { index: clippedIndex, length: clippedEnd - clippedIndex };
9291
9366
  }
9367
+ function isInSurrogatePair(text, index) {
9368
+ const previous = text.charCodeAt(index - 1);
9369
+ const next = text.charCodeAt(index);
9370
+ return previous >= 55296 && previous <= 56319 && next >= 56320 && next <= 57343;
9371
+ }
9372
+ function clipIndexToCodePointBoundary(text, index) {
9373
+ const clippedIndex = Math.max(0, Math.min(index, text.length));
9374
+ return isInSurrogatePair(text, clippedIndex) ? clippedIndex - 1 : clippedIndex;
9375
+ }
9376
+ function clipRangeToCodePointBoundaries(text, index, length) {
9377
+ const clipped = clipRange(index, length, text.length);
9378
+ if (clipped.length === 0) {
9379
+ return {
9380
+ index: clipIndexToCodePointBoundary(text, clipped.index),
9381
+ length: 0
9382
+ };
9383
+ }
9384
+ const clippedEnd = clipped.index + clipped.length;
9385
+ const normalizedIndex = isInSurrogatePair(text, clipped.index) ? clipped.index - 1 : clipped.index;
9386
+ const normalizedEnd = isInSurrogatePair(text, clippedEnd) ? clippedEnd + 1 : clippedEnd;
9387
+ return {
9388
+ index: normalizedIndex,
9389
+ length: normalizedEnd - normalizedIndex
9390
+ };
9391
+ }
9292
9392
  function applyInsert(segments, index, text, attributes) {
9293
9393
  if (text.length === 0) {
9294
9394
  return normalizeSegments(segments);
@@ -9770,21 +9870,21 @@ var LiveText = class _LiveText extends AbstractCrdt {
9770
9870
  throw new Error("LiveText cannot contain child nodes");
9771
9871
  }
9772
9872
  /** @internal */
9773
- _apply(op, isLocal) {
9873
+ _apply(op, source) {
9774
9874
  if (op.type !== OpCode.UPDATE_TEXT) {
9775
- return super._apply(op, isLocal);
9875
+ return super._apply(op, source);
9776
9876
  }
9777
- if (isLocal) {
9778
- return this.#applyLocal(op);
9877
+ if (source.origin === "local" && source.optimistic) {
9878
+ return this.#applyLocal(op, toUpdateSource(source));
9779
9879
  }
9780
9880
  if (op.opId !== void 0 && op.opId === this.#inFlightOpId) {
9781
- return this.#applyAck(op);
9881
+ return this.#applyAck(op, toUpdateSource(source));
9782
9882
  }
9783
9883
  if (op.opId !== void 0 && this.#acceptedOps.some((entry) => entry.opId === op.opId)) {
9784
9884
  this.#version = Math.max(this.#version, op.version ?? op.baseVersion + 1);
9785
9885
  return { modified: false };
9786
9886
  }
9787
- return this.#applyRemote(op);
9887
+ return this.#applyRemote(op, toUpdateSource(source));
9788
9888
  }
9789
9889
  /**
9790
9890
  * Inserts text at the given index.
@@ -9800,7 +9900,7 @@ var LiveText = class _LiveText extends AbstractCrdt {
9800
9900
  * text.insert(0, "Say: ", { italic: true });
9801
9901
  */
9802
9902
  insert(index, text, attributes) {
9803
- const clippedIndex = Math.max(0, Math.min(index, this.length));
9903
+ const clippedIndex = clipIndexToCodePointBoundary(this.toString(), index);
9804
9904
  this.#dispatch([{ type: "insert", index: clippedIndex, text, attributes }]);
9805
9905
  }
9806
9906
  /**
@@ -9811,7 +9911,11 @@ var LiveText = class _LiveText extends AbstractCrdt {
9811
9911
  * text.delete(5, 6); // "Hello"
9812
9912
  */
9813
9913
  delete(index, length) {
9814
- const clipped = clipRange(index, length, this.length);
9914
+ const clipped = clipRangeToCodePointBoundaries(
9915
+ this.toString(),
9916
+ index,
9917
+ length
9918
+ );
9815
9919
  if (clipped.length === 0) {
9816
9920
  return;
9817
9921
  }
@@ -9827,7 +9931,11 @@ var LiveText = class _LiveText extends AbstractCrdt {
9827
9931
  * text.replace(0, 5, "Hi"); // "Hi world"
9828
9932
  */
9829
9933
  replace(index, length, text, attributes) {
9830
- const clipped = clipRange(index, length, this.length);
9934
+ const clipped = clipRangeToCodePointBoundaries(
9935
+ this.toString(),
9936
+ index,
9937
+ length
9938
+ );
9831
9939
  const ops = [];
9832
9940
  if (clipped.length > 0) {
9833
9941
  ops.push({
@@ -9920,7 +10028,11 @@ var LiveText = class _LiveText extends AbstractCrdt {
9920
10028
  * text.format(0, 5, { bold: null });
9921
10029
  */
9922
10030
  format(index, length, attributes) {
9923
- const clipped = clipRange(index, length, this.length);
10031
+ const clipped = clipRangeToCodePointBoundaries(
10032
+ this.toString(),
10033
+ index,
10034
+ length
10035
+ );
9924
10036
  if (clipped.length === 0) {
9925
10037
  return;
9926
10038
  }
@@ -9954,7 +10066,8 @@ var LiveText = class _LiveText extends AbstractCrdt {
9954
10066
  type: "LiveText",
9955
10067
  node: this,
9956
10068
  version: this.#version,
9957
- updates: changes
10069
+ updates: changes,
10070
+ source: LOCAL_EDIT
9958
10071
  }
9959
10072
  ]
9960
10073
  ]);
@@ -9984,7 +10097,7 @@ var LiveText = class _LiveText extends AbstractCrdt {
9984
10097
  * A local replay of an existing wire op: an undo/redo frame, or an
9985
10098
  * unacknowledged op re-sent after a reconnect.
9986
10099
  */
9987
- #applyLocal(op) {
10100
+ #applyLocal(op, source) {
9988
10101
  const mutableOp = op;
9989
10102
  if (op.opId !== void 0 && op.opId === this.#inFlightOpId) {
9990
10103
  this.#inFlightOps = [...this.#inFlightOps, ...this.#queuedOps];
@@ -10020,12 +10133,13 @@ var LiveText = class _LiveText extends AbstractCrdt {
10020
10133
  type: "LiveText",
10021
10134
  node: this,
10022
10135
  version: this.#version,
10023
- updates: changes
10136
+ updates: changes,
10137
+ source
10024
10138
  }
10025
10139
  };
10026
10140
  }
10027
10141
  /** Server acknowledgement of our in-flight op. */
10028
- #applyAck(op) {
10142
+ #applyAck(op, source) {
10029
10143
  const ackedVersion = op.version ?? Math.max(this.#version, op.baseVersion + 1);
10030
10144
  const predicted = this.#inFlightOps;
10031
10145
  const opId = this.#inFlightOpId;
@@ -10047,7 +10161,8 @@ var LiveText = class _LiveText extends AbstractCrdt {
10047
10161
  type: "LiveText",
10048
10162
  node: this,
10049
10163
  version: ackedVersion,
10050
- updates: rebuilt.changes
10164
+ updates: rebuilt.changes,
10165
+ source
10051
10166
  }
10052
10167
  };
10053
10168
  }
@@ -10058,7 +10173,7 @@ var LiveText = class _LiveText extends AbstractCrdt {
10058
10173
  return result;
10059
10174
  }
10060
10175
  /** An accepted op from another client (or a server-fabricated fix op). */
10061
- #applyRemote(op) {
10176
+ #applyRemote(op, source) {
10062
10177
  const version = op.version ?? this.#version + 1;
10063
10178
  this.#confirmed = applyTextOperationsToSegments(this.#confirmed, op.ops);
10064
10179
  const [overInFlight, inFlight] = transformTextOperationsX(
@@ -10087,7 +10202,8 @@ var LiveText = class _LiveText extends AbstractCrdt {
10087
10202
  type: "LiveText",
10088
10203
  node: this,
10089
10204
  version: this.#version,
10090
- updates: changes
10205
+ updates: changes,
10206
+ source
10091
10207
  }
10092
10208
  };
10093
10209
  }
@@ -10171,7 +10287,7 @@ var LiveText = class _LiveText extends AbstractCrdt {
10171
10287
  *
10172
10288
  * @internal
10173
10289
  */
10174
- _resyncText(data, version) {
10290
+ _resyncText(data, version, source) {
10175
10291
  this.#confirmed = dataToSegments(data);
10176
10292
  this.#version = version;
10177
10293
  this.#acceptedOps = [];
@@ -10183,7 +10299,8 @@ var LiveText = class _LiveText extends AbstractCrdt {
10183
10299
  type: "LiveText",
10184
10300
  node: this,
10185
10301
  version: this.#version,
10186
- updates: rebuilt.changes
10302
+ updates: rebuilt.changes,
10303
+ source
10187
10304
  };
10188
10305
  }
10189
10306
  /**
@@ -10697,37 +10814,30 @@ function mergeTextStorageUpdates(first, second) {
10697
10814
  updates: first.updates.concat(second.updates)
10698
10815
  };
10699
10816
  }
10817
+ function mergeUpdateSources(first, second) {
10818
+ if (first.origin === "remote" || second.origin === "remote") {
10819
+ return REMOTE;
10820
+ }
10821
+ if (second.via !== "edit") return second;
10822
+ if (first.via !== "edit") return first;
10823
+ return LOCAL_EDIT;
10824
+ }
10700
10825
  function mergeStorageUpdates(first, second) {
10701
10826
  if (first === void 0) {
10702
10827
  return second;
10703
10828
  }
10704
- let merged;
10829
+ const source = mergeUpdateSources(first.source, second.source);
10705
10830
  if (first.type === "LiveObject" && second.type === "LiveObject") {
10706
- merged = mergeObjectStorageUpdates(first, second);
10831
+ return { ...mergeObjectStorageUpdates(first, second), source };
10707
10832
  } else if (first.type === "LiveMap" && second.type === "LiveMap") {
10708
- merged = mergeMapStorageUpdates(first, second);
10833
+ return { ...mergeMapStorageUpdates(first, second), source };
10709
10834
  } else if (first.type === "LiveList" && second.type === "LiveList") {
10710
- merged = mergeListStorageUpdates(first, second);
10835
+ return { ...mergeListStorageUpdates(first, second), source };
10711
10836
  } else if (first.type === "LiveText" && second.type === "LiveText") {
10712
- merged = mergeTextStorageUpdates(first, second);
10837
+ return { ...mergeTextStorageUpdates(first, second), source };
10713
10838
  } else {
10714
- merged = second;
10715
- }
10716
- const sa = first[kStorageUpdateSource];
10717
- const sb = second[kStorageUpdateSource];
10718
- if (sa !== void 0 || sb !== void 0) {
10719
- if (sa?.origin === "remote" || sb?.origin === "remote") {
10720
- merged[kStorageUpdateSource] = { origin: "remote" };
10721
- } else if (sa?.via === "history" || sb?.via === "history") {
10722
- const historySource = sb?.via === "history" ? sb : sa?.via === "history" ? sa : void 0;
10723
- if (historySource?.via === "history") {
10724
- merged[kStorageUpdateSource] = historySource;
10725
- }
10726
- } else {
10727
- merged[kStorageUpdateSource] = { origin: "local", via: "mutation" };
10728
- }
10839
+ return { ...second, source };
10729
10840
  }
10730
- return merged;
10731
10841
  }
10732
10842
 
10733
10843
  // src/devtools/bridge.ts
@@ -11771,9 +11881,6 @@ function createRoom(options, config) {
11771
11881
  }
11772
11882
  });
11773
11883
  function onDispatch(ops, reverse, storageUpdates, options2) {
11774
- for (const value of storageUpdates.values()) {
11775
- value[kStorageUpdateSource] = { origin: "local", via: "mutation" };
11776
- }
11777
11884
  if (context.activeBatch) {
11778
11885
  for (const op of ops) {
11779
11886
  context.activeBatch.ops.push(op);
@@ -11923,7 +12030,7 @@ function createRoom(options, config) {
11923
12030
  if (crdt.type === CrdtType.TEXT) {
11924
12031
  const node = context.pool.nodes.get(id);
11925
12032
  if (node !== void 0 && isLiveText(node)) {
11926
- const update = node._resyncText(crdt.data, crdt.version);
12033
+ const update = node._resyncText(crdt.data, crdt.version, REMOTE);
11927
12034
  if (update !== void 0) {
11928
12035
  result.updates.storageUpdates.set(
11929
12036
  id,
@@ -12026,7 +12133,10 @@ function createRoom(options, config) {
12026
12133
  eventHub.myPresence.notify(context.myPresence.get());
12027
12134
  }
12028
12135
  if (storageUpdates !== void 0 && storageUpdates.size > 0) {
12029
- const updates2 = Array.from(storageUpdates.values());
12136
+ const updates2 = Array.from(storageUpdates.values(), (update) => ({
12137
+ ...update,
12138
+ source: toUpdateSource(update.source)
12139
+ }));
12030
12140
  eventHub.storageBatch.notify(updates2);
12031
12141
  }
12032
12142
  notifyStorageStatus();
@@ -12040,7 +12150,16 @@ function createRoom(options, config) {
12040
12150
  "Internal. Tried to get connection id but connection was never open"
12041
12151
  );
12042
12152
  }
12043
- function applyLocalOps(frames, localStorageUpdateSource = { origin: "local", via: "mutation" }) {
12153
+ const viaByOpId = /* @__PURE__ */ new Map();
12154
+ function viaOfAckedOp(opId) {
12155
+ const via = viaByOpId.get(opId);
12156
+ if (via === void 0) {
12157
+ return "edit";
12158
+ }
12159
+ viaByOpId.delete(opId);
12160
+ return via;
12161
+ }
12162
+ function applyLocalOps(frames, localSource = LOCAL_EDIT) {
12044
12163
  const [pframes, ops] = partition(
12045
12164
  frames,
12046
12165
  (f) => f.type === "presence"
@@ -12083,12 +12202,17 @@ function createRoom(options, config) {
12083
12202
  const opsWithOpIds = remappedOps.map(
12084
12203
  (op) => op.opId === void 0 ? { ...op, opId: context.pool.generateOpId() } : op
12085
12204
  );
12205
+ if (localSource.via !== "edit") {
12206
+ for (const op of opsWithOpIds) {
12207
+ viaByOpId.set(op.opId, localSource.via);
12208
+ }
12209
+ }
12086
12210
  const { reverse, updates } = applyOps(
12087
12211
  pframes,
12088
12212
  opsWithOpIds,
12089
12213
  /* isLocal */
12090
12214
  true,
12091
- localStorageUpdateSource
12215
+ localSource
12092
12216
  );
12093
12217
  return { opsToEmit: opsWithOpIds, reverse, updates };
12094
12218
  }
@@ -12100,7 +12224,7 @@ function createRoom(options, config) {
12100
12224
  false
12101
12225
  );
12102
12226
  }
12103
- function applyOps(pframes, ops, isLocal, localStorageUpdateSource = { origin: "local", via: "mutation" }) {
12227
+ function applyOps(pframes, ops, isLocal, localSource = LOCAL_EDIT) {
12104
12228
  const output = {
12105
12229
  reverse: new Deque(),
12106
12230
  storageUpdates: /* @__PURE__ */ new Map(),
@@ -12129,16 +12253,19 @@ function createRoom(options, config) {
12129
12253
  for (const op of ops) {
12130
12254
  let source;
12131
12255
  if (isLocal) {
12132
- source = 0 /* LOCAL */;
12256
+ source = { ...localSource, optimistic: true };
12133
12257
  } else if (op.opId !== void 0) {
12134
12258
  context.unacknowledgedOps.delete(op.opId);
12135
- source = 2 /* OURS */;
12259
+ source = {
12260
+ origin: "local",
12261
+ via: viaOfAckedOp(op.opId),
12262
+ optimistic: false
12263
+ };
12136
12264
  } else {
12137
- source = 1 /* THEIRS */;
12265
+ source = REMOTE;
12138
12266
  }
12139
12267
  const applyOpResult = applyOp(op, source);
12140
12268
  if (applyOpResult.modified) {
12141
- applyOpResult.modified[kStorageUpdateSource] = source === 1 /* THEIRS */ ? { origin: "remote" } : localStorageUpdateSource;
12142
12269
  const nodeId = applyOpResult.modified.node._id;
12143
12270
  if (!(nodeId && createdNodeIds.has(nodeId))) {
12144
12271
  output.storageUpdates.set(
@@ -12176,7 +12303,7 @@ function createRoom(options, config) {
12176
12303
  if (node === void 0) {
12177
12304
  return { modified: false };
12178
12305
  }
12179
- return node._apply(op, source === 0 /* LOCAL */);
12306
+ return node._apply(op, source);
12180
12307
  }
12181
12308
  case OpCode.SET_PARENT_KEY: {
12182
12309
  const node = context.pool.nodes.get(op.id);
@@ -12469,6 +12596,7 @@ function createRoom(options, config) {
12469
12596
  const rejectedOp = context.unacknowledgedOps.get(opId);
12470
12597
  context.unacknowledgedOps.delete(opId);
12471
12598
  context.buffer.storageOperations = context.buffer.storageOperations.filter((op) => op.opId !== opId);
12599
+ viaByOpId.delete(opId);
12472
12600
  if (rejectedOp !== void 0 && rejectedOp.type === OpCode.UPDATE_TEXT) {
12473
12601
  const node = context.pool.nodes.get(rejectedOp.id);
12474
12602
  if (node !== void 0 && isLiveText(node)) {
@@ -13027,11 +13155,7 @@ function createRoom(options, config) {
13027
13155
  return;
13028
13156
  }
13029
13157
  context.pausedHistory = null;
13030
- const result = applyLocalOps(item.frames, {
13031
- origin: "local",
13032
- via: "history",
13033
- action: "undo"
13034
- });
13158
+ const result = applyLocalOps(item.frames, LOCAL_UNDO);
13035
13159
  context.redoStack.push({ id: item.id, frames: result.reverse });
13036
13160
  notifyPrivateHistory({ action: "undo", id: item.id });
13037
13161
  notify(result.updates);
@@ -13050,11 +13174,7 @@ function createRoom(options, config) {
13050
13174
  return;
13051
13175
  }
13052
13176
  context.pausedHistory = null;
13053
- const result = applyLocalOps(item.frames, {
13054
- origin: "local",
13055
- via: "history",
13056
- action: "redo"
13057
- });
13177
+ const result = applyLocalOps(item.frames, LOCAL_REDO);
13058
13178
  context.undoStack.push({ id: item.id, frames: result.reverse });
13059
13179
  notifyPrivateHistory({ action: "redo", id: item.id });
13060
13180
  notify(result.updates);
@@ -14696,7 +14816,6 @@ export {
14696
14816
  isTextStorageNode,
14697
14817
  isUrl,
14698
14818
  kInternal,
14699
- kStorageUpdateSource,
14700
14819
  keys,
14701
14820
  makeAbortController,
14702
14821
  makeEventSource,