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>>,
)
| 1353 | /// A loop which reads raw WS messages from `recv`, parses them into domain types, |
| 1354 | /// and pushes the [`ParsedMessage`]s into `send`. |
| 1355 | async 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, |
no test coverage detected
searching dependent graphs…