Skip to content

Commit

Permalink
Merge pull request #2093 from quadratichq/ayush/code_cell
Browse files Browse the repository at this point in the history
fix: code cell outlines
  • Loading branch information
davidkircos authored Nov 21, 2024
2 parents 3a1800b + 614b21b commit ce3b362
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,53 @@ impl PendingTransaction {
offsets_modified.insert((None, Some(row)), size.unwrap_or(0.0));
}
}

pub fn add_updates_from_transaction(&mut self, transaction: PendingTransaction) {
self.generate_thumbnail |= transaction.generate_thumbnail;

self.validations.extend(transaction.validations);

for (sheet_id, dirty_hashes) in transaction.dirty_hashes {
self.dirty_hashes
.entry(sheet_id)
.or_default()
.extend(dirty_hashes);
}

Check warning on line 359 in quadratic-core/src/controller/active_transactions/pending_transaction.rs

View check run for this annotation

Codecov / codecov/patch

quadratic-core/src/controller/active_transactions/pending_transaction.rs#L355-L359

Added lines #L355 - L359 were not covered by tests

self.sheet_borders.extend(transaction.sheet_borders);

for (sheet_id, code_cells) in transaction.code_cells {
self.code_cells
.entry(sheet_id)
.or_default()
.extend(code_cells);
}

Check warning on line 368 in quadratic-core/src/controller/active_transactions/pending_transaction.rs

View check run for this annotation

Codecov / codecov/patch

quadratic-core/src/controller/active_transactions/pending_transaction.rs#L364-L368

Added lines #L364 - L368 were not covered by tests

for (sheet_id, html_cells) in transaction.html_cells {
self.html_cells
.entry(sheet_id)
.or_default()
.extend(html_cells);
}

Check warning on line 375 in quadratic-core/src/controller/active_transactions/pending_transaction.rs

View check run for this annotation

Codecov / codecov/patch

quadratic-core/src/controller/active_transactions/pending_transaction.rs#L371-L375

Added lines #L371 - L375 were not covered by tests

for (sheet_id, image_cells) in transaction.image_cells {
self.image_cells
.entry(sheet_id)
.or_default()
.extend(image_cells);
}

Check warning on line 382 in quadratic-core/src/controller/active_transactions/pending_transaction.rs

View check run for this annotation

Codecov / codecov/patch

quadratic-core/src/controller/active_transactions/pending_transaction.rs#L378-L382

Added lines #L378 - L382 were not covered by tests

self.fill_cells.extend(transaction.fill_cells);

self.sheet_info.extend(transaction.sheet_info);

for (sheet_id, offsets_modified) in transaction.offsets_modified {
self.offsets_modified
.entry(sheet_id)
.or_default()
.extend(offsets_modified);
}

Check warning on line 393 in quadratic-core/src/controller/active_transactions/pending_transaction.rs

View check run for this annotation

Codecov / codecov/patch

quadratic-core/src/controller/active_transactions/pending_transaction.rs#L389-L393

Added lines #L389 - L393 were not covered by tests
}
}

#[cfg(test)]
Expand Down
45 changes: 20 additions & 25 deletions quadratic-core/src/controller/execution/receive_multiplayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl GridController {
}

