-
Notifications
You must be signed in to change notification settings - Fork 269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(event cache store): add an IndexedDB implementation #4617
base: main
Are you sure you want to change the base?
Changes from 11 commits
6955428
b8bfd3e
c2761d5
3c6ca6a
7eb8946
64e68ba
1328003
eebe00e
e49bda6
1b1029d
c317bd8
0f8ebf1
ee40ddd
73713ac
d3b6f87
8528522
7622be4
01cb5ba
8f2b427
722bac2
4ca221f
0e05ac8
28647b6
eee0bad
0b0e15e
993283c
d755cc0
abee3ab
06b0395
ae25b35
4dcf22a
610860b
ebda277
1af016f
6d8b405
4179750
6c7b729
c686f90
8cfe19c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use matrix_sdk_base::{event_cache::store::EventCacheStoreError, StoreError}; | ||
use matrix_sdk_crypto::store::CryptoStoreError; | ||
use matrix_sdk_store_encryption::Error as EncryptionError; | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum IndexeddbEventCacheStoreError { | ||
#[error(transparent)] | ||
Json(#[from] serde_json::Error), | ||
#[error(transparent)] | ||
Encryption(#[from] EncryptionError), | ||
#[error("DomException {name} ({code}): {message}")] | ||
DomException { name: String, message: String, code: u16 }, | ||
#[error(transparent)] | ||
StoreError(#[from] StoreError), | ||
#[error("Can't migrate {name} from {old_version} to {new_version} without deleting data. See MigrationConflictStrategy for ways to configure.")] | ||
MigrationConflict { name: String, old_version: u32, new_version: u32 }, | ||
#[error(transparent)] | ||
CryptoStoreError(#[from] CryptoStoreError), | ||
} | ||
|
||
impl From<web_sys::DomException> for IndexeddbEventCacheStoreError { | ||
fn from(frm: web_sys::DomException) -> IndexeddbEventCacheStoreError { | ||
IndexeddbEventCacheStoreError::DomException { | ||
name: frm.name(), | ||
message: frm.message(), | ||
code: frm.code(), | ||
} | ||
} | ||
} | ||
|
||
impl From<IndexeddbEventCacheStoreError> for StoreError { | ||
fn from(e: IndexeddbEventCacheStoreError) -> Self { | ||
match e { | ||
IndexeddbEventCacheStoreError::Json(e) => StoreError::Json(e), | ||
IndexeddbEventCacheStoreError::StoreError(e) => e, | ||
IndexeddbEventCacheStoreError::Encryption(e) => StoreError::Encryption(e), | ||
_ => StoreError::backend(e), | ||
} | ||
} | ||
} | ||
|
||
impl From<IndexeddbEventCacheStoreError> for EventCacheStoreError { | ||
fn from(e: IndexeddbEventCacheStoreError) -> Self { | ||
match e { | ||
IndexeddbEventCacheStoreError::Json(e) => EventCacheStoreError::Serialization(e), | ||
IndexeddbEventCacheStoreError::StoreError(e) => { | ||
EventCacheStoreError::Backend(Box::new(e)) | ||
} | ||
IndexeddbEventCacheStoreError::Encryption(e) => EventCacheStoreError::Encryption(e), | ||
_ => EventCacheStoreError::backend(e), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. backend => Backend, right? |
||
} | ||
} | ||
} | ||
|
||
impl From<IndexeddbEventCacheStoreError> for CryptoStoreError { | ||
fn from(frm: IndexeddbEventCacheStoreError) -> CryptoStoreError { | ||
match frm { | ||
IndexeddbEventCacheStoreError::Json(e) => CryptoStoreError::Serialization(e), | ||
IndexeddbEventCacheStoreError::CryptoStoreError(e) => e, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would think this needs a wrapper as well, right? |
||
_ => CryptoStoreError::backend(frm), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/backend/Backend/ |
||
} | ||
} | ||
} | ||
|
||
impl From<serde_wasm_bindgen::Error> for IndexeddbEventCacheStoreError { | ||
fn from(e: serde_wasm_bindgen::Error) -> Self { | ||
IndexeddbEventCacheStoreError::Json(serde::de::Error::custom(e.to_string())) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use gloo_utils::format::JsValueSerdeExt; | ||
use indexed_db_futures::idb_object_store::IdbObjectStore; | ||
use serde::{Deserialize, Serialize}; | ||
use wasm_bindgen::JsValue; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct Chunk { | ||
id: String, | ||
previous: Option<u64>, | ||
new: u64, | ||
next: Option<u64>, | ||
} | ||
|
||
pub async fn insert_chunk( | ||
store: IdbObjectStore<'_>, | ||
hashed_room_id: &String, | ||
previous: Option<u64>, | ||
new: u64, | ||
next: Option<u64>, | ||
) -> Result<(), web_sys::DomException> { | ||
let id = format!("{}-{}", hashed_room_id, new); | ||
let chunk = Chunk { id, previous, new, next }; | ||
let value = JsValue::from_serde(&chunk).unwrap(); | ||
store.add_val(&value)?; | ||
ospfranco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Ok(()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this compile, I'm surprised it doesn't need a wrapper for StoreError here?