Skip to content

Commit 7bfb2ab

Browse files
committed
Add web feature flag to the rust sdk
1 parent 4dc0f71 commit 7bfb2ab

5 files changed

Lines changed: 70 additions & 57 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/sdk/Cargo.toml

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ description = "A Rust SDK for clients to interface with SpacetimeDB"
77

88
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
99

10+
[features]
11+
default = []
12+
web = [
13+
"dep:getrandom",
14+
"dep:gloo-console",
15+
"dep:gloo-storage",
16+
"dep:rustls-pki-types",
17+
"dep:tokio-tungstenite-wasm",
18+
"dep:wasm-bindgen",
19+
"dep:wasm-bindgen-futures",
20+
"dep:web-sys",
21+
]
22+
1023
[dependencies]
1124
spacetimedb-data-structures.workspace = true
1225
spacetimedb-sats.workspace = true
@@ -27,24 +40,26 @@ once_cell.workspace = true
2740
prometheus.workspace = true
2841
rand.workspace = true
2942

43+
getrandom = { version = "0.3.2", features = ["wasm_js"], optional = true }
44+
gloo-console = { version = "0.3.0", optional = true }
45+
gloo-storage = { version = "0.3.0", optional = true }
46+
rustls-pki-types = { version = "1.12.0", features = ["web"], optional = true }
47+
tokio-tungstenite-wasm = { version = "0.6.0", optional = true }
48+
wasm-bindgen = { version = "0.2.100", optional = true }
49+
wasm-bindgen-futures = { version = "0.4.45", optional = true }
50+
web-sys = { version = "0.3.77", features = [
51+
"Document", "HtmlDocument", "Window"
52+
], optional = true}
53+
3054
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
3155
home.workspace = true
3256
tokio.workspace = true
3357
tokio-tungstenite.workspace = true
3458

3559
[target.'cfg(target_arch = "wasm32")'.dependencies]
36-
getrandom = { version = "0.3.2", features = ["wasm_js"]}
37-
gloo-console = "0.3.0"
38-
gloo-storage = "0.3.0"
39-
rustls-pki-types = { version = "1.11.0", features = ["web"] }
4060
tokio = { version = "1.37", default-features = false, features = [
4161
"rt", "macros", "sync", "io-util"
4262
] }
43-
tokio-tungstenite-wasm = "0.6.0"
44-
tungstenite = { version = "0.26.2", features = ["rustls"] }
45-
wasm-bindgen = "0.2.100"
46-
wasm-bindgen-futures = "0.4.45"
47-
web-sys = { version = "0.3.77", features = [ "Document", "HtmlDocument", "Window" ] }
4863

4964
[dev-dependencies]
5065
# for quickstart-chat and cursive-chat examples

crates/sdk/src/credentials.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! }
99
//! ```
1010
11-
#[cfg(not(target_arch = "wasm32"))]
11+
#[cfg(not(feature = "web"))]
1212
mod native_mod {
1313
use home::home_dir;
1414
use spacetimedb_lib::{bsatn, de::Deserialize, ser::Serialize};
@@ -153,7 +153,7 @@ mod native_mod {
153153
}
154154
}
155155

156-
#[cfg(target_arch = "wasm32")]
156+
#[cfg(feature = "web")]
157157
mod web_mod {
158158
pub use gloo_storage::{LocalStorage, SessionStorage, Storage};
159159

@@ -319,8 +319,8 @@ mod web_mod {
319319
}
320320
}
321321

322-
#[cfg(not(target_arch = "wasm32"))]
322+
#[cfg(not(feature = "web"))]
323323
pub use native_mod::*;
324324

325-
#[cfg(target_arch = "wasm32")]
325+
#[cfg(feature = "web")]
326326
pub use web_mod::*;

crates/sdk/src/db_connection.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use std::{
4141
sync::{atomic::AtomicU32, Arc, Mutex as StdMutex, OnceLock},
4242
};
4343
use tokio::runtime::{self, Runtime};
44-
#[cfg(not(target_arch = "wasm32"))]
44+
#[cfg(not(feature = "web"))]
4545
use tokio::sync::Mutex as TokioMutex;
4646