/// Rolls back unsaved transactions to apply earlier transactions received from the server.
fn rollback_unsaved_transactions(&mut self) {
fn rollback_unsaved_transactions(&mut self, transaction: &mut PendingTransaction) {
if self.transactions.unsaved_transactions.is_empty() {
return;
}
Expand All @@ -50,10 +50,11 @@ impl GridController {
};
self.start_transaction(&mut rollback);
rollback.send_transaction();
transaction.add_updates_from_transaction(rollback);
}

/// Reapplies the rolled-back unsaved transactions after adding earlier transactions.
fn reapply_unsaved_transactions(&mut self) {
fn reapply_unsaved_transactions(&mut self, transaction: &mut PendingTransaction) {
if self.transactions.unsaved_transactions.is_empty() {
return;
}
Expand All @@ -74,6 +75,7 @@ impl GridController {
};
self.start_transaction(&mut reapply);
reapply.send_transaction();
transaction.add_updates_from_transaction(reapply);
}

/// Used by the server to apply transactions. Since the server owns the sequence_num,
Expand Down Expand Up @@ -123,7 +125,11 @@ impl GridController {

/// Check the out_of_order_transactions to see if they are next in order. If so, we remove them from
/// out_of_order_transactions and apply their operations.
fn apply_out_of_order_transactions(&mut self, mut sequence_num: u64) {
fn apply_out_of_order_transactions(
&mut self,
transaction: &mut PendingTransaction,
mut sequence_num: u64,
) {
// nothing to do here
if self.transactions.out_of_order_transactions.is_empty() {
self.transactions.last_sequence_num = sequence_num;
Expand Down Expand Up @@ -154,7 +160,7 @@ impl GridController {
};
self.start_transaction(&mut out_of_order_transaction);
out_of_order_transaction.send_transaction();

transaction.add_updates_from_transaction(out_of_order_transaction);
self.transactions.last_sequence_num = sequence_num;
}

Expand All @@ -176,23 +182,23 @@ impl GridController {
if index == 0 {
self.transactions.unsaved_transactions.remove(index);
self.mark_transaction_sent(transaction.id);
self.apply_out_of_order_transactions(sequence_num);
self.apply_out_of_order_transactions(transaction, sequence_num);
}
// otherwise we need to rollback all transaction and properly apply it
else {
self.rollback_unsaved_transactions();
self.rollback_unsaved_transactions(transaction);

Check warning on line 189 in quadratic-core/src/controller/execution/receive_multiplayer.rs

View check run for this annotation

Codecov / codecov/patch

quadratic-core/src/controller/execution/receive_multiplayer.rs#L189

Added line #L189 was not covered by tests
self.transactions.unsaved_transactions.remove(index);
self.mark_transaction_sent(transaction.id);
self.start_transaction(transaction);
self.apply_out_of_order_transactions(sequence_num);
self.reapply_unsaved_transactions();
self.apply_out_of_order_transactions(transaction, sequence_num);
self.reapply_unsaved_transactions(transaction);

Check warning on line 194 in quadratic-core/src/controller/execution/receive_multiplayer.rs

View check run for this annotation

Codecov / codecov/patch

quadratic-core/src/controller/execution/receive_multiplayer.rs#L193-L194

Added lines #L193 - L194 were not covered by tests
}
} else {
// If the transaction is not one of ours, then we just apply the transaction after rolling back any unsaved transactions
self.rollback_unsaved_transactions();
self.rollback_unsaved_transactions(transaction);
self.start_transaction(transaction);
self.apply_out_of_order_transactions(sequence_num);
self.reapply_unsaved_transactions();
self.apply_out_of_order_transactions(transaction, sequence_num);
self.reapply_unsaved_transactions(transaction);

// We do not need to render a thumbnail unless we have outstanding unsaved transactions.
// Note: this may result in a thumbnail being unnecessarily generated by a user who's
Expand Down Expand Up @@ -228,7 +234,7 @@ impl GridController {
transaction_type: TransactionType::Multiplayer,
..Default::default()
};
self.rollback_unsaved_transactions();
self.rollback_unsaved_transactions(&mut results);

// combine all transaction into one transaction
transactions.into_iter().for_each(|t| {
Expand All @@ -245,25 +251,14 @@ impl GridController {
};

self.client_apply_transaction(&mut transaction, t.sequence_num);
results.generate_thumbnail |= transaction.generate_thumbnail;
results.validations.extend(transaction.validations);
results.dirty_hashes.extend(transaction.dirty_hashes);
results.sheet_borders.extend(transaction.sheet_borders);
results.code_cells.extend(transaction.code_cells);
results.html_cells.extend(transaction.html_cells);
results.image_cells.extend(transaction.image_cells);
results.fill_cells.extend(transaction.fill_cells);
results.sheet_info.extend(transaction.sheet_info);
results
.offsets_modified
.extend(transaction.offsets_modified);
results.add_updates_from_transaction(transaction);
} else {
dbgjs!(
"Unable to decompress and deserialize operations in received_transactions()"
);
}
});
self.reapply_unsaved_transactions();
self.reapply_unsaved_transactions(&mut results);
self.finalize_transaction(results);
}

Expand Down

0 comments on commit ce3b362

Please sign in to comment.