Skip to content

Commit 9c08cd8

Browse files
mgoldenbergHywan
authored andcommitted
feat(indexeddb): add type for building IndexeddbEventCacheStore
Signed-off-by: Michael Goldenberg <[email protected]>
1 parent e0ceef3 commit 9c08cd8

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2025 The Matrix.org Foundation C.I.C.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License
14+
15+
use std::sync::Arc;
16+
17+
use matrix_sdk_base::event_cache::store::MemoryStore;
18+
use matrix_sdk_store_encryption::StoreCipher;
19+
use web_sys::DomException;
20+
21+
use crate::{
22+
event_cache_store::{
23+
migrations::open_and_upgrade_db, serializer::IndexeddbEventCacheStoreSerializer,
24+
IndexeddbEventCacheStore,
25+
},
26+
serializer::IndexeddbSerializer,
27+
};
28+
29+
/// A type for conveniently building an [`IndexeddbEventCacheStore`]
30+
pub struct IndexeddbEventCacheStoreBuilder {
31+
// The name of the IndexedDB database which will be opened
32+
database_name: String,
33+
// The store cipher, if any, to use when encrypting data
34+
// before it is persisted to the IndexedDB database
35+
store_cipher: Option<Arc<StoreCipher>>,
36+
}
37+
38+
impl Default for IndexeddbEventCacheStoreBuilder {
39+
fn default() -> Self {
40+
Self { database_name: Self::DEFAULT_DATABASE_NAME.to_owned(), store_cipher: None }
41+
}
42+
}
43+
44+
impl IndexeddbEventCacheStoreBuilder {
45+
/// The default name of the IndexedDB database used to back the
46+
/// [`IndexeddbEventCacheStore`]
47+
pub const DEFAULT_DATABASE_NAME: &'static str = "event_cache";
48+
49+
/// Sets the name of the IndexedDB database which will be opened. This
50+
/// defaults to [`Self::DEFAULT_DATABASE_NAME`].
51+
pub fn database_name(mut self, name: String) -> Self {
52+
self.database_name = name;
53+
self
54+
}
55+
56+
/// Sets the store cipher to use when encrypting data before it is persisted
57+
/// to the IndexedDB database. By default, no store cipher is used -
58+
/// i.e., data is not encrypted before it is persisted.
59+
pub fn store_cipher(mut self, store_cipher: Arc<StoreCipher>) -> Self {
60+
self.store_cipher = Some(store_cipher);
61+
self
62+
}
63+
64+
/// Opens the IndexedDB database with the provided name. If successfully
65+
/// opened, builds the [`IndexeddbEventCacheStore`] with that database
66+
/// and the provided store cipher.
67+
pub async fn build(self) -> Result<IndexeddbEventCacheStore, DomException> {
68+
Ok(IndexeddbEventCacheStore {
69+
inner: open_and_upgrade_db(&self.database_name).await?,
70+
serializer: IndexeddbEventCacheStoreSerializer::new(IndexeddbSerializer::new(
71+
self.store_cipher,
72+
)),
73+
memory_store: MemoryStore::new(),
74+
})
75+
}
76+
}

crates/matrix-sdk-indexeddb/src/event_cache_store/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ use web_sys::IdbTransactionMode;
2020

2121
use crate::event_cache_store::serializer::IndexeddbEventCacheStoreSerializer;
2222

23+
mod builder;
2324
mod error;
2425
mod migrations;
2526
mod serializer;
2627
mod transaction;
2728
mod types;
2829

30+
pub use builder::IndexeddbEventCacheStoreBuilder;
31+
2932
/// A type for providing an IndexedDB implementation of [`EventCacheStore`][1].
3033
/// This is meant to be used as a backend to [`EventCacheStore`][1] in browser
3134
/// contexts.
@@ -43,3 +46,11 @@ pub struct IndexeddbEventCacheStore {
4346
// functions in `EventCacheStore`.
4447
memory_store: MemoryStore,
4548
}
49+
50+
impl IndexeddbEventCacheStore {
51+
/// Provides a type with which to conveniently build an
52+
/// [`IndexeddbEventCacheStore`]
53+
pub fn builder() -> IndexeddbEventCacheStoreBuilder {
54+
IndexeddbEventCacheStoreBuilder::default()
55+
}
56+
}

0 commit comments

Comments
 (0)