From 569fc7cffa2a879760f572fd1aff5143d6189021 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Fri, 3 Jan 2025 16:34:37 -0600 Subject: [PATCH] centralize core.msg "expect" --- harbor-client/src/lib.rs | 33 ++++++++------- harbor-ui/src/bridge.rs | 88 +++++++++------------------------------- 2 files changed, 37 insertions(+), 84 deletions(-) diff --git a/harbor-client/src/lib.rs b/harbor-client/src/lib.rs index 2400e67..fff0317 100644 --- a/harbor-client/src/lib.rs +++ b/harbor-client/src/lib.rs @@ -126,33 +126,36 @@ pub struct HarborCore { } impl HarborCore { - pub async fn msg(&self, id: Option, msg: CoreUIMsg) -> anyhow::Result<()> { - self.tx.clone().send(CoreUIMsgPacket { id, msg }).await?; + // Initial setup messages that don't have an id + // Panics if fails to send + async fn send_system_msg(&self, msg: CoreUIMsg) { + self.tx.clone().send(CoreUIMsgPacket { id: None, msg }).await.expect("Could not communicate with the UI"); + } - Ok(()) + // Standard core->ui communication with an id + // Panics if fails to send + pub async fn msg(&self, id: Uuid, msg: CoreUIMsg) { + self.tx.clone().send(CoreUIMsgPacket { id: Some(id), msg }).await.expect("Could not communicate with the UI"); } // Sends updates to the UI to refelect the initial state pub async fn init_ui_state(&self) -> anyhow::Result<()> { for client in self.clients.read().await.values() { let fed_balance = client.fedimint_client.get_balance().await; - self.msg( - None, - CoreUIMsg::FederationBalanceUpdated { - id: client.fedimint_client.federation_id(), - balance: fed_balance, - }, - ) - .await?; + self.send_system_msg(CoreUIMsg::FederationBalanceUpdated { + id: client.fedimint_client.federation_id(), + balance: fed_balance, + }) + .await; } let history = self.storage.get_transaction_history()?; - self.msg(None, CoreUIMsg::TransactionHistoryUpdated(history)) - .await?; + self.send_system_msg(CoreUIMsg::TransactionHistoryUpdated(history)) + .await; let federation_items = self.get_federation_items().await; - self.msg(None, CoreUIMsg::FederationListUpdated(federation_items)) - .await?; + self.send_system_msg(CoreUIMsg::FederationListUpdated(federation_items)) + .await; Ok(()) } diff --git a/harbor-ui/src/bridge.rs b/harbor-ui/src/bridge.rs index 5ced699..1a7d9cd 100644 --- a/harbor-ui/src/bridge.rs +++ b/harbor-ui/src/bridge.rs @@ -395,33 +395,23 @@ async fn process_core(core_handle: &mut CoreHandle, core: &HarborCore) { invoice, } => { log::info!("Got UICoreMsg::Send"); - core.msg(Some(msg.id), CoreUIMsg::Sending) - .await - .expect("Could not send"); + core.msg(msg.id, CoreUIMsg::Sending).await; if let Err(e) = core.send_lightning(msg.id, federation_id, invoice).await { error!("Error sending: {e}"); - core.msg(Some(msg.id), CoreUIMsg::SendFailure(e.to_string())) - .await - .expect("Could not send"); + core.msg(msg.id, CoreUIMsg::SendFailure(e.to_string())).await; } } UICoreMsg::ReceiveLightning { federation_id, amount, } => { - core.msg(Some(msg.id), CoreUIMsg::ReceiveGenerating) - .await - .expect("Could not send"); + core.msg(msg.id, CoreUIMsg::ReceiveGenerating).await; match core.receive_lightning(msg.id, federation_id, amount).await { Err(e) => { - core.msg(Some(msg.id), CoreUIMsg::ReceiveFailed(e.to_string())) - .await - .expect("Could not send"); + core.msg(msg.id, CoreUIMsg::ReceiveFailed(e.to_string())).await; } Ok(invoice) => { - core.msg(Some(msg.id), CoreUIMsg::ReceiveInvoiceGenerated(invoice)) - .await - .expect("Could not send"); + core.msg(msg.id, CoreUIMsg::ReceiveInvoiceGenerated(invoice)).await; } } } @@ -431,33 +421,23 @@ async fn process_core(core_handle: &mut CoreHandle, core: &HarborCore) { amount_sats, } => { log::info!("Got UICoreMsg::SendOnChain"); - core.msg(Some(msg.id), CoreUIMsg::Sending) - .await - .expect("Could not send"); + core.msg(msg.id, CoreUIMsg::Sending).await; if let Err(e) = core .send_onchain(msg.id, federation_id, address, amount_sats) .await { error!("Error sending: {e}"); - core.msg(Some(msg.id), CoreUIMsg::SendFailure(e.to_string())) - .await - .expect("Could not send"); + core.msg(msg.id, CoreUIMsg::SendFailure(e.to_string())).await; } } UICoreMsg::ReceiveOnChain { federation_id } => { - core.msg(Some(msg.id), CoreUIMsg::ReceiveGenerating) - .await - .expect("Could not send"); + core.msg(msg.id, CoreUIMsg::ReceiveGenerating).await; match core.receive_onchain(msg.id, federation_id).await { Err(e) => { - core.msg(Some(msg.id), CoreUIMsg::ReceiveFailed(e.to_string())) - .await - .expect("Could not send"); + core.msg(msg.id, CoreUIMsg::ReceiveFailed(e.to_string())).await; } Ok(address) => { - core.msg(Some(msg.id), CoreUIMsg::ReceiveAddressGenerated(address)) - .await - .expect("Could not send"); + core.msg(msg.id, CoreUIMsg::ReceiveAddressGenerated(address)).await; } } } @@ -465,59 +445,31 @@ async fn process_core(core_handle: &mut CoreHandle, core: &HarborCore) { match core.get_federation_info(invite_code).await { Err(e) => { error!("Error getting federation info: {e}"); - core.msg( - Some(msg.id), - CoreUIMsg::AddFederationFailed(e.to_string()), - ) - .await - .expect("Could not send"); + core.msg(msg.id, CoreUIMsg::AddFederationFailed(e.to_string())).await; } Ok(config) => { - core.msg(Some(msg.id), CoreUIMsg::FederationInfo(config)) - .await - .expect("Could not send"); + core.msg(msg.id, CoreUIMsg::FederationInfo(config)).await; } } } UICoreMsg::AddFederation(invite_code) => { if let Err(e) = core.add_federation(invite_code).await { error!("Error adding federation: {e}"); - core.msg(Some(msg.id), CoreUIMsg::AddFederationFailed(e.to_string())) - .await - .expect("Could not send"); + core.msg(msg.id, CoreUIMsg::AddFederationFailed(e.to_string())).await; } else { - core.msg(Some(msg.id), CoreUIMsg::AddFederationSuccess) - .await - .expect("Could not send"); + core.msg(msg.id, CoreUIMsg::AddFederationSuccess).await; let new_federation_list = core.get_federation_items().await; - core.msg( - Some(msg.id), - CoreUIMsg::FederationListUpdated(new_federation_list), - ) - .await - .expect("Could not send"); + core.msg(msg.id, CoreUIMsg::FederationListUpdated(new_federation_list)).await; } } UICoreMsg::RemoveFederation(id) => { if let Err(e) = core.remove_federation(id).await { error!("Error removing federation: {e}"); - core.msg( - Some(msg.id), - CoreUIMsg::RemoveFederationFailed(e.to_string()), - ) - .await - .expect("Could not send"); + core.msg(msg.id, CoreUIMsg::RemoveFederationFailed(e.to_string())).await; } else { - core.msg(Some(msg.id), CoreUIMsg::RemoveFederationSuccess) - .await - .expect("Could not send"); + core.msg(msg.id, CoreUIMsg::RemoveFederationSuccess).await; let new_federation_list = core.get_federation_items().await; - core.msg( - Some(msg.id), - CoreUIMsg::FederationListUpdated(new_federation_list), - ) - .await - .expect("Could not send"); + core.msg(msg.id, CoreUIMsg::FederationListUpdated(new_federation_list)).await; } } UICoreMsg::Unlock(_password) => { @@ -528,9 +480,7 @@ async fn process_core(core_handle: &mut CoreHandle, core: &HarborCore) { } UICoreMsg::GetSeedWords => { let seed_words = core.get_seed_words().await; - core.msg(Some(msg.id), CoreUIMsg::SeedWords(seed_words)) - .await - .expect("Could not send"); + core.msg(msg.id, CoreUIMsg::SeedWords(seed_words)).await; } } }