Răsfoiți Sursa

Disable events on wasm since it relies on tokio tasks

Cesar Rodas 1 săptămână în urmă
părinte
comite
71b1c87b8c

+ 4 - 0
crates/cdk-common/src/error.rs

@@ -100,6 +100,10 @@ pub enum Error {
     #[error("Internal receive error: {0}")]
     RecvError(String),
 
+    /// Not supported
+    #[error("Functionality not supported")]
+    NotSupported,
+
     // Mint Errors
     /// Minting is disabled
     #[error("Minting is disabled")]

+ 2 - 0
crates/cdk/src/wallet/builder.rs

@@ -10,6 +10,7 @@ use cdk_common::AuthToken;
 #[cfg(feature = "auth")]
 use tokio::sync::RwLock;
 
+#[cfg(not(target_arch = "wasm32"))]
 use super::events::EventStore;
 use crate::cdk_database::WalletDatabase;
 use crate::error::Error;
@@ -157,6 +158,7 @@ impl WalletBuilder {
             xpriv,
             client: client.clone(),
             subscription: SubscriptionManager::new(client),
+            #[cfg(not(target_arch = "wasm32"))]
             event_manager: Arc::new(EventStore::default().into()),
         })
     }

+ 25 - 5
crates/cdk/src/wallet/events.rs

@@ -1,4 +1,5 @@
 use std::collections::HashMap;
+#[cfg(not(target_arch = "wasm32"))]
 use std::sync::Arc;
 
 use cdk_common::mint_url::MintUrl;
@@ -7,8 +8,12 @@ use cdk_common::pub_sub::{OnNewSubscription, SubId};
 use cdk_common::{Amount, CurrencyUnit, State};
 use tokio::sync::RwLock;
 
-use super::{ProofsMethods, Wallet};
-use crate::pub_sub::{self, ActiveSubscription};
+#[cfg(not(target_arch = "wasm32"))]
+use super::ProofsMethods;
+use super::Wallet;
+#[cfg(not(target_arch = "wasm32"))]
+use crate::pub_sub;
+use crate::pub_sub::ActiveSubscription;
 
 /// The internal event is `()` because the events() will accept no filter, all events all sent to
 /// all subscriber with no option to filter.
@@ -70,6 +75,7 @@ impl OnNewSubscription for EventStore {
     }
 }
 
+#[cfg(not(target_arch = "wasm32"))]
 /// The event manager is an alias manager
 pub type EventManager = pub_sub::Manager<Event, EventStore>;
 
@@ -95,6 +101,7 @@ impl Wallet {
     /// Internal function to trigger an event. This function is private and must be called from
     /// within itself.
     #[inline(always)]
+    #[cfg(not(target_arch = "wasm32"))]
     async fn trigger_events(event_manager: Arc<EventManager>, events: Vec<Event>) {
         let events = if let Some(event_store) = event_manager.on_new_subscription() {
             let mut last_events = event_store.last_events.write().await;
@@ -122,6 +129,11 @@ impl Wallet {
     }
 
     /// Notify all balances, because it is likely it has changed
+    #[cfg(target_arch = "wasm32")]
+    pub(crate) fn notify_update_balance(&self) {}
+
+    /// Notify all balances, because it is likely it has changed
+    #[cfg(not(target_arch = "wasm32"))]
     pub(crate) fn notify_update_balance(&self) {
         let db = self.localstore.clone();
         let event_manager = self.event_manager.clone();
@@ -192,9 +204,17 @@ impl Wallet {
     }
 
     /// Subscribe to wallet events
-    pub async fn events(&self) -> ActiveSubscription<Event, EventFilter> {
-        self.event_manager
+    #[cfg(target_arch = "wasm32")]
+    pub async fn events(&self) -> Result<ActiveSubscription<Event, EventFilter>, super::Error> {
+        Err(super::Error::NotSupported)
+    }
+
+    /// Subscribe to wallet events
+    #[cfg(not(target_arch = "wasm32"))]
+    pub async fn events(&self) -> Result<ActiveSubscription<Event, EventFilter>, super::Error> {
+        Ok(self
+            .event_manager
             .subscribe(SubscribeToAllEvents::default())
-            .await
+            .await)
     }
 }

+ 2 - 0
crates/cdk/src/wallet/mod.rs

@@ -7,6 +7,7 @@ use std::sync::Arc;
 use bitcoin::bip32::Xpriv;
 use cdk_common::database::{self, WalletDatabase};
 use cdk_common::subscription::Params;
+#[cfg(not(target_arch = "wasm32"))]
 use events::EventManager;
 use getrandom::getrandom;
 use subscription::{ActiveSubscription, SubscriptionManager};
@@ -83,6 +84,7 @@ pub struct Wallet {
     xpriv: Xpriv,
     client: Arc<dyn MintConnector + Send + Sync>,
     subscription: SubscriptionManager,
+    #[cfg(not(target_arch = "wasm32"))]
     event_manager: Arc<EventManager>,
 }