Make the blockchain interface async again on wasm32-unknown-unknown

The procedural macro `#[maybe_async]` makes a method or every method of a trait
"async" whenever the target_arch is `wasm32`, and leaves them untouched on
every other platform.

The macro `maybe_await!($e:expr)` can be used to call `maybe_async` methods on
multi-platform code: it expands to `$e` on non-wasm32 platforms and to
`$e.await` on wasm32.

The macro `await_or_block!($e:expr)` can be used to contain async code as much
as possible: it expands to `tokio::runtime::Runtime::new().unwrap().block_on($e)`
on non-wasm32 platforms, and to `$e.await` on wasm32.
This commit is contained in:
Alekos Filini
2020-07-20 15:51:57 +02:00
parent 4a51d50e1f
commit 4fcf7ac89e
10 changed files with 254 additions and 85 deletions

View File

@@ -276,6 +276,7 @@ pub fn add_global_flags<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
.subcommand(SubCommand::with_name("repl").about("Opens an interactive shell"))
}
#[maybe_async]
pub fn handle_matches<C, D>(
wallet: &Wallet<C, D>,
matches: ArgMatches<'_>,
@@ -287,7 +288,7 @@ where
if let Some(_sub_matches) = matches.subcommand_matches("get_new_address") {
Ok(Some(format!("{}", wallet.get_new_address()?)))
} else if let Some(_sub_matches) = matches.subcommand_matches("sync") {
wallet.sync(None, None)?;
maybe_await!(wallet.sync(None, None))?;
Ok(None)
} else if let Some(_sub_matches) = matches.subcommand_matches("list_unspent") {
let mut res = String::new();
@@ -382,7 +383,7 @@ where
panic!("Missing `psbt` and `tx` option");
};
let txid = wallet.broadcast(tx)?;
let txid = maybe_await!(wallet.broadcast(tx))?;
Ok(Some(format!("TXID: {}", txid)))
} else if let Some(sub_matches) = matches.subcommand_matches("extract_psbt") {