make eval copy
This commit is contained in:
1 parent
894a79d3df
commit
a789d6a64e
13 files changed
+191
-107
No files matched your search
@@ -1,9 +1,7 @@
|
||||
#![allow(clippy::await_holding_refcell_ref)]
|
||||
|
||||
use async_trait::async_trait;
|
||||
use dioxus_core::ScopeId;
|
||||
use dioxus_html::prelude::{EvalError, EvalProvider, Evaluator};
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use generational_box::{AnyStorage, GenerationalBox, UnsyncStorage};
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::query::{Query, QueryEngine};
|
||||
|
||||
@@ -14,48 +12,51 @@ pub fn init_eval() {
|
||||
ScopeId::ROOT.provide_context(provider);
|
||||
}
|
||||
|
||||
/// Reprents the desktop-target's provider of evaluators.
|
||||
/// Reprints the desktop-target's provider of evaluators.
|
||||
pub struct DesktopEvalProvider {
|
||||
query: QueryEngine,
|
||||
}
|
||||
|
||||
impl EvalProvider for DesktopEvalProvider {
|
||||
fn new_evaluator(&self, js: String) -> Result<Box<dyn Evaluator>, EvalError> {
|
||||
Ok(Box::new(DesktopEvaluator::new(self.query.clone(), js)))
|
||||
fn new_evaluator(&self, js: String) -> Result<GenerationalBox<Box<dyn Evaluator>>, EvalError> {
|
||||
Ok(DesktopEvaluator::create(self.query.clone(), js))
|
||||
}
|
||||
}
|
||||
|
||||
/// Reprents a desktop-target's JavaScript evaluator.
|
||||
pub(crate) struct DesktopEvaluator {
|
||||
query: Rc<RefCell<Query<serde_json::Value>>>,
|
||||
query: Query<serde_json::Value>,
|
||||
}
|
||||
|
||||
impl DesktopEvaluator {
|
||||
/// Creates a new evaluator for desktop-based targets.
|
||||
pub fn new(query: QueryEngine, js: String) -> Self {
|
||||
let query = query.new_query(&js);
|
||||
pub fn create(query_engine: QueryEngine, js: String) -> GenerationalBox<Box<dyn Evaluator>> {
|
||||
let query = query_engine.new_query(&js);
|
||||
// We create a generational box that is owned by the query slot so that when we drop the query slot, the generational box is also dropped.
|
||||
let owner = UnsyncStorage::owner();
|
||||
let query_id = query.id;
|
||||
let query = owner.insert(Box::new(DesktopEvaluator { query }) as Box<dyn Evaluator>);
|
||||
query_engine.active_requests.slab.borrow_mut()[query_id].owner = Some(owner);
|
||||
|
||||
Self {
|
||||
query: Rc::new(RefCell::new(query)),
|
||||
}
|
||||
query
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl Evaluator for DesktopEvaluator {
|
||||
/// # Panics
|
||||
/// This will panic if the query is currently being awaited.
|
||||
async fn join(&self) -> Result<serde_json::Value, EvalError> {
|
||||
fn poll_join(
|
||||
&mut self,
|
||||
context: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<Result<serde_json::Value, EvalError>> {
|
||||
self.query
|
||||
.borrow_mut()
|
||||
.result()
|
||||
.await
|
||||
.poll_result(context)
|
||||
.map_err(|e| EvalError::Communication(e.to_string()))
|
||||
}
|
||||
|
||||
/// Sends a message to the evaluated JavaScript.
|
||||
fn send(&self, data: serde_json::Value) -> Result<(), EvalError> {
|
||||
if let Err(e) = self.query.borrow_mut().send(data) {
|
||||
if let Err(e) = self.query.send(data) {
|
||||
return Err(EvalError::Communication(e.to_string()));
|
||||
}
|
||||
Ok(())
|
||||
@@ -65,11 +66,12 @@ impl Evaluator for DesktopEvaluator {
|
||||
///
|
||||
/// # Panics
|
||||
/// This will panic if the query is currently being awaited.
|
||||
async fn recv(&mut self) -> Result<serde_json::Value, EvalError> {
|
||||
fn poll_recv(
|
||||
&mut self,
|
||||
context: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<Result<serde_json::Value, EvalError>> {
|
||||
self.query
|
||||
.borrow_mut()
|
||||
.recv()
|
||||
.await
|
||||
.poll_recv(context)
|
||||
.map_err(|e| EvalError::Communication(e.to_string()))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user