4747
pub(crate) type SharedCell<T> = Arc<StdMutex<T>>;
@@ -65,9 +65,9 @@ pub struct DbContextImpl<M: SpacetimeModule> {
6565

6666
/// Receiver channel for WebSocket messages,
6767
/// which are pre-parsed in the background by [`parse_loop`].
68-
#[cfg(not(target_arch = "wasm32"))]
68+
#[cfg(not(feature = "web"))]
6969
recv: Arc<TokioMutex<mpsc::UnboundedReceiver<ParsedMessage<M>>>>,
70-
#[cfg(target_arch = "wasm32")]
70+
#[cfg(feature = "web")]
7171
recv: SharedCell<mpsc::UnboundedReceiver<ParsedMessage<M>>>,
7272

7373
/// Channel into which operations which apparently mutate SDK state,
@@ -78,9 +78,9 @@ pub struct DbContextImpl<M: SpacetimeModule> {
7878

7979
/// Receive end of `pending_mutations_send`,
8080
/// from which [Self::apply_pending_mutations] and friends read mutations.
81-
#[cfg(not(target_arch = "wasm32"))]
81+
#[cfg(not(feature = "web"))]
8282
pending_mutations_recv: Arc<TokioMutex<mpsc::UnboundedReceiver<PendingMutation<M>>>>,
83-
#[cfg(target_arch = "wasm32")]
83+
#[cfg(feature = "web")]
8484
pending_mutations_recv: SharedCell<mpsc::UnboundedReceiver<PendingMutation<M>>>,
8585

8686
/// This connection's `Identity`.
@@ -264,12 +264,12 @@ impl<M: SpacetimeModule> DbContextImpl<M> {
264264

265265
/// Apply all queued [`PendingMutation`]s.
266266
fn apply_pending_mutations(&self) -> crate::Result<()> {
267-
#[cfg(not(target_arch = "wasm32"))]
267+
#[cfg(not(feature = "web"))]
268268
while let Ok(Some(pending_mutation)) = self.pending_mutations_recv.blocking_lock().try_next() {
269269
self.apply_mutation(pending_mutation)?;
270270
}
271271

272-
#[cfg(target_arch = "wasm32")]
272+
#[cfg(feature = "web")]
273273
while let Ok(Some(pending_mutation)) = self.pending_mutations_recv.lock().unwrap().try_next() {
274274
self.apply_mutation(pending_mutation)?;
275275
}
@@ -489,10 +489,10 @@ impl<M: SpacetimeModule> DbContextImpl<M> {
489489
// model or not.
490490

491491
let res = {
492-
#[cfg(not(target_arch = "wasm32"))]
492+
#[cfg(not(feature = "web"))]
493493
let mut recv = self.recv.blocking_lock();
494494

495-
#[cfg(target_arch = "wasm32")]
495+
#[cfg(feature = "web")]
496496
let mut recv = self.recv.lock().unwrap();
497497

498498
match recv.try_next() {
@@ -519,14 +519,14 @@ impl<M: SpacetimeModule> DbContextImpl<M> {
519519
// We call this out as an incorrect and unsupported thing to do.
520520
#![allow(clippy::await_holding_lock)]
521521

522-
#[cfg(not(target_arch = "wasm32"))]
522+
#[cfg(not(feature = "web"))]
523523
let mut pending_mutations = self.pending_mutations_recv.lock().await;
524-
#[cfg(target_arch = "wasm32")]
524+
#[cfg(feature = "web")]
525525
let mut pending_mutations = self.pending_mutations_recv.lock().unwrap();
526526

527-
#[cfg(not(target_arch = "wasm32"))]
527+
#[cfg(not(feature = "web"))]
528528
let mut recv = self.recv.lock().await;
529-
#[cfg(target_arch = "wasm32")]
529+
#[cfg(feature = "web")]
530530
let mut recv = self.recv.lock().unwrap();
531531

532532
// Always process pending mutations before WS messages, if they're available,
@@ -585,7 +585,7 @@ impl<M: SpacetimeModule> DbContextImpl<M> {
585585
/// Spawn a thread which does [`Self::advance_one_message_blocking`] in a loop.
586586
///
587587
/// Called by the autogenerated `DbConnection` method of the same name.
588-
#[cfg(not(target_arch = "wasm32"))]
588+
#[cfg(not(feature = "web"))]
589589
pub fn run_threaded(&self) -> std::thread::JoinHandle<()> {
590590
let this = self.clone();
591591
std::thread::spawn(move || loop {
@@ -597,7 +597,7 @@ impl<M: SpacetimeModule> DbContextImpl<M> {
597597
})
598598
}
599599

600-
#[cfg(target_arch = "wasm32")]
600+
#[cfg(feature = "web")]
601601
pub fn run_threaded(&self) {
602602
let this = self.clone();
603603
wasm_bindgen_futures::spawn_local(async move {
@@ -865,21 +865,21 @@ You must explicitly advance the connection by calling any one of:
865865
Which of these methods you should call depends on the specific needs of your application,
866866
but you must call one of them, or else the connection will never progress.
867867
"]
868-
#[cfg(not(target_arch = "wasm32"))]
868+
#[cfg(not(feature = "web"))]
869869
pub fn build(self) -> crate::Result<M::DbConnection> {
870870
let imp = self.build_impl()?;
871871
Ok(<M::DbConnection as DbConnection>::new(imp))
872872
}
873873

874-
#[cfg(target_arch = "wasm32")]
874+
#[cfg(feature = "web")]
875875
pub async fn build(self) -> crate::Result<M::DbConnection> {
876876
let imp = self.build_impl().await?;
877877
Ok(<M::DbConnection as DbConnection>::new(imp))
878878
}
879879

880880
/// Open a WebSocket connection, build an empty client cache, &c,
881881
/// to construct a [`DbContextImpl`].
882-
#[cfg(not(target_arch = "wasm32"))]
882+
#[cfg(not(feature = "web"))]
883883
fn build_impl(self) -> crate::Result<DbContextImpl<M>> {
884884
let (runtime, handle) = enter_or_create_runtime()?;
885885
let db_callbacks = DbCallbacks::default();
@@ -934,7 +934,7 @@ but you must call one of them, or else the connection will never progress.
934934
Ok(ctx_imp)
935935
}
936936

937-
#[cfg(target_arch = "wasm32")]
937+
#[cfg(feature = "web")]
938938
pub async fn build_impl(self) -> crate::Result<DbContextImpl<M>> {
939939
let (runtime, handle) = enter_or_create_runtime()?;
940940
let db_callbacks = DbCallbacks::default();
@@ -1104,9 +1104,9 @@ Instead of registering multiple `on_disconnect` callbacks, register a single cal
11041104
fn enter_or_create_runtime() -> crate::Result<(Option<Runtime>, runtime::Handle)> {
11051105
match runtime::Handle::try_current() {
11061106
Err(e) if e.is_missing_context() => {
1107-
#[cfg(not(target_arch = "wasm32"))]
1107+
#[cfg(not(feature = "web"))]
11081108
let mut rt = tokio::runtime::Builder::new_multi_thread();
1109-
#[cfg(target_arch = "wasm32")]
1109+
#[cfg(feature = "web")]
11101110
let mut rt = tokio::runtime::Builder::new_current_thread();
11111111

11121112
let rt = rt
@@ -1138,7 +1138,7 @@ enum ParsedMessage<M: SpacetimeModule> {
11381138
Error(crate::Error),
11391139
}
11401140

1141-
#[cfg(not(target_arch = "wasm32"))]
1141+
#[cfg(not(feature = "web"))]
11421142
fn spawn_parse_loop<M: SpacetimeModule>(
11431143
raw_message_recv: mpsc::UnboundedReceiver<ws::ServerMessage<BsatnFormat>>,
11441144
handle: &runtime::Handle,
@@ -1148,7 +1148,7 @@ fn spawn_parse_loop<M: SpacetimeModule>(
11481148
(handle, parsed_message_recv)
11491149
}
11501150

1151-
#[cfg(target_arch = "wasm32")]
1151+
#[cfg(feature = "web")]
11521152
fn spawn_parse_loop<M: SpacetimeModule>(
11531153
raw_message_recv: mpsc::UnboundedReceiver<ws::ServerMessage<BsatnFormat>>,
11541154
) -> mpsc::UnboundedReceiver<ParsedMessage<M>> {

0 commit comments

Comments
 (0)