diff options
author | sommerfeld <sommerfeld@sommerfeld.dev> | 2024-04-23 14:00:33 +0100 |
---|---|---|
committer | sommerfeld <sommerfeld@sommerfeld.dev> | 2024-04-23 16:08:55 +0100 |
commit | 849820e626b06f8e99f2e88b1391d101bca3640c (patch) | |
tree | 80bba105eb09e1e9e0ff648be6063419fedb3d2e /src | |
parent | a4326613610afbe73a9f1989e64024b43339c2fb (diff) | |
download | sentrum-849820e626b06f8e99f2e88b1391d101bca3640c.tar.gz sentrum-849820e626b06f8e99f2e88b1391d101bca3640c.tar.bz2 sentrum-849820e626b06f8e99f2e88b1391d101bca3640c.zip |
Refactor action dispatching to run_actions()
Diffstat (limited to 'src')
-rw-r--r-- | src/actions.rs | 12 | ||||
-rw-r--r-- | src/main.rs | 20 |
2 files changed, 15 insertions, 17 deletions
diff --git a/src/actions.rs b/src/actions.rs index bf78414..d006207 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -1,6 +1,7 @@ use std::fmt; use anyhow::Result; +use async_scoped::TokioScope; use async_trait::async_trait; use log::{debug, info, warn}; use serde::Deserialize; @@ -119,3 +120,14 @@ pub async fn get_actions<'a>( result } + +pub async fn run_actions( + actions: &[&(dyn Action<'_> + Sync)], + params: Option<MessageParams<'_, '_>>, +) { + TokioScope::scope_and_block(|s| { + for &action in actions { + s.spawn(action.run(params.as_ref())); + } + }); +} diff --git a/src/main.rs b/src/main.rs index 0173eb8..6006348 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ mod config; mod message; mod wallets; -use crate::actions::get_actions; +use crate::actions::{get_actions, run_actions}; use crate::message::MessageParams; use crate::{ blockchain::BlockchainState, @@ -63,14 +63,6 @@ fn set_signal_handlers() -> Result<()> { Ok(()) } -async fn run_test_actions(actions: &[&(dyn Action<'_> + Sync)]) { - TokioScope::scope_and_block(|s| { - for &action in actions { - s.spawn(action.run(None)); - } - }); -} - fn get_and_handle_new_txs( wallet_info: &SafeWalletInfo, actions: &[&(dyn Action<'_> + Sync)], @@ -80,13 +72,7 @@ fn get_and_handle_new_txs( TokioScope::scope_and_block(|s| { for tx in txs.iter() { let params = MessageParams::new(tx, &locked_wallet_info); - s.spawn(async move { - TokioScope::scope_and_block(|s| { - for &action in actions { - s.spawn(action.run(Some(¶ms))); - } - }); - }); + s.spawn(run_actions(actions, Some(params))); } }); Ok(()) @@ -148,7 +134,7 @@ async fn do_main() -> Result<()> { let actions_ref = actions.iter().map(Box::as_ref).collect::<Vec<_>>(); if args.test() { - run_test_actions(&actions_ref).await; + run_actions(&actions_ref, None).await; return Ok(()); } |