Skip to content

Commit d2011c8

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 ddb6e90 commit d2011c8

File tree

3 files changed

+83
-14
lines changed

3 files changed

+83
-14
lines changed

crates/codegen/src/rust.rs

+39-6
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

+39-6
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,7 @@ impl __sdk::InModule for RemoteTables {
17231723
///
17241724
/// - [`DbConnection::frame_tick`].
17251725
/// - [`DbConnection::run_threaded`].
1726+
/// - [`DbConnection::run_background`].
17261727
/// - [`DbConnection::run_async`].
17271728
/// - [`DbConnection::advance_one_message`].
17281729
/// - [`DbConnection::advance_one_message_blocking`].
@@ -1822,8 +1823,19 @@ impl DbConnection {
18221823
/// This is a low-level primitive exposed for power users who need significant control over scheduling.
18231824
/// Most applications should call [`Self::run_threaded`] to spawn a thread
18241825
/// which advances the connection automatically.
1826+
///
1827+
/// # Panics
1828+
/// At runtime if called on any `wasm32` target.
18251829
pub fn advance_one_message_blocking(&self) -> __sdk::Result<()> {
1826-
self.imp.advance_one_message_blocking()
1830+
#[cfg(target_arch = "wasm32")]
1831+
{
1832+
panic!("`DbConnection::advance_one_message_blocking` is not supported on WebAssembly (wasm32); \
1833+
prefer using `advance_one_message` or `advance_one_message_async` instead");
1834+
}
1835+
#[cfg(not(target_arch = "wasm32"))]
1836+
{
1837+
self.imp.advance_one_message_blocking()
1838+
}
18271839
}
18281840

18291841
/// Process one WebSocket message, `await`ing until one is received.
@@ -1847,14 +1859,35 @@ impl DbConnection {
18471859
}
18481860

18491861
/// Spawn a thread which processes WebSocket messages as they are received.
1850-
#[cfg(not(target_arch = "wasm32"))]
1862+
///
1863+
/// # Panics
1864+
/// At runtime if called on any `wasm32` target.
18511865
pub fn run_threaded(&self) -> std::thread::JoinHandle<()> {
1852-
self.imp.run_threaded()
1866+
#[cfg(target_arch = "wasm32")]
1867+
{
1868+
panic!("`DbConnection::run_threaded` is not supported on WebAssembly (wasm32); \
1869+
prefer using `DbConnection::run_background` instead");
1870+
}
1871+
#[cfg(not(target_arch = "wasm32"))]
1872+
{
1873+
self.imp.run_threaded()
1874+
}
18531875
}
18541876

1855-
#[cfg(target_arch = "wasm32")]
1856-
pub fn run_threaded(&self) {
1857-
self.imp.run_threaded()
1877+
/// Spawn a task which processes WebSocket messages as they are received.
1878+
///
1879+
/// # Panics
1880+
/// At runtime if called on any non-`wasm32` target.
1881+
pub fn run_background(&self) {
1882+
#[cfg(not(target_arch = "wasm32"))]
1883+
{
1884+
panic!("`DbConnection::run_background` is only supported on WebAssembly (wasm32); \
1885+
prefer using `DbConnection::run_threaded` instead");
1886+
}
1887+
#[cfg(target_arch = "wasm32")]
1888+
{
1889+
self.imp.run_background()
1890+
}
18581891
}
18591892

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

crates/sdk/src/db_connection.rs

+5-2
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)