2021-01-14 07:56:41 +00:00
# Dioxus-core
2021-02-03 07:26:04 +00:00
This is the core crate for the Dioxus Virtual DOM. This README will focus on the technical design and layout of this Virtual DOM implementation. If you want to read more about using Dioxus, then check out the Dioxus crate, documentation, and website.
2021-01-14 07:56:41 +00:00
2021-02-03 07:26:04 +00:00
## Internals
Dioxus-core builds off the many frameworks that came before it. Notably, Dioxus borrows these concepts:
2021-01-14 07:56:41 +00:00
2021-02-03 07:26:04 +00:00
- React: hooks, concurrency, suspense
2021-02-08 00:14:04 +00:00
- Dodrio: bump allocation, double buffering, and source code for nodes + NodeBuilder
2021-02-03 19:07:07 +00:00
- Percy: html! macro architecture, platform-agnostic edits
2021-02-03 07:26:04 +00:00
- Yew: passion and inspiration ❤️
2021-01-14 07:56:41 +00:00
2021-02-03 07:26:04 +00:00
## Goals
2021-01-14 07:56:41 +00:00
2021-02-03 07:26:04 +00:00
We have big goals for Dioxus. The final implementation must:
2021-01-14 07:56:41 +00:00
2021-02-03 07:26:04 +00:00
- Be **fast** . Allocators are typically slow in WASM/Rust, so we should have a smart way of allocating.
2021-02-03 19:07:07 +00:00
- Be extremely memory efficient. Servers should handle tens of thousands of simultaneous VDoms with no problem.
2021-02-03 07:26:04 +00:00
- Be concurrent. Components should be able to pause rendering using a threading mechanism.
2021-02-08 00:14:04 +00:00
- Be "remote". Edit lists should be separate from the Renderer implementation.
2021-02-03 07:26:04 +00:00
- Support SSR. VNodes should render to a string that can be served via a web server.
- Be "live". Components should be able to be both server rendered and client rendered without needing frontend APIs.
- Be modular. Components and hooks should be work anywhere without worrying about target platform.
2021-01-14 07:56:41 +00:00
2021-02-03 07:26:04 +00:00
## Optimizations
2021-01-14 07:56:41 +00:00
2021-02-03 07:26:04 +00:00
- Support a pluggable allocation strategy that makes VNode creation **very** fast
2021-02-08 00:14:04 +00:00
- Support lazy DomTrees (ie DomTrees that are not actually created when the html! macro is used)
- Support advanced diffing strategies (patience, Myers, etc)
2021-01-14 07:56:41 +00:00
2021-02-03 07:26:04 +00:00
## Design Quirks
2021-01-14 07:56:41 +00:00
2021-02-03 07:26:04 +00:00
- Use of "Context" as a way of mitigating threading issues and the borrow checker. (JS relies on globals)
2021-02-03 19:07:07 +00:00
- html! is lazy - needs to be used with a partner function to actually allocate the html. (Good be a good thing or a bad thing)
2021-02-03 07:26:04 +00:00
```rust
let text = TextRenderer::render(html! {< div > "hello world"< / div > });
// < div > hello world< / div >
```
2021-01-14 07:56:41 +00:00
2021-02-03 19:07:07 +00:00
```rust
fn main() {
tide::new()
.get("blah", serve_app("../"))
.get("blah", ws_handler(serve_app))
}
fn serve_app(ctx: & Context< ()>) -> VNode {
let livecontext = LiveContext::new()
.with_handler("graph", graph_component)
.with_handler("graph", graph_component)
.with_handler("graph", graph_component)
.with_handler("graph", graph_component)
.with_handler("graph", graph_component)
.with_handler("graph", graph_component)
.build();
2021-03-01 02:21:17 +00:00
ctx.render(html! {
2021-02-03 19:07:07 +00:00
< LiveContext ctx = {livecontext} >
< App / >
< / LiveContext >
})
}
```