@liveblocks/core 3.23.1-exp1 → 3.23.1-exp3
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.cjs +399 -251
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +180 -167
- package/dist/index.d.ts +180 -167
- package/dist/index.js +326 -178
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
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-
|
|
9
|
+
var PKG_VERSION = "3.23.1-exp3";
|
|
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,
|
|
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(
|
|
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,
|
|
1694
|
-
return super._apply(op,
|
|
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,
|
|
6915
|
-
return super._apply(op,
|
|
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
|
-
#
|
|
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(
|
|
7095
|
-
|
|
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(
|
|
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(
|
|
7180
|
-
|
|
7181
|
-
|
|
7182
|
-
|
|
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(
|
|
7199
|
-
|
|
7200
|
-
|
|
7201
|
-
|
|
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(
|
|
7238
|
-
|
|
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(
|
|
7344
|
-
|
|
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
|
-
#
|
|
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
|
-
#
|
|
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,
|
|
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 (
|
|
7444
|
-
result = this.#
|
|
7445
|
-
} else if (
|
|
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.#
|
|
7482
|
+
result = this.#applyLocalSet(op, source);
|
|
7449
7483
|
}
|
|
7450
7484
|
} else {
|
|
7451
|
-
if (
|
|
7485
|
+
if (opSource.origin === "remote") {
|
|
7452
7486
|
result = this.#applyRemoteInsert(op);
|
|
7453
|
-
} else if (
|
|
7454
|
-
result = this.#applyInsertAck(op);
|
|
7487
|
+
} else if (!opSource.optimistic) {
|
|
7488
|
+
result = this.#applyInsertAck(op, source);
|
|
7455
7489
|
} else {
|
|
7456
|
-
result = this.#
|
|
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(
|
|
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
|
-
#
|
|
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(
|
|
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(
|
|
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(
|
|
7583
|
-
|
|
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
|
-
#
|
|
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(
|
|
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,
|
|
7621
|
-
|
|
7622
|
-
|
|
7623
|
-
|
|
7624
|
-
|
|
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.#
|
|
7679
|
+
return this.#applyLocalSetChildKey(newKey, child, source);
|
|
7627
7680
|
}
|
|
7628
7681
|
}
|
|
7629
7682
|
/** @internal */
|
|
7630
|
-
_apply(op,
|
|
7631
|
-
return super._apply(op,
|
|
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
|
-
[
|
|
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(
|
|
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(
|
|
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 ===
|
|
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 ===
|
|
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,
|
|
8811
|
+
_apply(op, source) {
|
|
8743
8812
|
if (op.type === OpCode.UPDATE_OBJECT) {
|
|
8744
|
-
return this.#applyUpdate(op,
|
|
8813
|
+
return this.#applyUpdate(op, source);
|
|
8745
8814
|
} else if (op.type === OpCode.DELETE_OBJECT_KEY) {
|
|
8746
|
-
return this.#applyDeleteObjectKey(op,
|
|
8815
|
+
return this.#applyDeleteObjectKey(op, source);
|
|
8747
8816
|
}
|
|
8748
|
-
return super._apply(op,
|
|
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,
|
|
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 (
|
|
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,
|
|
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 (!
|
|
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);
|
|
@@ -9638,6 +9738,34 @@ function applyLiveTextOperations(data, ops) {
|
|
|
9638
9738
|
applyTextOperationsToSegments(dataToSegments(data), ops)
|
|
9639
9739
|
);
|
|
9640
9740
|
}
|
|
9741
|
+
function normalizeLiveTextOperations(data, operations) {
|
|
9742
|
+
let shadow = dataToSegments(data);
|
|
9743
|
+
const normalized = [];
|
|
9744
|
+
for (const operation of operations) {
|
|
9745
|
+
const text = shadow.map((segment) => segment.text).join("");
|
|
9746
|
+
let normalizedOperation;
|
|
9747
|
+
if (operation.type === "insert") {
|
|
9748
|
+
normalizedOperation = {
|
|
9749
|
+
...operation,
|
|
9750
|
+
index: clipIndexToCodePointBoundary(text, operation.index)
|
|
9751
|
+
};
|
|
9752
|
+
} else {
|
|
9753
|
+
const range = clipRangeToCodePointBoundaries(
|
|
9754
|
+
text,
|
|
9755
|
+
operation.index,
|
|
9756
|
+
operation.length
|
|
9757
|
+
);
|
|
9758
|
+
normalizedOperation = {
|
|
9759
|
+
...operation,
|
|
9760
|
+
index: range.index,
|
|
9761
|
+
length: range.length
|
|
9762
|
+
};
|
|
9763
|
+
}
|
|
9764
|
+
normalized.push(normalizedOperation);
|
|
9765
|
+
shadow = applyTextOperationsToSegments(shadow, [normalizedOperation]);
|
|
9766
|
+
}
|
|
9767
|
+
return normalized;
|
|
9768
|
+
}
|
|
9641
9769
|
function invertTextOperations(segments, ops) {
|
|
9642
9770
|
let shadow = [...segments];
|
|
9643
9771
|
const reverse = [];
|
|
@@ -9770,21 +9898,21 @@ var LiveText = class _LiveText extends AbstractCrdt {
|
|
|
9770
9898
|
throw new Error("LiveText cannot contain child nodes");
|
|
9771
9899
|
}
|
|
9772
9900
|
/** @internal */
|
|
9773
|
-
_apply(op,
|
|
9901
|
+
_apply(op, source) {
|
|
9774
9902
|
if (op.type !== OpCode.UPDATE_TEXT) {
|
|
9775
|
-
return super._apply(op,
|
|
9903
|
+
return super._apply(op, source);
|
|
9776
9904
|
}
|
|
9777
|
-
if (
|
|
9778
|
-
return this.#applyLocal(op);
|
|
9905
|
+
if (source.origin === "local" && source.optimistic) {
|
|
9906
|
+
return this.#applyLocal(op, toUpdateSource(source));
|
|
9779
9907
|
}
|
|
9780
9908
|
if (op.opId !== void 0 && op.opId === this.#inFlightOpId) {
|
|
9781
|
-
return this.#applyAck(op);
|
|
9909
|
+
return this.#applyAck(op, toUpdateSource(source));
|
|
9782
9910
|
}
|
|
9783
9911
|
if (op.opId !== void 0 && this.#acceptedOps.some((entry) => entry.opId === op.opId)) {
|
|
9784
9912
|
this.#version = Math.max(this.#version, op.version ?? op.baseVersion + 1);
|
|
9785
9913
|
return { modified: false };
|
|
9786
9914
|
}
|
|
9787
|
-
return this.#applyRemote(op);
|
|
9915
|
+
return this.#applyRemote(op, toUpdateSource(source));
|
|
9788
9916
|
}
|
|
9789
9917
|
/**
|
|
9790
9918
|
* Inserts text at the given index.
|
|
@@ -9800,7 +9928,7 @@ var LiveText = class _LiveText extends AbstractCrdt {
|
|
|
9800
9928
|
* text.insert(0, "Say: ", { italic: true });
|
|
9801
9929
|
*/
|
|
9802
9930
|
insert(index, text, attributes) {
|
|
9803
|
-
const clippedIndex =
|
|
9931
|
+
const clippedIndex = clipIndexToCodePointBoundary(this.toString(), index);
|
|
9804
9932
|
this.#dispatch([{ type: "insert", index: clippedIndex, text, attributes }]);
|
|
9805
9933
|
}
|
|
9806
9934
|
/**
|
|
@@ -9811,7 +9939,11 @@ var LiveText = class _LiveText extends AbstractCrdt {
|
|
|
9811
9939
|
* text.delete(5, 6); // "Hello"
|
|
9812
9940
|
*/
|
|
9813
9941
|
delete(index, length) {
|
|
9814
|
-
const clipped =
|
|
9942
|
+
const clipped = clipRangeToCodePointBoundaries(
|
|
9943
|
+
this.toString(),
|
|
9944
|
+
index,
|
|
9945
|
+
length
|
|
9946
|
+
);
|
|
9815
9947
|
if (clipped.length === 0) {
|
|
9816
9948
|
return;
|
|
9817
9949
|
}
|
|
@@ -9827,7 +9959,11 @@ var LiveText = class _LiveText extends AbstractCrdt {
|
|
|
9827
9959
|
* text.replace(0, 5, "Hi"); // "Hi world"
|
|
9828
9960
|
*/
|
|
9829
9961
|
replace(index, length, text, attributes) {
|
|
9830
|
-
const clipped =
|
|
9962
|
+
const clipped = clipRangeToCodePointBoundaries(
|
|
9963
|
+
this.toString(),
|
|
9964
|
+
index,
|
|
9965
|
+
length
|
|
9966
|
+
);
|
|
9831
9967
|
const ops = [];
|
|
9832
9968
|
if (clipped.length > 0) {
|
|
9833
9969
|
ops.push({
|
|
@@ -9920,7 +10056,11 @@ var LiveText = class _LiveText extends AbstractCrdt {
|
|
|
9920
10056
|
* text.format(0, 5, { bold: null });
|
|
9921
10057
|
*/
|
|
9922
10058
|
format(index, length, attributes) {
|
|
9923
|
-
const clipped =
|
|
10059
|
+
const clipped = clipRangeToCodePointBoundaries(
|
|
10060
|
+
this.toString(),
|
|
10061
|
+
index,
|
|
10062
|
+
length
|
|
10063
|
+
);
|
|
9924
10064
|
if (clipped.length === 0) {
|
|
9925
10065
|
return;
|
|
9926
10066
|
}
|
|
@@ -9954,7 +10094,8 @@ var LiveText = class _LiveText extends AbstractCrdt {
|
|
|
9954
10094
|
type: "LiveText",
|
|
9955
10095
|
node: this,
|
|
9956
10096
|
version: this.#version,
|
|
9957
|
-
updates: changes
|
|
10097
|
+
updates: changes,
|
|
10098
|
+
source: LOCAL_EDIT
|
|
9958
10099
|
}
|
|
9959
10100
|
]
|
|
9960
10101
|
]);
|
|
@@ -9984,7 +10125,7 @@ var LiveText = class _LiveText extends AbstractCrdt {
|
|
|
9984
10125
|
* A local replay of an existing wire op: an undo/redo frame, or an
|
|
9985
10126
|
* unacknowledged op re-sent after a reconnect.
|
|
9986
10127
|
*/
|
|
9987
|
-
#applyLocal(op) {
|
|
10128
|
+
#applyLocal(op, source) {
|
|
9988
10129
|
const mutableOp = op;
|
|
9989
10130
|
if (op.opId !== void 0 && op.opId === this.#inFlightOpId) {
|
|
9990
10131
|
this.#inFlightOps = [...this.#inFlightOps, ...this.#queuedOps];
|
|
@@ -10020,12 +10161,13 @@ var LiveText = class _LiveText extends AbstractCrdt {
|
|
|
10020
10161
|
type: "LiveText",
|
|
10021
10162
|
node: this,
|
|
10022
10163
|
version: this.#version,
|
|
10023
|
-
updates: changes
|
|
10164
|
+
updates: changes,
|
|
10165
|
+
source
|
|
10024
10166
|
}
|
|
10025
10167
|
};
|
|
10026
10168
|
}
|
|
10027
10169
|
/** Server acknowledgement of our in-flight op. */
|
|
10028
|
-
#applyAck(op) {
|
|
10170
|
+
#applyAck(op, source) {
|
|
10029
10171
|
const ackedVersion = op.version ?? Math.max(this.#version, op.baseVersion + 1);
|
|
10030
10172
|
const predicted = this.#inFlightOps;
|
|
10031
10173
|
const opId = this.#inFlightOpId;
|
|
@@ -10047,7 +10189,8 @@ var LiveText = class _LiveText extends AbstractCrdt {
|
|
|
10047
10189
|
type: "LiveText",
|
|
10048
10190
|
node: this,
|
|
10049
10191
|
version: ackedVersion,
|
|
10050
|
-
updates: rebuilt.changes
|
|
10192
|
+
updates: rebuilt.changes,
|
|
10193
|
+
source
|
|
10051
10194
|
}
|
|
10052
10195
|
};
|
|
10053
10196
|
}
|
|
@@ -10058,7 +10201,7 @@ var LiveText = class _LiveText extends AbstractCrdt {
|
|
|
10058
10201
|
return result;
|
|
10059
10202
|
}
|
|
10060
10203
|
/** An accepted op from another client (or a server-fabricated fix op). */
|
|
10061
|
-
#applyRemote(op) {
|
|
10204
|
+
#applyRemote(op, source) {
|
|
10062
10205
|
const version = op.version ?? this.#version + 1;
|
|
10063
10206
|
this.#confirmed = applyTextOperationsToSegments(this.#confirmed, op.ops);
|
|
10064
10207
|
const [overInFlight, inFlight] = transformTextOperationsX(
|
|
@@ -10087,7 +10230,8 @@ var LiveText = class _LiveText extends AbstractCrdt {
|
|
|
10087
10230
|
type: "LiveText",
|
|
10088
10231
|
node: this,
|
|
10089
10232
|
version: this.#version,
|
|
10090
|
-
updates: changes
|
|
10233
|
+
updates: changes,
|
|
10234
|
+
source
|
|
10091
10235
|
}
|
|
10092
10236
|
};
|
|
10093
10237
|
}
|
|
@@ -10171,7 +10315,7 @@ var LiveText = class _LiveText extends AbstractCrdt {
|
|
|
10171
10315
|
*
|
|
10172
10316
|
* @internal
|
|
10173
10317
|
*/
|
|
10174
|
-
_resyncText(data, version) {
|
|
10318
|
+
_resyncText(data, version, source) {
|
|
10175
10319
|
this.#confirmed = dataToSegments(data);
|
|
10176
10320
|
this.#version = version;
|
|
10177
10321
|
this.#acceptedOps = [];
|
|
@@ -10183,7 +10327,8 @@ var LiveText = class _LiveText extends AbstractCrdt {
|
|
|
10183
10327
|
type: "LiveText",
|
|
10184
10328
|
node: this,
|
|
10185
10329
|
version: this.#version,
|
|
10186
|
-
updates: rebuilt.changes
|
|
10330
|
+
updates: rebuilt.changes,
|
|
10331
|
+
source
|
|
10187
10332
|
};
|
|
10188
10333
|
}
|
|
10189
10334
|
/**
|
|
@@ -10697,37 +10842,30 @@ function mergeTextStorageUpdates(first, second) {
|
|
|
10697
10842
|
updates: first.updates.concat(second.updates)
|
|
10698
10843
|
};
|
|
10699
10844
|
}
|
|
10845
|
+
function mergeUpdateSources(first, second) {
|
|
10846
|
+
if (first.origin === "remote" || second.origin === "remote") {
|
|
10847
|
+
return REMOTE;
|
|
10848
|
+
}
|
|
10849
|
+
if (second.via !== "edit") return second;
|
|
10850
|
+
if (first.via !== "edit") return first;
|
|
10851
|
+
return LOCAL_EDIT;
|
|
10852
|
+
}
|
|
10700
10853
|
function mergeStorageUpdates(first, second) {
|
|
10701
10854
|
if (first === void 0) {
|
|
10702
10855
|
return second;
|
|
10703
10856
|
}
|
|
10704
|
-
|
|
10857
|
+
const source = mergeUpdateSources(first.source, second.source);
|
|
10705
10858
|
if (first.type === "LiveObject" && second.type === "LiveObject") {
|
|
10706
|
-
|
|
10859
|
+
return { ...mergeObjectStorageUpdates(first, second), source };
|
|
10707
10860
|
} else if (first.type === "LiveMap" && second.type === "LiveMap") {
|
|
10708
|
-
|
|
10861
|
+
return { ...mergeMapStorageUpdates(first, second), source };
|
|
10709
10862
|
} else if (first.type === "LiveList" && second.type === "LiveList") {
|
|
10710
|
-
|
|
10863
|
+
return { ...mergeListStorageUpdates(first, second), source };
|
|
10711
10864
|
} else if (first.type === "LiveText" && second.type === "LiveText") {
|
|
10712
|
-
|
|
10865
|
+
return { ...mergeTextStorageUpdates(first, second), source };
|
|
10713
10866
|
} else {
|
|
10714
|
-
|
|
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
|
-
}
|
|
10867
|
+
return { ...second, source };
|
|
10729
10868
|
}
|
|
10730
|
-
return merged;
|
|
10731
10869
|
}
|
|
10732
10870
|
|
|
10733
10871
|
// src/devtools/bridge.ts
|
|
@@ -11771,9 +11909,6 @@ function createRoom(options, config) {
|
|
|
11771
11909
|
}
|
|
11772
11910
|
});
|
|
11773
11911
|
function onDispatch(ops, reverse, storageUpdates, options2) {
|
|
11774
|
-
for (const value of storageUpdates.values()) {
|
|
11775
|
-
value[kStorageUpdateSource] = { origin: "local", via: "mutation" };
|
|
11776
|
-
}
|
|
11777
11912
|
if (context.activeBatch) {
|
|
11778
11913
|
for (const op of ops) {
|
|
11779
11914
|
context.activeBatch.ops.push(op);
|
|
@@ -11923,7 +12058,7 @@ function createRoom(options, config) {
|
|
|
11923
12058
|
if (crdt.type === CrdtType.TEXT) {
|
|
11924
12059
|
const node = context.pool.nodes.get(id);
|
|
11925
12060
|
if (node !== void 0 && isLiveText(node)) {
|
|
11926
|
-
const update = node._resyncText(crdt.data, crdt.version);
|
|
12061
|
+
const update = node._resyncText(crdt.data, crdt.version, REMOTE);
|
|
11927
12062
|
if (update !== void 0) {
|
|
11928
12063
|
result.updates.storageUpdates.set(
|
|
11929
12064
|
id,
|
|
@@ -12026,7 +12161,10 @@ function createRoom(options, config) {
|
|
|
12026
12161
|
eventHub.myPresence.notify(context.myPresence.get());
|
|
12027
12162
|
}
|
|
12028
12163
|
if (storageUpdates !== void 0 && storageUpdates.size > 0) {
|
|
12029
|
-
const updates2 = Array.from(storageUpdates.values())
|
|
12164
|
+
const updates2 = Array.from(storageUpdates.values(), (update) => ({
|
|
12165
|
+
...update,
|
|
12166
|
+
source: toUpdateSource(update.source)
|
|
12167
|
+
}));
|
|
12030
12168
|
eventHub.storageBatch.notify(updates2);
|
|
12031
12169
|
}
|
|
12032
12170
|
notifyStorageStatus();
|
|
@@ -12040,7 +12178,16 @@ function createRoom(options, config) {
|
|
|
12040
12178
|
"Internal. Tried to get connection id but connection was never open"
|
|
12041
12179
|
);
|
|
12042
12180
|
}
|
|
12043
|
-
|
|
12181
|
+
const viaByOpId = /* @__PURE__ */ new Map();
|
|
12182
|
+
function viaOfAckedOp(opId) {
|
|
12183
|
+
const via = viaByOpId.get(opId);
|
|
12184
|
+
if (via === void 0) {
|
|
12185
|
+
return "edit";
|
|
12186
|
+
}
|
|
12187
|
+
viaByOpId.delete(opId);
|
|
12188
|
+
return via;
|
|
12189
|
+
}
|
|
12190
|
+
function applyLocalOps(frames, localSource = LOCAL_EDIT) {
|
|
12044
12191
|
const [pframes, ops] = partition(
|
|
12045
12192
|
frames,
|
|
12046
12193
|
(f) => f.type === "presence"
|
|
@@ -12083,12 +12230,17 @@ function createRoom(options, config) {
|
|
|
12083
12230
|
const opsWithOpIds = remappedOps.map(
|
|
12084
12231
|
(op) => op.opId === void 0 ? { ...op, opId: context.pool.generateOpId() } : op
|
|
12085
12232
|
);
|
|
12233
|
+
if (localSource.via !== "edit") {
|
|
12234
|
+
for (const op of opsWithOpIds) {
|
|
12235
|
+
viaByOpId.set(op.opId, localSource.via);
|
|
12236
|
+
}
|
|
12237
|
+
}
|
|
12086
12238
|
const { reverse, updates } = applyOps(
|
|
12087
12239
|
pframes,
|
|
12088
12240
|
opsWithOpIds,
|
|
12089
12241
|
/* isLocal */
|
|
12090
12242
|
true,
|
|
12091
|
-
|
|
12243
|
+
localSource
|
|
12092
12244
|
);
|
|
12093
12245
|
return { opsToEmit: opsWithOpIds, reverse, updates };
|
|
12094
12246
|
}
|
|
@@ -12100,7 +12252,7 @@ function createRoom(options, config) {
|
|
|
12100
12252
|
false
|
|
12101
12253
|
);
|
|
12102
12254
|
}
|
|
12103
|
-
function applyOps(pframes, ops, isLocal,
|
|
12255
|
+
function applyOps(pframes, ops, isLocal, localSource = LOCAL_EDIT) {
|
|
12104
12256
|
const output = {
|
|
12105
12257
|
reverse: new Deque(),
|
|
12106
12258
|
storageUpdates: /* @__PURE__ */ new Map(),
|
|
@@ -12129,16 +12281,19 @@ function createRoom(options, config) {
|
|
|
12129
12281
|
for (const op of ops) {
|
|
12130
12282
|
let source;
|
|
12131
12283
|
if (isLocal) {
|
|
12132
|
-
source =
|
|
12284
|
+
source = { ...localSource, optimistic: true };
|
|
12133
12285
|
} else if (op.opId !== void 0) {
|
|
12134
12286
|
context.unacknowledgedOps.delete(op.opId);
|
|
12135
|
-
source =
|
|
12287
|
+
source = {
|
|
12288
|
+
origin: "local",
|
|
12289
|
+
via: viaOfAckedOp(op.opId),
|
|
12290
|
+
optimistic: false
|
|
12291
|
+
};
|
|
12136
12292
|
} else {
|
|
12137
|
-
source =
|
|
12293
|
+
source = REMOTE;
|
|
12138
12294
|
}
|
|
12139
12295
|
const applyOpResult = applyOp(op, source);
|
|
12140
12296
|
if (applyOpResult.modified) {
|
|
12141
|
-
applyOpResult.modified[kStorageUpdateSource] = source === 1 /* THEIRS */ ? { origin: "remote" } : localStorageUpdateSource;
|
|
12142
12297
|
const nodeId = applyOpResult.modified.node._id;
|
|
12143
12298
|
if (!(nodeId && createdNodeIds.has(nodeId))) {
|
|
12144
12299
|
output.storageUpdates.set(
|
|
@@ -12176,7 +12331,7 @@ function createRoom(options, config) {
|
|
|
12176
12331
|
if (node === void 0) {
|
|
12177
12332
|
return { modified: false };
|
|
12178
12333
|
}
|
|
12179
|
-
return node._apply(op, source
|
|
12334
|
+
return node._apply(op, source);
|
|
12180
12335
|
}
|
|
12181
12336
|
case OpCode.SET_PARENT_KEY: {
|
|
12182
12337
|
const node = context.pool.nodes.get(op.id);
|
|
@@ -12469,6 +12624,7 @@ function createRoom(options, config) {
|
|
|
12469
12624
|
const rejectedOp = context.unacknowledgedOps.get(opId);
|
|
12470
12625
|
context.unacknowledgedOps.delete(opId);
|
|
12471
12626
|
context.buffer.storageOperations = context.buffer.storageOperations.filter((op) => op.opId !== opId);
|
|
12627
|
+
viaByOpId.delete(opId);
|
|
12472
12628
|
if (rejectedOp !== void 0 && rejectedOp.type === OpCode.UPDATE_TEXT) {
|
|
12473
12629
|
const node = context.pool.nodes.get(rejectedOp.id);
|
|
12474
12630
|
if (node !== void 0 && isLiveText(node)) {
|
|
@@ -13027,11 +13183,7 @@ function createRoom(options, config) {
|
|
|
13027
13183
|
return;
|
|
13028
13184
|
}
|
|
13029
13185
|
context.pausedHistory = null;
|
|
13030
|
-
const result = applyLocalOps(item.frames,
|
|
13031
|
-
origin: "local",
|
|
13032
|
-
via: "history",
|
|
13033
|
-
action: "undo"
|
|
13034
|
-
});
|
|
13186
|
+
const result = applyLocalOps(item.frames, LOCAL_UNDO);
|
|
13035
13187
|
context.redoStack.push({ id: item.id, frames: result.reverse });
|
|
13036
13188
|
notifyPrivateHistory({ action: "undo", id: item.id });
|
|
13037
13189
|
notify(result.updates);
|
|
@@ -13050,11 +13202,7 @@ function createRoom(options, config) {
|
|
|
13050
13202
|
return;
|
|
13051
13203
|
}
|
|
13052
13204
|
context.pausedHistory = null;
|
|
13053
|
-
const result = applyLocalOps(item.frames,
|
|
13054
|
-
origin: "local",
|
|
13055
|
-
via: "history",
|
|
13056
|
-
action: "redo"
|
|
13057
|
-
});
|
|
13205
|
+
const result = applyLocalOps(item.frames, LOCAL_REDO);
|
|
13058
13206
|
context.undoStack.push({ id: item.id, frames: result.reverse });
|
|
13059
13207
|
notifyPrivateHistory({ action: "redo", id: item.id });
|
|
13060
13208
|
notify(result.updates);
|
|
@@ -14696,7 +14844,6 @@ export {
|
|
|
14696
14844
|
isTextStorageNode,
|
|
14697
14845
|
isUrl,
|
|
14698
14846
|
kInternal,
|
|
14699
|
-
kStorageUpdateSource,
|
|
14700
14847
|
keys,
|
|
14701
14848
|
makeAbortController,
|
|
14702
14849
|
makeEventSource,
|
|
@@ -14708,6 +14855,7 @@ export {
|
|
|
14708
14855
|
nanoid,
|
|
14709
14856
|
nn,
|
|
14710
14857
|
nodeStreamToCompactNodes,
|
|
14858
|
+
normalizeLiveTextOperations,
|
|
14711
14859
|
normalizeRoomAccesses,
|
|
14712
14860
|
normalizeRoomPermissions,
|
|
14713
14861
|
normalizeUpdateRoomAccesses,
|