2022-12-30 01:08:25 -05:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2023-03-23 15:19:00 -05:00
|
|
|
use crate::query::Query;
|
|
|
|
use crate::query::QueryError;
|
2022-12-30 01:08:25 -05:00
|
|
|
use crate::use_window;
|
|
|
|
use dioxus_core::ScopeState;
|
|
|
|
use std::future::Future;
|
|
|
|
use std::future::IntoFuture;
|
|
|
|
use std::pin::Pin;
|
|
|
|
|
|
|
|
/// A future that resolves to the result of a JavaScript evaluation.
|
|
|
|
pub struct EvalResult {
|
2023-03-23 15:19:00 -05:00
|
|
|
pub(crate) query: Query<serde_json::Value>,
|
2022-12-30 02:56:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EvalResult {
|
2023-03-23 15:19:00 -05:00
|
|
|
pub(crate) fn new(query: Query<serde_json::Value>) -> Self {
|
|
|
|
Self { query }
|
2022-12-30 02:56:10 -05:00
|
|
|
}
|
2022-12-30 01:08:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoFuture for EvalResult {
|
2023-03-23 15:19:00 -05:00
|
|
|
type Output = Result<serde_json::Value, QueryError>;
|
2022-12-30 01:08:25 -05:00
|
|
|
|
2023-03-23 15:19:00 -05:00
|
|
|
type IntoFuture = Pin<Box<dyn Future<Output = Result<serde_json::Value, QueryError>>>>;
|
2022-12-30 01:08:25 -05:00
|
|
|
|
|
|
|
fn into_future(self) -> Self::IntoFuture {
|
2023-03-23 15:19:00 -05:00
|
|
|
Box::pin(self.query.resolve())
|
|
|
|
as Pin<Box<dyn Future<Output = Result<serde_json::Value, QueryError>>>>
|
2022-12-30 01:08:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a closure that executes any JavaScript in the WebView context.
|
|
|
|
pub fn use_eval(cx: &ScopeState) -> &Rc<dyn Fn(String) -> EvalResult> {
|
|
|
|
let desktop = use_window(cx);
|
|
|
|
&*cx.use_hook(|| {
|
|
|
|
let desktop = desktop.clone();
|
|
|
|
|
|
|
|
Rc::new(move |script: String| desktop.eval(&script)) as Rc<dyn Fn(String) -> EvalResult>
|
|
|
|
})
|
|
|
|
}
|