wip: move examples around

This commit is contained in:
Jonathan Kelley committed 2021-07-11 18:39:45 -04:00
1 parent 269e81b0fd
commit 304259d818
24 files changed
+81 -71

No files matched your search

+21 -15
View File
@@ -8,16 +8,26 @@ description = "Core functionality for Dioxus - a concurrent renderer-agnostic Vi
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dioxus-core = { path="./packages/core", version="0.1.0" }
dioxus-core-macro = { path="./packages/core-macro", version="0.1.0", optional=true }
dioxus-html = { path="./packages/html", optional=true }
dioxus-web = { path="./packages/web", optional=true }
dioxus-webview = { path="./packages/webview", optional=true }
dioxus-hooks = { path="./packages/hooks", version="0.0.0", optional=true }
dioxus-core = { path = "./packages/core" }
dioxus-core-macro = { path = "./packages/core-macro", optional = true }
dioxus-html = { path = "./packages/html", optional = true }
dioxus-web = { path = "./packages/web", optional = true }
dioxus-webview = { path = "./packages/webview", optional = true }
dioxus-hooks = { path = "./packages/hooks", optional = true }
[features]
default = ["macro", "ssr", "hooks", "router", "core", "atoms", "html", "web", "desktop"]
default = [
"core",
"atoms",
"macro",
"ssr",
"hooks",
"router",
"html",
"web",
"desktop",
]
atoms = []
macro = ["dioxus-core-macro"]
ssr = []
@@ -31,29 +41,25 @@ desktop = ["dioxus-webview"]
core = []
[profile.dev]
split-debuginfo = "unpacked"
[dev-dependencies]
futures = "0.3.15"
log = "0.4.14"
num-format = "0.4.0"
rand = "0.8.4"
separator = "0.4.1"
serde = { version="1.0.126", features=["derive"] }
serde = { version = "1.0.126", features = ["derive"] }
surf = "2.2.0"
env_logger = "*"
async-std = { version="1.9.0", features=["attributes"] }
async-std = { version = "1.9.0", features = ["attributes"] }
[workspace]
members = [
"packages/core-macro",
"packages/core",
"packages/core-macro",
"packages/html",
"packages/hooks",
"packages/web",
"packages/webview",
"packages/hooks",
"packages/ssr",
# "packages/atoms",
# "packages/docsite",
+2 -2
View File
@@ -85,7 +85,7 @@ static AntipatternNestedFragments: FC<()> = |cx| {
/// recognize from the function signature, but Dioxus will not update the "live" version of state. Calling `set_state`
/// merely places a new value in the queue and schedules the component for a future update.
static AntipaternRelyingOnSetState: FC<()> = |cx| {
let (state, set_state) = use_state_classic(cx, || "Hello world");
let (state, set_state) = use_state(cx, || "Hello world").classic();
set_state("New state");
// This will return false! `state` will *still* be "Hello world"
assert!(state == &"New state");
@@ -126,7 +126,7 @@ static AntipatternMisusedHooks: FC<MisuedHooksProps> = |cx| {
if cx.should_render_state {
// do not place a hook in the conditional!
// prefer to move it out of the conditional
let (state, set_state) = use_state_classic(cx, || "hello world");
let (state, set_state) = use_state(cx, || "hello world").classic();
rsx!(in cx, div { "{state}" })
} else {
rsx!(in cx, div { "Not rendering state" })
+2 -6
View File
@@ -3,10 +3,6 @@
//!
//! This is a simple pattern that allows you to return no elements!
use dioxus::prelude::*;
fn main() {}
static Example: FC<()> = |cx| {
//
cx.render(rsx! {})
};
use dioxus::prelude::*;
static Example: FC<()> = |cx| cx.render(rsx! { Fragment {} });
+7 -6
View File
@@ -35,11 +35,12 @@ static Example2: FC<()> = |cx| {
// Using the `fragment` method on the NodeFactory
static Example3: FC<()> = |cx| {
cx.render(LazyNodes::new(move |fac: &NodeFactory| {
fac.fragment_builder(None, |list| {
list.add_child(fac.text(format_args!("A")))
.add_child(fac.text(format_args!("B")))
.finish()
})
cx.render(LazyNodes::new(move |fac| {
fac.fragment_from_iter([
fac.text(format_args!("A")),
fac.text(format_args!("B")),
fac.text(format_args!("A")),
fac.text(format_args!("B")),
])
}))
};
+2 -2
View File
@@ -2,11 +2,11 @@ use dioxus::prelude::*;
fn main() {}
static Example: FC<()> = |cx| {
let (g, set_g) = use_state_classic(cx, || 0);
let g = use_state(cx, || 0);
let v = (0..10).map(|f| {
rsx! {
li {
onclick: move |_| set_g(10)
onclick: move |_| g.set(10)
}
}
});
+2 -2
View File
@@ -11,11 +11,11 @@ fn main() {}
/// We can use `set_name` in multiple closures; the closures automatically *copy* the reference to set_name.
static ButtonList: FC<()> = |cx| {
let (name, set_name) = use_state_classic(cx, || "...?");
let name = use_state(cx, || "...?");
let names = ["jack", "jill", "john", "jane"]
.iter()
.map(move |n| rsx!(button { onclick: move |_| set_name(n), "{n}" }));
.map(move |n| rsx!(button { onclick: move |_| name.set(n), "{n}" }));
cx.render(rsx!(
div {
+23 -19
View File
@@ -16,7 +16,7 @@ struct DogApi {
const ENDPOINT: &str = "https://dog.ceo/api/breeds/image/random";
pub static App: FC<()> = |cx| {
let doggo = use_future_effect(&cx, move || async move {
let doggo = use_future_effect(cx, move || async move {
match surf::get(ENDPOINT).recv_json::<DogApi>().await {
Ok(res) => rsx!(in cx, img { src: "{res.message}" }),
Err(_) => rsx!(in cx, div { "No doggos for you :(" }),
@@ -31,29 +31,33 @@ pub static App: FC<()> = |cx| {
))
};
use dioxus_core::virtual_dom::SuspendedContext;
// use dioxus_core::virtual_dom::SuspendedContext;
use futures::Future;
use futures::FutureExt;
use std::pin::Pin;
fn use_fetch<'a, T: serde::de::DeserializeOwned + 'static>(
cx: &impl Scoped<'a>,
url: &str,
g: impl FnOnce(SuspendedContext, surf::Result<T>) -> VNode<'a> + 'a,
) -> VNode<'a> {
// essentially we're doing a "use_effect" but with no dependent props or post-render shenanigans
let fetch_promise = cx.use_hook(
move || surf::get(url).recv_json::<T>().boxed_local(),
// just pass the future through
|p| p,
|_| (),
);
cx.suspend(fetch_promise, g)
fn use_fetch<'a, T, P, F>(cx: Context<P>, url: &str, g: F) -> VNode<'a>
where
T: serde::de::DeserializeOwned + 'static,
// F: FnOnce(SuspendedContext, surf::Result<T>) -> VNode<'a> + 'a,
F: FnOnce((), surf::Result<T>) -> VNode<'a> + 'a,
{
todo!()
// // essentially we're doing a "use_effect" but with no dependent props or post-render shenanigans
// let fetch_promise = cx.use_hook(
// move || surf::get(url).recv_json::<T>().boxed_local(),
// // just pass the future through
// |p| p,
// |_| (),
// );
// cx.suspend(fetch_promise, g)
}
/// Spawns the future only when the inputs change
fn use_future_effect<'a, 'b, F: Future<Output = VNode<'b>>>(
cx: &impl Scoped<'a>,
g: impl FnOnce() -> F + 'a,
) -> VNode<'a> {
fn use_future_effect<'a, 'b, F, P, F2>(cx: Context<P>, g: F2) -> VNode<'a>
where
F: Future<Output = VNode<'b>>,
F2: FnOnce() -> F + 'a,
{
todo!()
}
+11 -4
View File
@@ -14,8 +14,15 @@ use dioxus::{builder::ElementBuilder, prelude::NodeFactory};
fn main() {}
// TODO
struct MyEle<'a, 'b> {
el: ElementBuilder<'a, 'b>,
fac: &'b NodeFactory<'a>,
mod dioxus_elements {
use dioxus::prelude::DioxusElement;
struct custom_element;
impl DioxusElement for custom_element {
const TAG_NAME: &'static str = "custom_element";
const NAME_SPACE: Option<&'static str> = None;
}
// Re-export the normal html namespace
pub use dioxus::prelude::dioxus_elements::*;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+3 -1
View File
@@ -356,7 +356,9 @@ impl ToTokens for ToToksCtx<&TextNode> {
fn to_tokens(&self, tokens: &mut TokenStream2) {
let mut token_stream = TokenStream2::new();
self.recurse(&self.inner.0).to_tokens(&mut token_stream);
tokens.append_all(quote! {dioxus::builder::text3(bump, format_args_f!(#token_stream))});
tokens.append_all(quote! {
__cx.text(format_args_f!(#token_stream))
});
}
}
+3 -9
View File
@@ -11,10 +11,10 @@ description = "Core functionality for Dioxus - a concurrent renderer-agnostic Vi
[dependencies]
# todo: use wast for faster load/compile
dioxus-core-macro = { path="../core-macro", version="0.1.1" }
dioxus-core-macro = { path = "../core-macro", version = "0.1.1" }
# Bumpalo is used as a micro heap backing each component
bumpalo = { version="3.6.0", features=["collections", "boxed"] }
bumpalo = { version = "3.6.0", features = ["collections", "boxed"] }
# custom error type
thiserror = "1"
@@ -29,7 +29,7 @@ longest-increasing-subsequence = "0.1.0"
log = "0.4"
# # Serialize the Edits for use in Webview/Liveview instances
serde = { version="1", features=["derive"], optional=true }
serde = { version = "1", features = ["derive"], optional = true }
# Backs scopes and unique keys
slotmap = "1.0.3"
@@ -38,12 +38,6 @@ appendlist = "1.4.0"
futures-util = "0.3.15"
[dev-dependencies]
dioxus-html = { path="../html" }
dioxus-hooks = { path="../hooks" }
env_logger = "*"
# async-std = { version="1.9.0", features=["attributes"] }
[features]
default = []
serialize = ["slotmap/serde", "serde"]
+3 -3
View File
@@ -43,12 +43,12 @@ pub struct VirtualDom {
/// The index of the root component
/// Should always be the first (gen=0, id=0)
pub(crate) base_scope: ScopeIdx,
pub base_scope: ScopeIdx,
/// All components dump their updates into a queue to be processed
pub(crate) event_queue: EventQueue,
pub event_queue: EventQueue,
pub(crate) tasks: TaskQueue,
pub tasks: TaskQueue,
root_props: std::pin::Pin<Box<dyn std::any::Any>>,
+1 -1
View File
@@ -7,4 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dioxus-core = { path = "../core" }
dioxus-core = { path = "../../packages/core", version = "*" }
+1 -1
View File
@@ -1,2 +1,2 @@
mod usestate;
pub use usestate::{use_state, UseState};
pub use usestate::use_state;