Skip to content

Commit

Permalink
fix: update emitter func (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
wiseaidev authored Jun 18, 2024
1 parent d8c310c commit 3f908ca
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 18 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,54 @@ fn main() {
yew::Renderer::<App>::new().render();
}
```
## 🎧 Event Listener

This crate implements a handy event listener pattern with a built-in `emitter` object that you can use to subscribe to particular events. This functionality allows you to set state in the UI, perform actions on wallet connect, and more.


```rust , ignore
// ...snip...

#[function_component]
pub fn LoginPage() -> Html {
let wallet_context = use_wallet();
let connected = use_state(|| false);
let wallet_adapter = use_state(|| wallet_context);

let connect_wallet = {
// ...snip...

Callback::from(move |_| {
// ...snip...

spawn_local(async move {
let mut wallet_info = (*wallet_adapter).clone();

wallet_info.emitter.on("connect", move |public_key: Pubkey| {
log::info!("Event Listener: Got pubkey {}", public_key);
wallet_adapter.set(wallet_info);
connected.set(true);
});

match wallet_info.connect().await {
Ok(_) => {
}
Err(err) => {
log::error!("Failed to connect wallet: {}", err);
}
}
});
})
};

// ...snip...

html! {
<>
</>
}
}
```

## 👥 Contributing

Expand Down
7 changes: 6 additions & 1 deletion examples/basic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use wasi_sol::{
connection::{use_connection, ConnectionProvider},
wallet::{use_wallet, WalletProvider},
},
spawn_local
spawn_local,
pubkey::Pubkey
};

#[function_component]
Expand Down Expand Up @@ -51,6 +52,10 @@ pub fn LoginPage() -> Html {
spawn_local(async move {
let mut wallet_info = (*wallet_adapter).clone();

wallet_info.emitter.on("connect", move |public_key: Pubkey| {
log::info!("Event Listener: Got pubkey {}", public_key);
});

match wallet_info.connect().await {
Ok(_) => {
wallet_adapter.set(wallet_info);
Expand Down
6 changes: 2 additions & 4 deletions src/core/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

use anyhow::Result;

use emitter_rs::EventEmitter;

use solana_client_wasm::WasmClient as RpcClient;
use solana_sdk::{pubkey::Pubkey, signature::Signature, transaction::Transaction};

use crate::core::{
Expand All @@ -28,15 +27,14 @@ pub trait WalletAdapter: WalletAdapterEvents + Send + Sync {
fn connected(&self) -> bool {
self.public_key().is_some()
}
fn emitter(&self) -> EventEmitter;

async fn auto_connect(&mut self) -> Result<(), WalletError>;
async fn connect(&mut self) -> Result<(), WalletError>;
async fn disconnect(&mut self) -> Result<(), WalletError>;
async fn send_transaction(
&mut self,
client: RpcClient,
transaction: TransactionOrVersionedTransaction,
connection: &str,
) -> Result<Signature, WalletError>;
}

Expand Down
11 changes: 2 additions & 9 deletions src/core/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct BaseWalletAdapter {
ready_state: WalletReadyState,
public_key: Option<Pubkey>,
connecting: bool,
emitter: EventEmitter,
pub emitter: EventEmitter,
}

impl BaseWalletAdapter {
Expand Down Expand Up @@ -84,10 +84,6 @@ impl WalletAdapter for BaseWalletAdapter {
self.connecting
}

fn emitter(&self) -> EventEmitter {
self.emitter.clone()
}

async fn auto_connect(&mut self) -> Result<(), WalletError> {
self.connect().await
}
Expand Down Expand Up @@ -174,17 +170,14 @@ impl WalletAdapter for BaseWalletAdapter {

async fn send_transaction(
&mut self,
client: RpcClient,
transaction: TransactionOrVersionedTransaction,
connection: &str,
) -> Result<Signature, WalletError> {
info!("Sending transaction...");

if self.public_key.is_none() {
return Err(WalletError::WalletNotConnectedError);
}

let client = RpcClient::new(connection);

let signature = match transaction {
TransactionOrVersionedTransaction::Transaction(tx) => client
.send_and_confirm_transaction(&tx)
Expand Down
5 changes: 1 addition & 4 deletions src/provider/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ pub fn WalletProvider(props: &WalletProviderProps) -> Html {
.cloned()
});

let wallet = (*wallet_context).clone().unwrap();

let context =
use_state(|| BaseWalletAdapter::new(wallet.name(), props.endpoint, wallet.icon()));
let context = use_state(|| (*wallet_context).clone().unwrap());

html! {
<ContextProvider<BaseWalletAdapter> context={(*context).clone()}>
Expand Down

0 comments on commit 3f908ca

Please sign in to comment.