Skip to content

Commit

Permalink
centralize core.msg "expect"
Browse files Browse the repository at this point in the history
  • Loading branch information
futurepaul committed Jan 3, 2025
1 parent 7dd2a1a commit 569fc7c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 84 deletions.
33 changes: 18 additions & 15 deletions harbor-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,33 +126,36 @@ pub struct HarborCore {
}

impl HarborCore {
pub async fn msg(&self, id: Option<Uuid>, 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(())
}
Expand Down
88 changes: 19 additions & 69 deletions harbor-ui/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand All @@ -431,93 +421,55 @@ 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;
}
}
}
UICoreMsg::GetFederationInfo(invite_code) => {
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) => {
Expand All @@ -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;
}
}
}
Expand Down

0 comments on commit 569fc7c

Please sign in to comment.