Skip to content

Commit 4d35a85

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 1dbeba3 commit 4d35a85

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
@@ -1210,6 +1210,7 @@ impl __sdk::InModule for RemoteTables {{
12101210
///
12111211
/// - [`DbConnection::frame_tick`].
12121212
/// - [`DbConnection::run_threaded`].
1213+
/// - [`DbConnection::run_background`].
12131214
/// - [`DbConnection::run_async`].
12141215
/// - [`DbConnection::advance_one_message`].
12151216
/// - [`DbConnection::advance_one_message_blocking`].
@@ -1309,8 +1310,19 @@ impl DbConnection {{
13091310
/// This is a low-level primitive exposed for power users who need significant control over scheduling.
13101311
/// Most applications should call [`Self::run_threaded`] to spawn a thread
13111312
/// which advances the connection automatically.
1313+
///
1314+
/// # Panics
1315+
/// At runtime if called on any `wasm32` target.
13121316
pub fn advance_one_message_blocking(&self) -> __sdk::Result<()> {{
1313-
self.imp.advance_one_message_blocking()
1317+
#[cfg(target_arch = \"wasm32\")]
1318+
{{
1319+
panic!(\"`DbConnection::advance_one_message_blocking` is not supported on WebAssembly (wasm32); \\
1320+
prefer using `advance_one_message` or `advance_one_message_async` instead\");
1321+
}}
1322+
#[cfg(not(target_arch = \"wasm32\"))]
1323+
{{
1324+
self.imp.advance_one_message_blocking()
1325+
}}
13141326
}}
13151327
13161328
/// Process one WebSocket message, `await`ing until one is received.
@@ -1334,14 +1346,35 @@ impl DbConnection {{
13341346
}}
13351347
13361348
/// Spawn a thread which processes WebSocket messages as they are received.
1337-
#[cfg(not(target_arch = \"wasm32\"))]
1349+
///
1350+
/// # Panics
1351+
/// At runtime if called on any `wasm32` target.
13381352
pub fn run_threaded(&self) -> std::thread::JoinHandle<()> {{
1339-
self.imp.run_threaded()
1353+
#[cfg(target_arch = \"wasm32\")]
1354+
{{
1355+
panic!(\"`DbConnection::run_threaded` is not supported on WebAssembly (wasm32); \\
1356+
prefer using `DbConnection::run_background` instead\");
1357+
}}
1358+
#[cfg(not(target_arch = \"wasm32\"))]
1359+
{{
1360+
self.imp.run_threaded()
1361+
}}
13401362
}}
13411363
1342-
#[cfg(target_arch = \"wasm32\")]
1343-
pub fn run_threaded(&self) {{
1344-
self.imp.run_threaded()
1364+
/// Spawn a task which processes WebSocket messages as they are received.
1365+
///
1366+
/// # Panics
1367+
/// At runtime if called on any non-`wasm32` target.
1368+
pub fn run_background(&self) {{
1369+
#[cfg(not(target_arch = \"wasm32\"))]
1370+
{{
1371+
panic!(\"`DbConnection::run_background` is only supported on WebAssembly (wasm32); \\
1372+
prefer using `DbConnection::run_threaded` instead\");
1373+
}}
1374+
#[cfg(target_arch = \"wasm32\")]
1375+
{{
1376+
self.imp.run_background()
1377+
}}
13451378
}}
13461379
13471380
/// 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
@@ -1753,6 +1753,7 @@ impl __sdk::InModule for RemoteTables {
17531753
///
17541754
/// - [`DbConnection::frame_tick`].
17551755
/// - [`DbConnection::run_threaded`].
1756+
/// - [`DbConnection::run_background`].
17561757
/// - [`DbConnection::run_async`].
17571758
/// - [`DbConnection::advance_one_message`].
17581759
/// - [`DbConnection::advance_one_message_blocking`].
@@ -1852,8 +1853,19 @@ impl DbConnection {
18521853
/// This is a low-level primitive exposed for power users who need significant control over scheduling.
18531854
/// Most applications should call [`Self::run_threaded`] to spawn a thread
18541855
/// which advances the connection automatically.
1856+
///
1857+
/// # Panics
1858+
/// At runtime if called on any `wasm32` target.
18551859
pub fn advance_one_message_blocking(&self) -> __sdk::Result<()> {
1856-
self.imp.advance_one_message_blocking()
1860+
#[cfg(target_arch = "wasm32")]
1861+
{
1862+
panic!("`DbConnection::advance_one_message_blocking` is not supported on WebAssembly (wasm32); \
1863+
prefer using `advance_one_message` or `advance_one_message_async` instead");
1864+
}
1865+
#[cfg(not(target_arch = "wasm32"))]
1866+
{
1867+
self.imp.advance_one_message_blocking()
1868+
}
18571869
}
18581870

18591871
/// Process one WebSocket message, `await`ing until one is received.
@@ -1877,14 +1889,35 @@ impl DbConnection {
18771889
}
18781890

18791891
/// Spawn a thread which processes WebSocket messages as they are received.
1880-
#[cfg(not(target_arch = "wasm32"))]
1892+
///
1893+
/// # Panics
1894+
/// At runtime if called on any `wasm32` target.
18811895
pub fn run_threaded(&self) -> std::thread::JoinHandle<()> {
1882-
self.imp.run_threaded()
1896+
#[cfg(target_arch = "wasm32")]
1897+
{
1898+
panic!("`DbConnection::run_threaded` is not supported on WebAssembly (wasm32); \
1899+
prefer using `DbConnection::run_background` instead");
1900+
}
1901+
#[cfg(not(target_arch = "wasm32"))]
1902+
{
1903+
self.imp.run_threaded()
1904+
}
18831905
}
18841906

1885-
#[cfg(target_arch = "wasm32")]
1886-
pub fn run_threaded(&self) {
1887-
self.imp.run_threaded()
1907+
/// Spawn a task which processes WebSocket messages as they are received.
1908+
///
1909+
/// # Panics
1910+
/// At runtime if called on any non-`wasm32` target.
1911+
pub fn run_background(&self) {
1912+
#[cfg(not(target_arch = "wasm32"))]
1913+
{
1914+
panic!("`DbConnection::run_background` is only supported on WebAssembly (wasm32); \
1915+
prefer using `DbConnection::run_threaded` instead");
1916+
}
1917+
#[cfg(target_arch = "wasm32")]
1918+
{
1919+
self.imp.run_background()
1920+
}
18881921
}
18891922

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

crates/sdk/src/db_connection.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,6 @@ 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(feature = "web"))]
589588
pub fn run_threaded(&self) -> std::thread::JoinHandle<()> {
590589
let this = self.clone();
591590
std::thread::spawn(move || loop {
@@ -597,8 +596,11 @@ impl<M: SpacetimeModule> DbContextImpl<M> {
597596
})
598597
}
599598

599+
/// Spawn a background task which does [`Self::advance_one_message_async`] in a loop.
600+
///
601+
/// Called by the autogenerated `DbConnection` method of the same name.
600602
#[cfg(feature = "web")]
601-
pub fn run_threaded(&self) {
603+
pub fn run_background(&self) {
602604
let this = self.clone();
603605
wasm_bindgen_futures::spawn_local(async move {
604606
loop {
@@ -857,6 +859,7 @@ You must explicitly advance the connection by calling any one of:
857859
858860
- `DbConnection::frame_tick`.
859861
- `DbConnection::run_threaded`.
862+
- `DbConnection::run_background`.
860863
- `DbConnection::run_async`.
861864
- `DbConnection::advance_one_message`.
862865
- `DbConnection::advance_one_message_blocking`.

0 commit comments

Comments
 (0)