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.
///
/// # Example
@ -8,8 +12,11 @@
/// let clipboard_text = read_text().await;
/// ```
#[inline(always)]
pub async fn read_text() -> Option<String> {
inner::readText().await.as_string()
pub async fn read_text() -> crate::Result<Option<String>> {
JsFuture::from(inner::readText())
.await
.map(|v| v.as_string())
.map_err(Error::Other)
}
/// 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.
#[inline(always)]
pub async fn write_text(text: &str) {
inner::writeText(text).await
pub async fn write_text(text: &str) -> crate::Result<()> {
JsFuture::from(inner::writeText(text))
.await
.map_err(Error::Other)?;
Ok(())
}
mod inner {
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
use wasm_bindgen::{prelude::wasm_bindgen};
#[wasm_bindgen(module = "/dist/clipboard.js")]
extern "C" {
pub async fn readText() -> JsValue;
pub async fn writeText(text: &str);
pub fn readText() -> js_sys::Promise;
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 wasm_bindgen::{prelude::Closure, JsValue};
use wasm_bindgen_futures::JsFuture;
use crate::Error;
#[derive(Deserialize)]
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 `_`.
#[inline(always)]
pub async fn emit<T: Serialize>(event: &str, payload: &T) {
inner::emit(event, serde_wasm_bindgen::to_value(payload).unwrap()).await
pub async fn emit<T: Serialize>(event: &str, payload: &T) -> crate::Result<()> {
JsFuture::from(inner::emit(event, serde_wasm_bindgen::to_value(payload)?))
.await
.map_err(Error::Other)?;
Ok(())
}
/// 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.
#[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
T: DeserializeOwned,
H: FnMut(Event<T>) + 'static,
@ -79,14 +86,16 @@ where
(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();
let unlisten = js_sys::Function::from(unlisten);
move || {
Ok(move || {
unlisten.call0(&wasm_bindgen::JsValue::NULL).unwrap();
}
})
}
/// 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.
#[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
T: DeserializeOwned,
H: FnMut(Event<T>) + 'static,
@ -124,14 +133,16 @@ where
(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();
let unlisten = js_sys::Function::from(unlisten);
move || {
Ok(move || {
unlisten.call0(&wasm_bindgen::JsValue::NULL).unwrap();
}
})
}
mod inner {
@ -142,8 +153,8 @@ mod inner {
#[wasm_bindgen(module = "/dist/event.js")]
extern "C" {
pub async fn emit(event: &str, payload: JsValue);
pub async fn listen(event: &str, handler: &Closure<dyn FnMut(JsValue)>) -> JsValue;
pub async fn once(event: &str, handler: &Closure<dyn FnMut(JsValue)>) -> JsValue;
pub fn emit(event: &str, payload: JsValue) -> js_sys::Promise;
pub fn listen(event: &str, handler: &Closure<dyn FnMut(JsValue)>) -> js_sys::Promise;
pub fn once(event: &str, handler: &Closure<dyn FnMut(JsValue)>) -> js_sys::Promise;
}
}