Skip to content

Commit

Permalink
Add WebLN::enable
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Jan 19, 2024
1 parent dfa63d1 commit ea7c8fb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
34 changes: 34 additions & 0 deletions webln-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,44 @@
#![allow(clippy::new_without_default)]

use wasm_bindgen::prelude::*;
use webln::WebLN;

pub mod error;

use self::error::{into_err, Result};

#[wasm_bindgen(start)]
pub fn start() {
console_error_panic_hook::set_once();
}

/// WebLN instance
#[wasm_bindgen(js_name = WebLN)]
pub struct JsWebLN {
inner: WebLN,
}

#[wasm_bindgen(js_class = WebLN)]
impl JsWebLN {
/// Compose new WebLN instance
#[wasm_bindgen(constructor)]
pub fn new() -> Result<JsWebLN> {
Ok(Self {
inner: WebLN::new().map_err(into_err)?,
})
}

/// Check if `webln` is enabled without explicitly enabling it through `webln.enable()`
/// (which may cause a confirmation popup in some providers)
#[wasm_bindgen(js_name = isEnabled)]
pub async fn is_enabled(&self) -> Result<bool> {
self.inner.is_enabled().await.map_err(into_err)
}

/// To begin interacting with WebLN APIs you'll first need to enable the provider.
/// Calling `webln.enable()` will prompt the user for permission to use the WebLN capabilities of the browser.
/// After that you are free to call any of the other API methods.
pub async fn enable(&self) -> Result<()> {
self.inner.enable().await.map_err(into_err)
}
}
10 changes: 10 additions & 0 deletions webln/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,14 @@ impl WebLN {
.as_bool()
.ok_or_else(|| Error::TypeMismatch(String::from("expected a bool")))
}

/// To begin interacting with WebLN APIs you'll first need to enable the provider.
/// Calling `webln.enable()` will prompt the user for permission to use the WebLN capabilities of the browser.
/// After that you are free to call any of the other API methods.
pub async fn enable(&self) -> Result<(), Error> {
let func: Function = self.get_func(&self.webln_obj, "enable")?;
let promise: Promise = Promise::resolve(&func.call0(&self.webln_obj)?);
JsFuture::from(promise).await?;
Ok(())
}
}

0 comments on commit ea7c8fb

Please sign in to comment.