Skip to content

Commit

Permalink
Merge pull request #1534 from quadratichq/fix-offline-transaction-ord…
Browse files Browse the repository at this point in the history
…ering

Fix offline ResizeRow/Column and transaction ordering
  • Loading branch information
davidkircos authored Jul 8, 2024
2 parents 8fb52fd + fd9b061 commit 12e7a1a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class Offline {
operations: r.operations ?? 0,
};
});
// set the index to the length of the results so that we can add new transactions to the end
this.index = results.length;
this.stats = {
transactions: results.length,
operations: results.reduce((acc, r) => acc + r.operations, 0),
Expand Down Expand Up @@ -150,13 +152,19 @@ class Offline {
}
}

unsentTransactions?.forEach((tx) => {
// we remove the transaction is there was a problem applying it (eg, if
// the schema has changed since it was saved offline)
if (!core.applyOfflineUnsavedTransaction(tx.transactionId, tx.transactions)) {
this.markTransactionSent(tx.transactionId);
// We need to apply the transactions in the opposite order to which they
// were saved because core will rollback old transactions before applying
// new ones
if (unsentTransactions?.length) {
for (let i = unsentTransactions.length - 1; i >= 0; i--) {
const tx = unsentTransactions[i];
// we remove the transaction is there was a problem applying it (eg, if
// the schema has changed since it was saved offline)
if (!core.applyOfflineUnsavedTransaction(tx.transactionId, tx.transactions)) {
this.markTransactionSent(tx.transactionId);
}
}
});
}
}

// Used by tests to clear all entries from the indexedDb for this fileId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,13 @@ mod test {
Some((0, &transaction))
);
}

#[test]
fn from_str() {
// this is a real example of a transaction that was failing to parse; it
// can be deleted if it ever causes problems
let json = r#"{"forward":{"id":"8ac83231-0af5-461a-82bb-f07119efad97","sequence_num":null,"operations":[{"ResizeColumn":{"sheet_id":{"id":"3d3235c9-c725-4568-9b84-ea0b74820776"},"column":1,"new_size":174.71484375,"client_resized":true}}],"cursor":null},"reverse":{"id":"8ac83231-0af5-461a-82bb-f07119efad97","sequence_num":null,"operations":[{"ResizeColumn":{"sheet_id":{"id":"3d3235c9-c725-4568-9b84-ea0b74820776"},"column":1,"new_size":100.0}}],"cursor":"{\"sheetId\":\"3d3235c9-c725-4568-9b84-ea0b74820776\",\"keyboardMovePosition\":{\"x\":0,\"y\":0},\"cursorPosition\":{\"x\":0,\"y\":0}}"},"sent_to_server":false}"#;
let unsaved_transaction: UnsavedTransaction = serde_json::from_str(json).unwrap();
assert!(!unsaved_transaction.sent_to_server);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl GridController {
sheet_id,
column,
new_size,
client_resized: true,
client_resized: false,
});

let old_size = sheet.offsets.set_column_width(column, new_size);
Expand Down Expand Up @@ -77,7 +77,7 @@ impl GridController {
sheet_id,
row,
new_size,
client_resized: true,
client_resized: false,
});
let old_size = sheet.offsets.set_row_height(row, new_size);

Expand Down
4 changes: 2 additions & 2 deletions quadratic-core/src/controller/operations/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub enum Operation {
// notified of the resize. For manual resizing, the original client is
// updated as the user drags the column/row so they don't need to be
// notified again.
#[serde(skip_serializing_if = "std::ops::Not::not")]
#[serde(default)]
client_resized: bool,
},
ResizeRow {
Expand All @@ -97,7 +97,7 @@ pub enum Operation {
new_size: f64,

// See note in ResizeColumn.
#[serde(skip_serializing_if = "std::ops::Not::not")]
#[serde(default)]
client_resized: bool,
},

Expand Down
7 changes: 4 additions & 3 deletions quadratic-core/src/wasm_bindings/controller/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ impl GridController {
&self.apply_offline_unsaved_transaction(transaction_id, unsaved_transaction),
)?)
} else {
Err(JsValue::from_str(
"Invalid unsaved transaction received in applyOfflineUnsavedTransaction",
))
Err(JsValue::from_str(&format!(
"Invalid unsaved transaction received in applyOfflineUnsavedTransaction {}",
unsaved_transaction
)))
}
}
}

0 comments on commit 12e7a1a

Please sign in to comment.