refactor(indexeddb): return indexed type from Transaction::add_item and its derivatives

Signed-off-by: Michael Goldenberg <m@mgoldenberg.net>
This commit is contained in:
Michael Goldenberg
2025-10-18 10:36:26 -04:00
committed by Damir Jelić
parent 4d4ae79b7a
commit 780b782660
3 changed files with 40 additions and 29 deletions
@@ -26,8 +26,9 @@ use crate::{
error::AsyncErrorDeps,
event_cache_store::{
serializer::indexed_types::{
IndexedChunkIdKey, IndexedEventIdKey, IndexedEventPositionKey, IndexedEventRelationKey,
IndexedEventRoomKey, IndexedGapIdKey, IndexedLeaseIdKey, IndexedNextChunkIdKey,
IndexedChunk, IndexedChunkIdKey, IndexedEventIdKey, IndexedEventPositionKey,
IndexedEventRelationKey, IndexedEventRoomKey, IndexedGapIdKey, IndexedLeaseIdKey,
IndexedNextChunkIdKey,
},
types::{Chunk, ChunkType, Event, Gap, Lease, Position},
},
@@ -248,9 +249,11 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
/// Add a chunk and ensure that the next and previous
/// chunks are properly linked to the chunk being added. If a chunk with
/// the same identifier already exists, the given chunk will be
/// rejected.
pub async fn add_chunk(&self, chunk: &Chunk) -> Result<(), TransactionError> {
self.add_item(chunk).await?;
/// rejected. When the item is successfully added, the
/// function returns the intermediary type [`IndexedChunk`] in case
/// inspection is needed.
pub async fn add_chunk(&self, chunk: &Chunk) -> Result<IndexedChunk, TransactionError> {
let indexed = self.add_item(chunk).await?;
if let Some(previous) = chunk.previous {
let previous_identifier = ChunkIdentifier::new(previous);
if let Some(mut previous_chunk) =
@@ -269,7 +272,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
self.put_item(&next_chunk).await?;
}
}
Ok(())
Ok(indexed)
}
/// Delete chunk that matches the given id and the given linked chunk id and
@@ -24,11 +24,12 @@ use ruma::MxcUri;
use crate::{
media_store::{
serializer::indexed_types::{
IndexedCoreIdKey, IndexedLeaseIdKey, IndexedMediaContentIdKey,
IndexedMediaContentSizeKey, IndexedMediaIdKey, IndexedMediaLastAccessKey,
IndexedMediaMetadataContentSizeKey, IndexedMediaMetadataIdKey,
IndexedMediaMetadataLastAccessKey, IndexedMediaMetadataRetentionKey,
IndexedMediaMetadataUriKey, IndexedMediaRetentionMetadataKey, IndexedMediaUriKey,
IndexedCoreIdKey, IndexedLeaseIdKey, IndexedMedia, IndexedMediaContent,
IndexedMediaContentIdKey, IndexedMediaContentSizeKey, IndexedMediaIdKey,
IndexedMediaLastAccessKey, IndexedMediaMetadata, IndexedMediaMetadataContentSizeKey,
IndexedMediaMetadataIdKey, IndexedMediaMetadataLastAccessKey,
IndexedMediaMetadataRetentionKey, IndexedMediaMetadataUriKey,
IndexedMediaRetentionMetadataKey, IndexedMediaUriKey,
},
types::{Lease, Media, MediaCleanupTime, MediaContent, MediaMetadata, UnixTime},
},
@@ -223,8 +224,10 @@ impl<'a> IndexeddbMediaStoreTransaction<'a> {
}
/// Adds [`Media`] to IndexedDB. If an item with the same key already
/// exists, it will be rejected.
pub async fn add_media(&self, media: &Media) -> Result<(), TransactionError> {
/// exists, it will be rejected. When the item is successfully added, the
/// function returns the intermediary type [`IndexedMedia`] in case
/// inspection is needed.
pub async fn add_media(&self, media: &Media) -> Result<IndexedMedia, TransactionError> {
self.add_item(media).await
}
@@ -448,11 +451,13 @@ impl<'a> IndexeddbMediaStoreTransaction<'a> {
}
/// Adds [`MediaMetadata`] to IndexedDB. If an item with the same key
/// already exists, it will be rejected.
/// already exists, it will be rejected. When the item is successfully
/// added, the function returns the intermediary type
/// [`IndexedMediaMetadata`] in case inspection is needed.
pub async fn add_media_metadata(
&self,
media_metadata: &MediaMetadata,
) -> Result<(), TransactionError> {
) -> Result<IndexedMediaMetadata, TransactionError> {
self.add_item(media_metadata).await
}
@@ -603,8 +608,13 @@ impl<'a> IndexeddbMediaStoreTransaction<'a> {
}
/// Adds [`MediaContent`] to IndexedDB. If an item with the same key already
/// exists, it will be rejected.
pub async fn add_media_content(&self, content: &MediaContent) -> Result<(), TransactionError> {
/// exists, it will be rejected. When the item is successfully added, the
/// function returns the intermediary type [`IndexedMediaContent`] in case
/// inspection is needed.
pub async fn add_media_content(
&self,
content: &MediaContent,
) -> Result<IndexedMediaContent, TransactionError> {
self.add_item(content).await
}
@@ -359,23 +359,21 @@ impl<'a> Transaction<'a> {
/// Adds an item to the corresponding IndexedDB object
/// store, i.e., `T::OBJECT_STORE`. If an item with the same key already
/// exists, it will be rejected.
pub async fn add_item<T>(&self, item: &T) -> Result<(), TransactionError>
/// exists, it will be rejected. When the item is successfully added, the
/// function returns the intermediary type [`Indexed::IndexedType`] in case
/// inspection is needed.
pub async fn add_item<T>(&self, item: &T) -> Result<T::IndexedType, TransactionError>
where
T: Indexed + Serialize,
T::IndexedType: Serialize,
T::Error: AsyncErrorDeps,
{
self.transaction
.object_store(T::OBJECT_STORE)?
.add(
self.serializer
.serialize(item)
.map_err(|e| TransactionError::Serialization(Box::new(e)))?
.value,
)
.await
.map_err(Into::into)
let output = self
.serializer
.serialize(item)
.map_err(|e| TransactionError::Serialization(Box::new(e)))?;
self.transaction.object_store(T::OBJECT_STORE)?.add(output.value).await?;
Ok(output.indexed)
}
/// Puts an item in the corresponding IndexedDB object