make eval copy
This commit is contained in:
1 parent
894a79d3df
commit
a789d6a64e
13 files changed
+191
-107
No files matched your search
@@ -1,7 +1,5 @@
|
||||
#![allow(clippy::await_holding_refcell_ref)]
|
||||
use async_trait::async_trait;
|
||||
use dioxus_html::prelude::{EvalError, EvalProvider, Evaluator};
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use generational_box::{AnyStorage, GenerationalBox, UnsyncStorage};
|
||||
|
||||
use crate::{query::Query, DesktopContext};
|
||||
|
||||
@@ -17,55 +15,57 @@ impl DesktopEvalProvider {
|
||||
}
|
||||
|
||||
impl EvalProvider for DesktopEvalProvider {
|
||||
fn new_evaluator(&self, js: String) -> Result<Box<dyn Evaluator>, EvalError> {
|
||||
Ok(Box::new(DesktopEvaluator::new(
|
||||
self.desktop_ctx.clone(),
|
||||
js,
|
||||
)))
|
||||
fn new_evaluator(&self, js: String) -> Result<GenerationalBox<Box<dyn Evaluator>>, EvalError> {
|
||||
Ok(DesktopEvaluator::create(self.desktop_ctx.clone(), js))
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents 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(desktop_ctx: DesktopContext, js: String) -> Self {
|
||||
pub fn create(desktop_ctx: DesktopContext, js: String) -> GenerationalBox<Box<dyn Evaluator>> {
|
||||
let ctx = desktop_ctx.clone();
|
||||
let query = desktop_ctx.query.new_query(&js, ctx);
|
||||
|
||||
Self {
|
||||
query: Rc::new(RefCell::new(query)),
|
||||
}
|
||||
// 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>);
|
||||
desktop_ctx.query.active_requests.slab.borrow_mut()[query_id].owner = Some(owner);
|
||||
|
||||
query
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl Evaluator for DesktopEvaluator {
|
||||
async fn join(&self) -> Result<serde_json::Value, EvalError> {
|
||||
fn poll_join(
|
||||
&mut self,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<Result<serde_json::Value, EvalError>> {
|
||||
self.query
|
||||
.borrow_mut()
|
||||
.result()
|
||||
.await
|
||||
.poll_result(cx)
|
||||
.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(())
|
||||
}
|
||||
|
||||
/// Gets an UnboundedReceiver to receive messages from the evaluated JavaScript.
|
||||
async fn recv(&mut self) -> Result<serde_json::Value, EvalError> {
|
||||
fn poll_recv(
|
||||
&mut self,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<Result<serde_json::Value, EvalError>> {
|
||||
self.query
|
||||
.borrow_mut()
|
||||
.recv()
|
||||
.await
|
||||
.poll_recv(cx)
|
||||
.map_err(|e| EvalError::Communication(e.to_string()))
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
use crate::DesktopContext;
|
||||
use futures_util::StreamExt;
|
||||
use futures_util::{FutureExt, StreamExt};
|
||||
use generational_box::Owner;
|
||||
use serde::{de::DeserializeOwned, Deserialize};
|
||||
use serde_json::Value;
|
||||
use slab::Slab;
|
||||
@@ -43,8 +44,8 @@ let dioxus = {
|
||||
|
||||
/// Tracks what query ids are currently active
|
||||
|
||||
struct SharedSlab<T = ()> {
|
||||
slab: Rc<RefCell<Slab<T>>>,
|
||||
pub(crate) struct SharedSlab<T = ()> {
|
||||
pub slab: Rc<RefCell<Slab<T>>>,
|
||||
}
|
||||
|
||||
impl<T> Clone for SharedSlab<T> {
|
||||
@@ -63,9 +64,10 @@ impl<T> Default for SharedSlab<T> {
|
||||
}
|
||||
}
|
||||
|
||||
struct QueryEntry {
|
||||
pub(crate) struct QueryEntry {
|
||||
channel_sender: futures_channel::mpsc::UnboundedSender<Value>,
|
||||
return_sender: Option<futures_channel::oneshot::Sender<Value>>,
|
||||
pub owner: Option<Owner>,
|
||||
}
|
||||
|
||||
const QUEUE_NAME: &str = "__msg_queues";
|
||||
@@ -73,7 +75,7 @@ const QUEUE_NAME: &str = "__msg_queues";
|
||||
/// Handles sending and receiving arbitrary queries from the webview. Queries can be resolved non-sequentially, so we use ids to track them.
|
||||
#[derive(Clone, Default)]
|
||||
pub(crate) struct QueryEngine {
|
||||
active_requests: SharedSlab<QueryEntry>,
|
||||
pub active_requests: SharedSlab<QueryEntry>,
|
||||
}
|
||||
|
||||
impl QueryEngine {
|
||||
@@ -88,6 +90,7 @@ impl QueryEngine {
|
||||
let request_id = self.active_requests.slab.borrow_mut().insert(QueryEntry {
|
||||
channel_sender: tx,
|
||||
return_sender: Some(return_tx),
|
||||
owner: None,
|
||||
});
|
||||
|
||||
// start the query
|
||||
@@ -161,7 +164,7 @@ pub(crate) struct Query<V: DeserializeOwned> {
|
||||
slab: SharedSlab<QueryEntry>,
|
||||
receiver: futures_channel::mpsc::UnboundedReceiver<Value>,
|
||||
return_receiver: Option<futures_channel::oneshot::Receiver<Value>>,
|
||||
id: usize,
|
||||
pub id: usize,
|
||||
phantom: std::marker::PhantomData<V>,
|
||||
}
|
||||
|
||||
@@ -198,9 +201,14 @@ impl<V: DeserializeOwned> Query<V> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Receive a message from the query
|
||||
pub async fn recv(&mut self) -> Result<Value, QueryError> {
|
||||
self.receiver.next().await.ok_or(QueryError::Recv)
|
||||
/// Poll the query for a message
|
||||
pub fn poll_recv(
|
||||
&mut self,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<Result<Value, QueryError>> {
|
||||
self.receiver
|
||||
.poll_next_unpin(cx)
|
||||
.map(|result| result.ok_or(QueryError::Recv))
|
||||
}
|
||||
|
||||
/// Receive the result of the query
|
||||
@@ -210,6 +218,17 @@ impl<V: DeserializeOwned> Query<V> {
|
||||
None => Err(QueryError::Finished),
|
||||
}
|
||||
}
|
||||
|
||||
/// Poll the query for a result
|
||||
pub fn poll_result(
|
||||
&mut self,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<Result<Value, QueryError>> {
|
||||
match self.return_receiver.as_mut() {
|
||||
Some(receiver) => receiver.poll_unpin(cx).map_err(|_| QueryError::Recv),
|
||||
None => std::task::Poll::Ready(Err(QueryError::Finished)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: DeserializeOwned> Drop for Query<V> {
|
||||
|
||||
Reference in new issue
Block a user