-
Notifications
You must be signed in to change notification settings - Fork 303
feat(ffi): Add ability to configure indexedb instead of sqlite for wasm platforms #5181
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
Conversation
a1e186e
to
3d39cdd
Compare
PR title should probably make it clear that this is about the FFI crate :) |
The challenge is to maintain a compatible -ffi API, despite the fact that both cannot be used on both platforms. Approach is to use runtime panics here to stop usage if it is being misused.
3d39cdd
to
9ace356
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #5181 +/- ##
==========================================
- Coverage 85.87% 85.86% -0.02%
==========================================
Files 338 338
Lines 37070 37070
==========================================
- Hits 31834 31830 -4
- Misses 5236 5240 +4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Thanks for the patch.
First off, I believe you should split all the modifications from the Cargo.toml
file into its own patch.
Second, I think you can simplify things by introducing a type alias SessionConfig
. We don't want to expose SQLite or IndexedDb directly, we want to hide that from the consumer/user. See my suggestions.
What do you think?
@@ -14,17 +14,17 @@ publish = false | |||
release = true | |||
|
|||
[lib] | |||
crate-type = ["cdylib", "staticlib"] | |||
crate-type = ["cdylib", "staticlib", "lib"] |
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.
I'm not sure it's related to what the patch says it does. Can you split this in another patch please?
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.
Yeah this seems weird, what's the idea with this?
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.
The uniffi bindings generator requires the library to be built as a "lib" so it can be included in a second generated-rust library that is fed through. But I will break that out into a second PR.
bindings/matrix-sdk-ffi/Cargo.toml
Outdated
[target.'cfg(target_family = "wasm")'.dependencies.matrix-sdk] | ||
workspace = true | ||
features = [ | ||
"anyhow", | ||
"e2e-encryption", | ||
"experimental-widgets", | ||
"markdown", | ||
"rustls-tls", | ||
"socks", | ||
"indexeddb", | ||
"uniffi", | ||
] |
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.
All the changes in Cargo.toml
just be in a dedicated patch.
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.
Opened up #5211 which is nearly merged. I will rebase this branch with those changes (and other feedback) once it lands.
pub fn username(self: Arc<Self>, username: String) -> Arc<Self> { | ||
#[derive(Clone, uniffi::Object)] | ||
pub struct IndexedDbSessionBuilder { | ||
#[cfg_attr(not(target_family = "wasm"), allow(unused))] |
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.
We don't use IndexedDb
outside Wasm, all fields can be present.
#[derive(Clone)] | ||
pub enum ClientSessionConfig { | ||
/// Setup the client to use the SQLite store. | ||
#[cfg_attr(target_family = "wasm", allow(unused))] | ||
Sqlite(SqliteSessionBuilder), | ||
|
||
/// Setup the client to use the IndexedDB store. | ||
#[cfg_attr(not(target_family = "wasm"), allow(unused))] | ||
IndexedDb(IndexedDbSessionBuilder), | ||
} |
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.
I think you can remove all this, and add:
#[derive(Clone)] | |
pub enum ClientSessionConfig { | |
/// Setup the client to use the SQLite store. | |
#[cfg_attr(target_family = "wasm", allow(unused))] | |
Sqlite(SqliteSessionBuilder), | |
/// Setup the client to use the IndexedDB store. | |
#[cfg_attr(not(target_family = "wasm"), allow(unused))] | |
IndexedDb(IndexedDbSessionBuilder), | |
} | |
#[cfg(target_family = "wasm")] | |
pub type SessionConfig = IndexedDbSessionBuilder; | |
#[cfg(not(target_family = "wasm"))] | |
pub type SessionConfig = SqliteSessionBuilder; |
|
||
#[derive(Clone, uniffi::Object)] | ||
pub struct ClientBuilder { | ||
session: Option<ClientSessionConfig>, |
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.
Then…
session: Option<ClientSessionConfig>, | |
session: Option<SessionConfig> |
pub fn session_sqlite(self: Arc<Self>, config: Arc<SqliteSessionBuilder>) -> Arc<Self> { | ||
let mut builder = unwrap_or_clone_arc(self); | ||
builder.homeserver_cfg = Some(HomeserverConfig::ServerName(server_name)); | ||
builder.session = Some(ClientSessionConfig::Sqlite(config.as_ref().clone())); | ||
Arc::new(builder) | ||
} | ||
|
||
pub fn homeserver_url(self: Arc<Self>, url: String) -> Arc<Self> { | ||
pub fn session_indexeddb(self: Arc<Self>, config: Arc<IndexedDbSessionBuilder>) -> Arc<Self> { | ||
let mut builder = unwrap_or_clone_arc(self); | ||
builder.homeserver_cfg = Some(HomeserverConfig::Url(url)); | ||
builder.session = Some(ClientSessionConfig::IndexedDb(config.as_ref().clone())); | ||
Arc::new(builder) | ||
} |
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.
And finally…
pub fn session_sqlite(self: Arc<Self>, config: Arc<SqliteSessionBuilder>) -> Arc<Self> { | |
let mut builder = unwrap_or_clone_arc(self); | |
builder.homeserver_cfg = Some(HomeserverConfig::ServerName(server_name)); | |
builder.session = Some(ClientSessionConfig::Sqlite(config.as_ref().clone())); | |
Arc::new(builder) | |
} | |
pub fn homeserver_url(self: Arc<Self>, url: String) -> Arc<Self> { | |
pub fn session_indexeddb(self: Arc<Self>, config: Arc<IndexedDbSessionBuilder>) -> Arc<Self> { | |
let mut builder = unwrap_or_clone_arc(self); | |
builder.homeserver_cfg = Some(HomeserverConfig::Url(url)); | |
builder.session = Some(ClientSessionConfig::IndexedDb(config.as_ref().clone())); | |
Arc::new(builder) | |
} | |
pub fn session(self: Arc<Self>, config: Arc<SessionConfig>) -> Arc<Self> { | |
let mut builder = unwrap_or_clone_arc(self); | |
builder.session = Some(config); | |
Arc::new(builder) | |
} |
Co-authored-by: Ivan Enderlin <[email protected]> Signed-off-by: Daniel Salinas <[email protected]>
Conditionalize uniffi exports when this flag is not present
Introduces
SqliteSessionBuilder
andIndexedDbSessionBuilder
to allow for a choice of session store. Non-wasm platforms will only support sqlite, while Wasm platforms will support indexedb. In order to maintain a compatible-ffi, these restrictions are handled at runtime rather than compile time.We also conditionally remove things related to network features (proxy, ssl validation, etc), that are not available on wasm platforms.
Signed-off-by: Daniel Salinas