MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / parse_loop

Function parse_loop

sdks/rust/src/db_connection.rs:1355–1495  ·  view source on GitHub ↗

A loop which reads raw WS messages from `recv`, parses them into domain types, and pushes the [`ParsedMessage`]s into `send`.

(
    mut recv: mpsc::UnboundedReceiver<ws::v2::ServerMessage>,
    send: mpsc::UnboundedSender<ParsedMessage<M>>,
    extra_logging: Option<SharedCell<File>>,
)

Source from the content-addressed store, hash-verified

1353/// A loop which reads raw WS messages from `recv`, parses them into domain types,
1354/// and pushes the [`ParsedMessage`]s into `send`.
1355async fn parse_loop<M: SpacetimeModule>(
1356 mut recv: mpsc::UnboundedReceiver<ws::v2::ServerMessage>,
1357 send: mpsc::UnboundedSender<ParsedMessage<M>>,
1358 extra_logging: Option<SharedCell<File>>,
1359) {
1360 while let Some(msg) = recv.next().await {
1361 debug_log(&extra_logging, |file| {
1362 writeln!(file, "`parse_loop`: Got raw message: {msg:?}")
1363 });
1364 let parsed = match msg {
1365 ws::v2::ServerMessage::TransactionUpdate(transaction_update) => {
1366 match M::DbUpdate::parse_update(transaction_update) {
1367 Err(e) => ParsedMessage::Error(
1368 InternalError::failed_parse("TransactionUpdate", "TransactionUpdate")
1369 .with_cause(e)
1370 .into(),
1371 ),
1372 Ok(db_update) => ParsedMessage::TransactionUpdate(db_update),
1373 }
1374 }
1375 ws::v2::ServerMessage::ReducerResult(ws::v2::ReducerResult {
1376 request_id,
1377 result,
1378 timestamp,
1379 }) => {
1380 match result {
1381 ws::v2::ReducerOutcome::OkEmpty => ParsedMessage::ReducerResult {
1382 request_id,
1383 timestamp,
1384 result: Ok(Ok(M::DbUpdate::default())),
1385 },
1386 ws::v2::ReducerOutcome::Ok(ws::v2::ReducerOk {
1387 ret_value,
1388 transaction_update,
1389 }) => {
1390 assert!(
1391 ret_value.is_empty(),
1392 "Reducer return value should be unit, i.e. 0 bytes, but got {ret_value:?}"
1393 );
1394 match M::DbUpdate::parse_update(transaction_update) {
1395 Ok(db_update) => ParsedMessage::ReducerResult {
1396 request_id,
1397 timestamp,
1398 result: Ok(Ok(db_update)),
1399 },
1400 // Parse errors are not errors with the reducer call itself,
1401 // so they don't go to `ParsedMessage::ReducerResult`.
1402 // Instead, they go to `ParsedMessage::Error`, as they represent bugs in the SDK.
1403 Err(e) => ParsedMessage::Error(
1404 InternalError::failed_parse("TransactionUpdate", "ReducerResult")
1405 .with_cause(e)
1406 .into(),
1407 ),
1408 }
1409 }
1410 ws::v2::ReducerOutcome::Err(error_return) => match bsatn::from_slice::<String>(&error_return) {
1411 Ok(error_message) => ParsedMessage::ReducerResult {
1412 request_id,

Callers 1

spawn_parse_loopFunction · 0.85

Calls 10

debug_logFunction · 0.85
with_causeMethod · 0.80
ErrorEnum · 0.70
TransactionUpdateClass · 0.50
OkFunction · 0.50
ErrFunction · 0.50
newFunction · 0.50
IdentityTokenClass · 0.50
nextMethod · 0.45
to_stringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…