diff --git a/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs b/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs index dfdfee4f854..6092a568f94 100644 --- a/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs +++ b/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs @@ -1205,15 +1205,12 @@ async fn fetch_from_object_store_batched( where F: Fn(JsValue) -> Result, { - enum NextBatch { - First, // In the first batch we don't start from a key - Later(JsValue), // In later batches we have a starting key - } - let mut result = Vec::new(); - let mut next_batch = NextBatch::First; let mut batch_n = 0; + // The empty string is before all keys in Indexed DB - first batch starts there. + let mut latest_key: JsValue = "".into(); + loop { debug!("Fetching Indexed DB records starting from {}", batch_n * batch_size); @@ -1221,28 +1218,17 @@ where // would like to use `get_all_with_key_and_limit` if it ever exists // but for now we use a cursor and manually limit batch size. - // Get hold of a cursor for this batch - let cursor = match &next_batch { - NextBatch::First => object_store.open_cursor(), - - NextBatch::Later(latest_key) => { - // We want a cursor starting after the last key we processed - let after_latest_key = IdbKeyRange::lower_bound_with_open(latest_key, true); - - // This key should always be valid since it was returned by `cursor.key()`. - // (Note: the key doesn't have to exist to be valid, so even if - // somehow the entry was deleted, we should not panic here.) - let after_latest_key = after_latest_key.expect("Key was not valid!"); - - object_store.open_cursor_with_range(&after_latest_key) - } - }? - .await?; + // Get hold of a cursor for this batch. (This should not panic in expect() + // because we always use "", or the result of cursor.key(), both of + // which are valid keys.) + let after_latest_key = + IdbKeyRange::lower_bound_with_open(&latest_key, true).expect("Key was not valid!"); + let cursor = object_store.open_cursor_with_range(&after_latest_key)?.await?; // Fetch batch_size records into result let next_key = fetch_batch(cursor, batch_size, &f, &mut result).await?; if let Some(next_key) = next_key { - next_batch = NextBatch::Later(next_key); + latest_key = next_key; } else { break; }