story: Update Table story to generate increment id. (#1670)

Close #1661
This commit is contained in:
Jason Lee 2025-11-24 15:01:34 +08:00 committed by GitHub
parent f2cbc16655
commit bdc5ce4b6e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -42,6 +42,7 @@ struct Counter {
static ALL_COUNTERS: LazyLock<Vec<Counter>> =
LazyLock::new(|| serde_json::from_str(include_str!("./fixtures/counters.json")).unwrap());
static INCREMENT_ID: LazyLock<std::sync::Mutex<usize>> = LazyLock::new(|| std::sync::Mutex::new(0));
impl Counter {
fn random() -> Self {
@ -124,7 +125,15 @@ impl Stock {
}
fn random_stocks(size: usize) -> Vec<Stock> {
(0..size)
// Incremental ID with size.
let start = {
let mut id_lock = INCREMENT_ID.lock().unwrap();
let start = *id_lock;
*id_lock += size + 1;
start
};
(start..start + size)
.map(|id| Stock {
id,
counter: Counter::random(),
@ -268,6 +277,12 @@ impl StockTableDelegate {
}
fn update_stocks(&mut self, size: usize) {
// Reset incremental ID
{
let mut id_lock = INCREMENT_ID.lock().unwrap();
*id_lock = 0;
}
self.stocks = random_stocks(size);
self.eof = size <= 50;
self.loading = false;