Skip to content

Commit

Permalink
impr(linearizeOps): remove the need for sorting before splicing
Browse files Browse the repository at this point in the history
  • Loading branch information
JanLewDev committed Aug 28, 2024
1 parent 276ed98 commit afa3587
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/crdt/src/cros/ReduceActionType/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class ReduceActionType<T> {
}

linearizeOps(): Operation<T>[] {
const order = this.hashGraph.topologicalSort();
let order = this.hashGraph.topologicalSort();
const result: Operation<T>[] = [];
let i = 0;

Expand Down Expand Up @@ -90,15 +90,19 @@ export class ReduceActionType<T> {
const resolved = this.resolveConflicts(concurrentOps);

switch (resolved.action) {
case ActionType.Reduce:
// Sort the indices in descending order, so that splice does not mess up the order
resolved.indices.sort((a, b) => (a < b ? 1 : -1));
case ActionType.Reduce: {
const newOrder = [];
for (const idx of resolved.indices) {
if (idx === i) shouldIncrementI = false;
order.splice(idx, 1);
order[idx] = "";
}
for (const val of order) {
if (val !== "") newOrder.push(val);
}
order = newOrder;
if (!shouldIncrementI) j = order.length; // Break out of inner loop
break;
}
case ActionType.Nop:
j++;
break;
Expand Down

0 comments on commit afa3587

Please sign in to comment.