Move the document trait into a separate crate (#3035)
* add a default head method through eval * remove the old document trait * implement document for each platform * pull out document into a dedicated crate to cut down on shared dependencies --------- Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
This commit is contained in:
1 parent
2b219c826c
commit
519ec9d294
91 files changed
+1113
-1044
No files matched your search
@@ -15,8 +15,8 @@ dioxus-html = { workspace = true, features = [
|
||||
"serialize",
|
||||
"mounted",
|
||||
"file_engine",
|
||||
"document",
|
||||
] }
|
||||
dioxus-document = { workspace = true }
|
||||
dioxus-signals = { workspace = true, optional = true }
|
||||
dioxus-interpreter-js = { workspace = true, features = ["binary-protocol", "serialize"] }
|
||||
dioxus-cli-config = { workspace = true }
|
||||
|
||||
@@ -16,23 +16,23 @@ static EVALS_RETURNED: GlobalSignal<usize> = Signal::global(|| 0);
|
||||
fn app() -> Element {
|
||||
// Double 100 values in the value
|
||||
use_future(|| async {
|
||||
let mut eval = eval(
|
||||
let mut eval = document::eval(
|
||||
r#"for (let i = 0; i < 100; i++) {
|
||||
let value = await dioxus.recv();
|
||||
dioxus.send(value*2);
|
||||
}"#,
|
||||
);
|
||||
for i in 0..100 {
|
||||
eval.send(serde_json::Value::from(i)).unwrap();
|
||||
let value = eval.recv().await.unwrap();
|
||||
assert_eq!(value, serde_json::Value::from(i * 2));
|
||||
eval.send(i).unwrap();
|
||||
let value: i32 = eval.recv().await.unwrap();
|
||||
assert_eq!(value, i * 2);
|
||||
EVALS_RECEIVED.with_mut(|x| *x += 1);
|
||||
}
|
||||
});
|
||||
|
||||
// Make sure returning no value resolves the future
|
||||
use_future(|| async {
|
||||
let eval = eval(r#"return;"#);
|
||||
let eval = document::eval(r#"return;"#);
|
||||
|
||||
eval.await.unwrap();
|
||||
EVALS_RETURNED.with_mut(|x| *x += 1);
|
||||
@@ -40,7 +40,7 @@ fn app() -> Element {
|
||||
|
||||
// Return a value from the future
|
||||
use_future(|| async {
|
||||
let eval = eval(
|
||||
let eval = document::eval(
|
||||
r#"
|
||||
return [1, 2, 3];
|
||||
"#,
|
||||
|
||||
@@ -16,7 +16,7 @@ fn use_inner_html(id: &'static str) -> Option<String> {
|
||||
spawn(async move {
|
||||
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
|
||||
|
||||
let res = eval(&format!(
|
||||
let res = document::eval(&format!(
|
||||
r#"let element = document.getElementById('{}');
|
||||
return element.innerHTML"#,
|
||||
id
|
||||
|
||||
@@ -50,7 +50,7 @@ pub fn mock_event_with_extra(id: &'static str, value: &'static str, extra: &'sta
|
||||
"#
|
||||
);
|
||||
|
||||
eval(&js).await.unwrap();
|
||||
document::eval(&js).await.unwrap();
|
||||
});
|
||||
})
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
use dioxus_html::document::{Document, EvalError, Evaluator};
|
||||
use dioxus_document::{Document, Eval, EvalError, Evaluator};
|
||||
use generational_box::{AnyStorage, GenerationalBox, UnsyncStorage};
|
||||
|
||||
use crate::{query::Query, DesktopContext};
|
||||
@@ -18,17 +18,13 @@ impl DesktopDocument {
|
||||
}
|
||||
|
||||
impl Document for DesktopDocument {
|
||||
fn new_evaluator(&self, js: String) -> GenerationalBox<Box<dyn Evaluator>> {
|
||||
DesktopEvaluator::create(self.desktop_ctx.clone(), js)
|
||||
fn eval(&self, js: String) -> Eval {
|
||||
Eval::new(DesktopEvaluator::create(self.desktop_ctx.clone(), js))
|
||||
}
|
||||
|
||||
fn set_title(&self, title: String) {
|
||||
self.desktop_ctx.window.set_title(&title);
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn std::any::Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents a desktop-target's JavaScript evaluator.
|
||||
|
||||
@@ -1 +1 @@
|
||||
[11927251734412729446]
|
||||
[14101548031762241351]
|
||||
@@ -2,7 +2,7 @@ import {
|
||||
Channel,
|
||||
DioxusChannel,
|
||||
WeakDioxusChannel,
|
||||
} from "../../../html/src/ts/eval";
|
||||
} from "../../../document/src/ts/eval";
|
||||
|
||||
// In dioxus desktop, eval needs to use the window object to store global state because we evaluate separate snippets of javascript in the browser
|
||||
declare global {
|
||||
@@ -86,5 +86,5 @@ export class NativeDioxusChannel extends DioxusChannel {
|
||||
}
|
||||
|
||||
// Receive data sent from javascript in rust. This is a no-op in the native interpreter because the rust code runs remotely
|
||||
async rustRecv(): Promise<any> {}
|
||||
async rustRecv(): Promise<any> { }
|
||||
}
|
||||
@@ -13,8 +13,9 @@ use crate::{
|
||||
Config, DesktopContext, DesktopService,
|
||||
};
|
||||
use dioxus_core::{Runtime, ScopeId, VirtualDom};
|
||||
use dioxus_document::Document;
|
||||
use dioxus_hooks::to_owned;
|
||||
use dioxus_html::{prelude::Document, HasFileData, HtmlEvent, PlatformEventData};
|
||||
use dioxus_html::{HasFileData, HtmlEvent, PlatformEventData};
|
||||
use futures_util::{pin_mut, FutureExt};
|
||||
use std::cell::OnceCell;
|
||||
use std::sync::Arc;
|
||||
|
||||
Reference in new issue
Block a user