mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-27 06:30:20 +00:00
Merge pull request #898 from Demonthos/make-use-eval-web-Rc
Make web and desktop use_eval compatible
This commit is contained in:
commit
e3b4021d34
1 changed files with 16 additions and 18 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
future::{IntoFuture, Ready},
|
future::{IntoFuture, Ready},
|
||||||
|
rc::Rc,
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -22,13 +23,11 @@ use serde_json::Value;
|
||||||
///
|
///
|
||||||
/// The closure will panic if the provided script is not valid JavaScript code
|
/// The closure will panic if the provided script is not valid JavaScript code
|
||||||
/// or if it returns an uncaught error.
|
/// or if it returns an uncaught error.
|
||||||
pub fn use_eval<S: std::string::ToString>(cx: &ScopeState) -> &dyn Fn(S) -> EvalResult {
|
pub fn use_eval(cx: &ScopeState) -> &Rc<dyn Fn(String) -> EvalResult> {
|
||||||
cx.use_hook(|| {
|
cx.use_hook(|| {
|
||||||
|script: S| {
|
Rc::new(|script: String| EvalResult {
|
||||||
let body = script.to_string();
|
|
||||||
EvalResult {
|
|
||||||
value: if let Ok(value) =
|
value: if let Ok(value) =
|
||||||
js_sys::Function::new_no_args(&body).call0(&wasm_bindgen::JsValue::NULL)
|
js_sys::Function::new_no_args(&script).call0(&wasm_bindgen::JsValue::NULL)
|
||||||
{
|
{
|
||||||
if let Ok(stringified) = js_sys::JSON::stringify(&value) {
|
if let Ok(stringified) = js_sys::JSON::stringify(&value) {
|
||||||
if !stringified.is_undefined() && stringified.is_valid_utf16() {
|
if !stringified.is_undefined() && stringified.is_valid_utf16() {
|
||||||
|
@ -43,8 +42,7 @@ pub fn use_eval<S: std::string::ToString>(cx: &ScopeState) -> &dyn Fn(S) -> Eval
|
||||||
} else {
|
} else {
|
||||||
Err(serde_json::Error::custom("Failed to execute script"))
|
Err(serde_json::Error::custom("Failed to execute script"))
|
||||||
},
|
},
|
||||||
}
|
}) as Rc<dyn Fn(String) -> EvalResult>
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue