From 1a24b21d4244ecb0057bef7ecf30fc65fb13e149 Mon Sep 17 00:00:00 2001 From: Michael Goldenberg Date: Mon, 23 Jun 2025 17:38:55 -0400 Subject: [PATCH] refactor(indexeddb): add type to represent IndexedDB transactions specific to event cache store Signed-off-by: Michael Goldenberg --- .../src/event_cache_store/mod.rs | 1 + .../src/event_cache_store/serializer/mod.rs | 4 +- .../src/event_cache_store/transaction.rs | 69 +++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 crates/matrix-sdk-indexeddb/src/event_cache_store/transaction.rs diff --git a/crates/matrix-sdk-indexeddb/src/event_cache_store/mod.rs b/crates/matrix-sdk-indexeddb/src/event_cache_store/mod.rs index 9211bdb0c..fc29522ff 100644 --- a/crates/matrix-sdk-indexeddb/src/event_cache_store/mod.rs +++ b/crates/matrix-sdk-indexeddb/src/event_cache_store/mod.rs @@ -16,4 +16,5 @@ mod migrations; mod serializer; +mod transaction; mod types; diff --git a/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/mod.rs b/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/mod.rs index 7aa62565c..9b68ca04e 100644 --- a/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/mod.rs +++ b/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/mod.rs @@ -28,8 +28,8 @@ use crate::{ serializer::IndexeddbSerializer, }; -mod traits; -mod types; +pub mod traits; +pub mod types; #[derive(Debug, Error)] pub enum IndexeddbEventCacheStoreSerializerError { diff --git a/crates/matrix-sdk-indexeddb/src/event_cache_store/transaction.rs b/crates/matrix-sdk-indexeddb/src/event_cache_store/transaction.rs new file mode 100644 index 000000000..342da5b1c --- /dev/null +++ b/crates/matrix-sdk-indexeddb/src/event_cache_store/transaction.rs @@ -0,0 +1,69 @@ +// Copyright 2025 The Matrix.org Foundation C.I.C. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License + +use indexed_db_futures::{prelude::IdbTransaction, IdbQuerySource}; +use matrix_sdk_base::{ + event_cache::{Event as RawEvent, Gap as RawGap}, + linked_chunk::{ChunkContent, ChunkIdentifier, RawChunk}, +}; +use ruma::{events::relation::RelationType, OwnedEventId, RoomId}; +use serde::{de::DeserializeOwned, Serialize}; +use thiserror::Error; +use web_sys::IdbCursorDirection; + +use crate::event_cache_store::{ + serializer::{ + traits::{Indexed, IndexedKey, IndexedKeyBounds, IndexedKeyComponentBounds}, + types::{ + IndexedChunkIdKey, IndexedEventIdKey, IndexedEventPositionKey, IndexedEventRelationKey, + IndexedGapIdKey, IndexedKeyRange, IndexedNextChunkIdKey, + }, + IndexeddbEventCacheStoreSerializer, + }, + types::{Chunk, ChunkType, Event, Gap, Position}, +}; + +#[derive(Debug, Error)] +pub enum IndexeddbEventCacheStoreTransactionError { + #[error("DomException {name} ({code}): {message}")] + DomException { name: String, message: String, code: u16 }, +} + +impl From for IndexeddbEventCacheStoreTransactionError { + fn from(value: web_sys::DomException) -> Self { + Self::DomException { name: value.name(), message: value.message(), code: value.code() } + } +} + +pub struct IndexeddbEventCacheStoreTransaction<'a> { + transaction: IdbTransaction<'a>, + serializer: &'a IndexeddbEventCacheStoreSerializer, +} + +impl<'a> IndexeddbEventCacheStoreTransaction<'a> { + pub fn new( + transaction: IdbTransaction<'a>, + serializer: &'a IndexeddbEventCacheStoreSerializer, + ) -> Self { + Self { transaction, serializer } + } + + pub fn into_inner(self) -> IdbTransaction<'a> { + self.transaction + } + + pub async fn commit(self) -> Result<(), IndexeddbEventCacheStoreTransactionError> { + self.transaction.await.into_result().map_err(Into::into) + } +}