Skip to content

Commit ba595d7

Browse files
committed
Rename misleading run_threaded to run_background on wasm32
Renamed the `run_threaded` method on `wasm32` to better reflect its behavior of spawning a background task. The generated `DbConnection` methods `run_threaded`, `run_background`, and `advance_one_message_blocking` now include runtime panics with a clear error feedback when called on unsupported targets.
1 parent 46d94b4 commit ba595d7

3 files changed

Lines changed: 83 additions & 14 deletions

File tree

crates/codegen/src/rust.rs

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,6 +1375,7 @@ impl __sdk::InModule for RemoteTables {{
13751375
///
13761376
/// - [`DbConnection::frame_tick`].
13771377
/// - [`DbConnection::run_threaded`].
1378+
/// - [`DbConnection::run_background`].
13781379
/// - [`DbConnection::run_async`].
13791380
/// - [`DbConnection::advance_one_message`].
13801381
/// - [`DbConnection::advance_one_message_blocking`].
@@ -1484,8 +1485,19 @@ impl DbConnection {{
14841485
/// This is a low-level primitive exposed for power users who need significant control over scheduling.
14851486
/// Most applications should call [`Self::run_threaded`] to spawn a thread
14861487
/// which advances the connection automatically.
1488+
///
1489+
/// # Panics
1490+
/// At runtime if called on any `wasm32` target.
14871491
pub fn advance_one_message_blocking(&self) -> __sdk::Result<()> {{
1488-
self.imp.advance_one_message_blocking()
1492+
#[cfg(target_arch = \"wasm32\")]
1493+
{{
1494+
panic!(\"`DbConnection::advance_one_message_blocking` is not supported on WebAssembly (wasm32); \\
1495+
prefer using `advance_one_message` or `advance_one_message_async` instead\");
1496+
}}
1497+
#[cfg(not(target_arch = \"wasm32\"))]
1498+
{{
1499+
self.imp.advance_one_message_blocking()
1500+
}}
14891501
}}
14901502
14911503
/// Process one WebSocket message, `await`ing until one is received.
@@ -1509,14 +1521,35 @@ impl DbConnection {{
15091521
}}
15101522
15111523
/// Spawn a thread which processes WebSocket messages as they are received.
1512-
#[cfg(not(target_arch = \"wasm32\"))]
1524+
///
1525+
/// # Panics
1526+
/// At runtime if called on any `wasm32` target.
15131527
pub fn run_threaded(&self) -> std::thread::JoinHandle<()> {{
1514-
self.imp.run_threaded()
1528+
#[cfg(target_arch = \"wasm32\")]
1529+
{{
1530+
panic!(\"`DbConnection::run_threaded` is not supported on WebAssembly (wasm32); \\
1531+
prefer using `DbConnection::run_background` instead\");
1532+
}}
1533+
#[cfg(not(target_arch = \"wasm32\"))]
1534+
{{
1535+
self.imp.run_threaded()
1536+
}}
15151537
}}
15161538
1517-
#[cfg(target_arch = \"wasm32\")]
1518-
pub fn run_threaded(&self) {{
1519-
self.imp.run_threaded()
1539+
/// Spawn a task which processes WebSocket messages as they are received.
1540+
///
1541+
/// # Panics
1542+
/// At runtime if called on any non-`wasm32` target.
1543+
pub fn run_background(&self) {{
1544+
#[cfg(not(target_arch = \"wasm32\"))]
1545+
{{
1546+
panic!(\"`DbConnection::run_background` is only supported on WebAssembly (wasm32); \\
1547+
prefer using `DbConnection::run_threaded` instead\");
1548+
}}
1549+
#[cfg(target_arch = \"wasm32\")]
1550+
{{
1551+
self.imp.run_background()
1552+
}}
15201553
}}
15211554
15221555
/// Run an `async` loop which processes WebSocket messages when polled.

crates/codegen/tests/snapshots/codegen__codegen_rust.snap

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,6 +1806,7 @@ impl __sdk::InModule for RemoteTables {
18061806
///
18071807
/// - [`DbConnection::frame_tick`].
18081808
/// - [`DbConnection::run_threaded`].
1809+
/// - [`DbConnection::run_background`].
18091810
/// - [`DbConnection::run_async`].
18101811
/// - [`DbConnection::advance_one_message`].
18111812
/// - [`DbConnection::advance_one_message_blocking`].
@@ -1915,8 +1916,19 @@ impl DbConnection {
19151916
/// This is a low-level primitive exposed for power users who need significant control over scheduling.
19161917
/// Most applications should call [`Self::run_threaded`] to spawn a thread
19171918
/// which advances the connection automatically.
1919+
///
1920+
/// # Panics
1921+
/// At runtime if called on any `wasm32` target.
19181922
pub fn advance_one_message_blocking(&self) -> __sdk::Result<()> {
1919-
self.imp.advance_one_message_blocking()
1923+
#[cfg(target_arch = "wasm32")]
1924+
{
1925+
panic!("`DbConnection::advance_one_message_blocking` is not supported on WebAssembly (wasm32); \
1926+
prefer using `advance_one_message` or `advance_one_message_async` instead");
1927+
}
1928+
#[cfg(not(target_arch = "wasm32"))]
1929+
{
1930+
self.imp.advance_one_message_blocking()
1931+
}
19201932
}
19211933

19221934
/// Process one WebSocket message, `await`ing until one is received.
@@ -1940,14 +1952,35 @@ impl DbConnection {
19401952
}
19411953

19421954
/// Spawn a thread which processes WebSocket messages as they are received.
1943-
#[cfg(not(target_arch = "wasm32"))]
1955+
///
1956+
/// # Panics
1957+
/// At runtime if called on any `wasm32` target.
19441958
pub fn run_threaded(&self) -> std::thread::JoinHandle<()> {
1945-
self.imp.run_threaded()
1959+
#[cfg(target_arch = "wasm32")]
1960+
{
1961+
panic!("`DbConnection::run_threaded` is not supported on WebAssembly (wasm32); \
1962+
prefer using `DbConnection::run_background` instead");
1963+
}
1964+
#[cfg(not(target_arch = "wasm32"))]
1965+
{
1966+
self.imp.run_threaded()
1967+
}
19461968
}
19471969

1948-
#[cfg(target_arch = "wasm32")]
1949-
pub fn run_threaded(&self) {
1950-
self.imp.run_threaded()
1970+
/// Spawn a task which processes WebSocket messages as they are received.
1971+
///
1972+
/// # Panics
1973+
/// At runtime if called on any non-`wasm32` target.
1974+
pub fn run_background(&self) {
1975+
#[cfg(not(target_arch = "wasm32"))]
1976+
{
1977+
panic!("`DbConnection::run_background` is only supported on WebAssembly (wasm32); \
1978+
prefer using `DbConnection::run_threaded` instead");
1979+
}
1980+
#[cfg(target_arch = "wasm32")]
1981+
{
1982+
self.imp.run_background()
1983+
}
19511984
}
19521985

19531986
/// Run an `async` loop which processes WebSocket messages when polled.

sdks/rust/src/db_connection.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,6 @@ impl<M: SpacetimeModule> DbContextImpl<M> {
645645
/// Spawn a thread which does [`Self::advance_one_message_blocking`] in a loop.
646646
///
647647
/// Called by the autogenerated `DbConnection` method of the same name.
648-
#[cfg(not(feature = "web"))]
649648
pub fn run_threaded(&self) -> std::thread::JoinHandle<()> {
650649
let this = self.clone();
651650
std::thread::spawn(move || loop {
@@ -657,8 +656,11 @@ impl<M: SpacetimeModule> DbContextImpl<M> {
657656
})
658657
}
659658

659+
/// Spawn a background task which does [`Self::advance_one_message_async`] in a loop.
660+
///
661+
/// Called by the autogenerated `DbConnection` method of the same name.
660662
#[cfg(feature = "web")]
661-
pub fn run_threaded(&self) {
663+
pub fn run_background(&self) {
662664
let this = self.clone();
663665
wasm_bindgen_futures::spawn_local(async move {
664666
loop {
@@ -952,6 +954,7 @@ You must explicitly advance the connection by calling any one of:
952954
953955
- `DbConnection::frame_tick`.
954956
- `DbConnection::run_threaded`.
957+
- `DbConnection::run_background`.
955958
- `DbConnection::run_async`.
956959
- `DbConnection::advance_one_message`.
957960
- `DbConnection::advance_one_message_blocking`.

0 commit comments

Comments
 (0)