Merge branch 'JonasKruckenberg:main' into dialog

This commit is contained in:
bicarlsen 2022-11-13 22:25:55 +01:00 committed by GitHub
commit 339852b948
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 20 deletions

View file

@ -1,3 +1,7 @@
use wasm_bindgen_futures::JsFuture;
use crate::Error;
/// Gets the clipboard content as plain text. /// Gets the clipboard content as plain text.
/// ///
/// # Example /// # Example
@ -8,8 +12,11 @@
/// let clipboard_text = read_text().await; /// let clipboard_text = read_text().await;
/// ``` /// ```
#[inline(always)] #[inline(always)]
pub async fn read_text() -> Option<String> { pub async fn read_text() -> crate::Result<Option<String>> {
inner::readText().await.as_string() JsFuture::from(inner::readText())
.await
.map(|v| v.as_string())
.map_err(Error::Other)
} }
/// Writes plain text to the clipboard. /// Writes plain text to the clipboard.
@ -25,16 +32,20 @@ pub async fn read_text() -> Option<String> {
/// ///
/// @returns A promise indicating the success or failure of the operation. /// @returns A promise indicating the success or failure of the operation.
#[inline(always)] #[inline(always)]
pub async fn write_text(text: &str) { pub async fn write_text(text: &str) -> crate::Result<()> {
inner::writeText(text).await JsFuture::from(inner::writeText(text))
.await
.map_err(Error::Other)?;
Ok(())
} }
mod inner { mod inner {
use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; use wasm_bindgen::{prelude::wasm_bindgen};
#[wasm_bindgen(module = "/dist/clipboard.js")] #[wasm_bindgen(module = "/dist/clipboard.js")]
extern "C" { extern "C" {
pub async fn readText() -> JsValue; pub fn readText() -> js_sys::Promise;
pub async fn writeText(text: &str); pub fn writeText(text: &str) -> js_sys::Promise;
} }
} }

View file

@ -2,6 +2,9 @@ use std::fmt::Debug;
use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serde::{de::DeserializeOwned, Deserialize, Serialize};
use wasm_bindgen::{prelude::Closure, JsValue}; use wasm_bindgen::{prelude::Closure, JsValue};
use wasm_bindgen_futures::JsFuture;
use crate::Error;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Event<T> { pub struct Event<T> {
@ -45,8 +48,12 @@ impl<T: Debug> Debug for Event<T> {
/// ///
/// @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`. /// @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
#[inline(always)] #[inline(always)]
pub async fn emit<T: Serialize>(event: &str, payload: &T) { pub async fn emit<T: Serialize>(event: &str, payload: &T) -> crate::Result<()> {
inner::emit(event, serde_wasm_bindgen::to_value(payload).unwrap()).await JsFuture::from(inner::emit(event, serde_wasm_bindgen::to_value(payload)?))
.await
.map_err(Error::Other)?;
Ok(())
} }
/// Listen to an event from the backend. /// Listen to an event from the backend.
@ -70,7 +77,7 @@ pub async fn emit<T: Serialize>(event: &str, payload: &T) {
/// ///
/// Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted. /// Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
#[inline(always)] #[inline(always)]
pub async fn listen<T, H>(event: &str, mut handler: H) -> impl FnOnce() pub async fn listen<T, H>(event: &str, mut handler: H) -> crate::Result<impl FnOnce()>
where where
T: DeserializeOwned, T: DeserializeOwned,
H: FnMut(Event<T>) + 'static, H: FnMut(Event<T>) + 'static,
@ -79,14 +86,16 @@ where
(handler)(serde_wasm_bindgen::from_value(raw).unwrap()) (handler)(serde_wasm_bindgen::from_value(raw).unwrap())
}); });
let unlisten = inner::listen(event, &closure).await; let unlisten = JsFuture::from(inner::listen(event, &closure))
.await
.map_err(Error::Other)?;
closure.forget(); closure.forget();
let unlisten = js_sys::Function::from(unlisten); let unlisten = js_sys::Function::from(unlisten);
move || { Ok(move || {
unlisten.call0(&wasm_bindgen::JsValue::NULL).unwrap(); unlisten.call0(&wasm_bindgen::JsValue::NULL).unwrap();
} })
} }
/// Listen to an one-off event from the backend. /// Listen to an one-off event from the backend.
@ -115,7 +124,7 @@ where
/// ///
/// Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted. /// Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
#[inline(always)] #[inline(always)]
pub async fn once<T, H>(event: &str, mut handler: H) -> impl FnOnce() pub async fn once<T, H>(event: &str, mut handler: H) -> crate::Result<impl FnOnce()>
where where
T: DeserializeOwned, T: DeserializeOwned,
H: FnMut(Event<T>) + 'static, H: FnMut(Event<T>) + 'static,
@ -124,14 +133,16 @@ where
(handler)(serde_wasm_bindgen::from_value(raw).unwrap()) (handler)(serde_wasm_bindgen::from_value(raw).unwrap())
}); });
let unlisten = inner::once(event, &closure).await; let unlisten = JsFuture::from(inner::once(event, &closure))
.await
.map_err(Error::Other)?;
closure.forget(); closure.forget();
let unlisten = js_sys::Function::from(unlisten); let unlisten = js_sys::Function::from(unlisten);
move || { Ok(move || {
unlisten.call0(&wasm_bindgen::JsValue::NULL).unwrap(); unlisten.call0(&wasm_bindgen::JsValue::NULL).unwrap();
} })
} }
mod inner { mod inner {
@ -142,8 +153,8 @@ mod inner {
#[wasm_bindgen(module = "/dist/event.js")] #[wasm_bindgen(module = "/dist/event.js")]
extern "C" { extern "C" {
pub async fn emit(event: &str, payload: JsValue); pub fn emit(event: &str, payload: JsValue) -> js_sys::Promise;
pub async fn listen(event: &str, handler: &Closure<dyn FnMut(JsValue)>) -> JsValue; pub fn listen(event: &str, handler: &Closure<dyn FnMut(JsValue)>) -> js_sys::Promise;
pub async fn once(event: &str, handler: &Closure<dyn FnMut(JsValue)>) -> JsValue; pub fn once(event: &str, handler: &Closure<dyn FnMut(JsValue)>) -> js_sys::Promise;
} }
} }