diff --git a/crates/matrix-sdk-indexeddb/src/transaction/mod.rs b/crates/matrix-sdk-indexeddb/src/transaction/mod.rs index 253a7b246..e9668cef4 100644 --- a/crates/matrix-sdk-indexeddb/src/transaction/mod.rs +++ b/crates/matrix-sdk-indexeddb/src/transaction/mod.rs @@ -237,30 +237,10 @@ impl<'a> Transaction<'a> { T: Indexed, T::IndexedType: DeserializeOwned, T::Error: AsyncErrorDeps, - K: IndexedKey + Serialize, + K: IndexedKey + Serialize + DeserializeOwned, { - let range = self.serializer.encode_key_range::(range); - let direction = CursorDirection::Prev; - let object_store = self.transaction.object_store(T::OBJECT_STORE)?; - if let Some(index) = K::INDEX { - let index = object_store.index(index)?; - if let Some(mut cursor) = - index.open_cursor().with_query(range).with_direction(direction).serde()?.await? - { - if let Some(record) = cursor.next_record_ser().await? { - return T::from_indexed(record, self.serializer.inner()) - .map(Some) - .map_err(|e| TransactionError::Serialization(Box::new(e))); - } - } - } else if let Some(mut cursor) = - object_store.open_cursor().with_query(range).with_direction(direction).serde()?.await? - { - if let Some(record) = cursor.next_record_ser().await? { - return T::from_indexed(record, self.serializer.inner()) - .map(Some) - .map_err(|e| TransactionError::Serialization(Box::new(e))); - } + if let Some(key) = self.get_max_key::(range).await? { + return self.get_item_by_key::(key).await; } Ok(None) } @@ -289,6 +269,37 @@ impl<'a> Transaction<'a> { Ok(Vec::new()) } + /// Query IndexedDB for the maximum key in the given range. + pub async fn get_max_key( + &self, + range: impl Into>, + ) -> Result, TransactionError> + where + T: Indexed, + K: IndexedKey + Serialize + DeserializeOwned, + { + let range = self.serializer.encode_key_range::(range); + let direction = CursorDirection::Prev; + let object_store = self.transaction.object_store(T::OBJECT_STORE)?; + if let Some(index) = K::INDEX { + let index = object_store.index(index)?; + if let Some(mut cursor) = + index.open_key_cursor().with_query(range).with_direction(direction).serde()?.await? + { + return cursor.next_key_ser().await.map_err(Into::into); + } + } else if let Some(mut cursor) = object_store + .open_key_cursor() + .with_query(range) + .with_direction(direction) + .serde()? + .await? + { + return cursor.next_key_ser().await.map_err(Into::into); + } + Ok(None) + } + /// Query IndexedDB for keys that match the given key range. Iterate over /// the keys in the given [`direction`](CursorDirection) using a cursor and /// fold them into an accumulator while the given function `f` returns