-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
233 additions
and
86 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,44 @@ | ||
use std::{ | ||
ops::{Deref, DerefMut}, | ||
path::Path, | ||
sync::Arc, | ||
}; | ||
use std::sync::Arc; | ||
|
||
use sage::Sage; | ||
use sage::{Result, Sage}; | ||
use sage_api::SyncEvent as ApiEvent; | ||
use sage_wallet::SyncEvent; | ||
use tauri::{AppHandle, Emitter}; | ||
use tokio::sync::Mutex; | ||
|
||
use crate::error::Result; | ||
|
||
pub type AppState = Arc<Mutex<AppStateInner>>; | ||
|
||
pub struct AppStateInner { | ||
pub app_handle: AppHandle, | ||
pub initialized: bool, | ||
pub sage: Sage, | ||
} | ||
|
||
impl Deref for AppStateInner { | ||
type Target = Sage; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.sage | ||
} | ||
} | ||
|
||
impl DerefMut for AppStateInner { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.sage | ||
} | ||
} | ||
|
||
impl AppStateInner { | ||
pub fn new(app_handle: AppHandle, path: &Path) -> Self { | ||
Self { | ||
app_handle, | ||
initialized: false, | ||
sage: Sage::new(path), | ||
} | ||
} | ||
|
||
pub async fn initialize(&mut self) -> Result<bool> { | ||
if self.initialized { | ||
return Ok(true); | ||
} | ||
|
||
self.initialized = true; | ||
|
||
let mut receiver = self.sage.initialize().await?; | ||
let app_handle = self.app_handle.clone(); | ||
|
||
tokio::spawn(async move { | ||
while let Some(event) = receiver.recv().await { | ||
let event = match event { | ||
SyncEvent::Start(ip) => ApiEvent::Start { ip: ip.to_string() }, | ||
SyncEvent::Stop => ApiEvent::Stop, | ||
SyncEvent::Subscribed => ApiEvent::Subscribed, | ||
SyncEvent::DerivationIndex { .. } => ApiEvent::Derivation, | ||
// TODO: New event? | ||
SyncEvent::CoinsUpdated { .. } | ||
| SyncEvent::TransactionUpdated { .. } | ||
| SyncEvent::TransactionEnded { .. } | ||
| SyncEvent::OfferUpdated { .. } => ApiEvent::CoinState, | ||
SyncEvent::PuzzleBatchSynced => ApiEvent::PuzzleBatchSynced, | ||
SyncEvent::CatInfo => ApiEvent::CatInfo, | ||
SyncEvent::DidInfo => ApiEvent::DidInfo, | ||
SyncEvent::NftData => ApiEvent::NftData, | ||
}; | ||
if app_handle.emit("sync-event", event).is_err() { | ||
break; | ||
} | ||
use tokio::{sync::Mutex, task::JoinHandle}; | ||
|
||
pub struct Initialized(pub Mutex<bool>); | ||
|
||
pub struct RpcTask(pub Mutex<Option<JoinHandle<anyhow::Result<()>>>>); | ||
|
||
pub type AppState = Arc<Mutex<Sage>>; | ||
|
||
pub async fn initialize(app_handle: AppHandle, sage: &mut Sage) -> Result<()> { | ||
let mut receiver = sage.initialize().await?; | ||
|
||
tokio::spawn(async move { | ||
while let Some(event) = receiver.recv().await { | ||
let event = match event { | ||
SyncEvent::Start(ip) => ApiEvent::Start { ip: ip.to_string() }, | ||
SyncEvent::Stop => ApiEvent::Stop, | ||
SyncEvent::Subscribed => ApiEvent::Subscribed, | ||
SyncEvent::DerivationIndex { .. } => ApiEvent::Derivation, | ||
// TODO: New event? | ||
SyncEvent::CoinsUpdated { .. } | ||
| SyncEvent::TransactionUpdated { .. } | ||
| SyncEvent::TransactionEnded { .. } | ||
| SyncEvent::OfferUpdated { .. } => ApiEvent::CoinState, | ||
SyncEvent::PuzzleBatchSynced => ApiEvent::PuzzleBatchSynced, | ||
SyncEvent::CatInfo => ApiEvent::CatInfo, | ||
SyncEvent::DidInfo => ApiEvent::DidInfo, | ||
SyncEvent::NftData => ApiEvent::NftData, | ||
}; | ||
if app_handle.emit("sync-event", event).is_err() { | ||
break; | ||
} | ||
} | ||
|
||
Result::Ok(()) | ||
}); | ||
Result::Ok(()) | ||
}); | ||
|
||
Ok(false) | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.