dioxus/packages/core/README.md

99 lines
4.3 KiB
Markdown
Raw Normal View History

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
To build new apps with Dioxus or to extend the ecosystem with new hooks or components, use the higher-level `dioxus` crate with the appropriate feature flags.
2021-12-21 05:58:14 +00:00
2022-01-03 06:12:39 +00:00
```rust, ignore
2021-12-30 02:28:28 +00:00
fn app(cx: Scope) -> Element {
2021-12-21 06:11:27 +00:00
rsx!(cx, div { "hello world" })
2021-12-21 05:58:14 +00:00
}
fn main() {
let mut renderer = SomeRenderer::new();
// Creating a new virtualdom from a component
let mut dom = VirtualDom::new(app);
// Patching the renderer with the changes to draw the screen
let edits = dom.rebuild();
renderer.apply(edits);
// Injecting events
2021-12-21 06:11:27 +00:00
dom.handle_message(SchedulerMsg::Event(UserEvent {
scope_id: None,
priority: EventPriority::High,
element: ElementId(0),
name: "onclick",
data: Arc::new(()),
}));
// polling asynchronously
dom.wait_for_work().await;
// working with a deadline
if let Some(edits) = dom.work_with_deadline(|| false) {
renderer.apply(edits);
}
// getting state of scopes
let scope = dom.get_scope(ScopeId(0)).unwrap();
// iterating through the tree
match scope.root_node() {
VNodes::Text(vtext) => dbg!(vtext),
VNodes::Element(vel) => dbg!(vel),
_ => todo!()
}
2021-12-21 05:58:14 +00:00
}
```
2021-02-03 07:26:04 +00:00
## Internals
2021-02-03 07:26:04 +00:00
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-07-29 22:04:09 +00:00
- Dodrio: bump allocation, double buffering, and some diffing architecture
Dioxus-core leverages some really cool techniques and hits a very high level of parity with mature frameworks. However, Dioxus also brings some new unique features:
- managed lifetimes for borrowed data
- placeholder approach for suspended vnodes
- fiber/interruptible diffing algorithm
- custom memory allocator for vnodes and all text content
- support for fragments w/ lazy normalization
2021-07-23 21:03:51 +00:00
- slab allocator for scopes
- mirrored-slab approach for remote vdoms
- dedicated subtrees for rendering into separate contexts from the same app
There's certainly more to the story, but these optimizations make Dioxus memory use and allocation count extremely minimal. For an average application, it is possible that zero allocations will need to be performed once the app has been loaded. Only when new components are added to the dom will allocations occur. For a given component, the space of old VNodes is dynamically recycled as new nodes are added. Additionally, Dioxus tracks the average memory footprint of previous components to estimate how much memory allocate for future components.
All in all, Dioxus treats memory as a valuable resource. Combined with the memory-efficient footprint of Wasm compilation, Dioxus apps can scale to thousands of components and still stay snappy.
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
The final implementation of Dioxus must:
2021-01-14 07:56:41 +00:00
2021-10-24 17:30:36 +00:00
- Be **fast**. Allocators are typically slow in Wasm/Rust, so we should have a smart way of allocating.
- Be memory efficient. Servers should handle tens of thousands of simultaneous VDoms with no problem.
- Be concurrent. Components should be able to pause rendering to let the screen paint the next frame.
- Be disconnected from a specific renderer (no WebSys dependency in the core crate).
- Support server-side-rendering (SSR). VNodes should render to a string that can be served via a web server.
2021-02-03 07:26:04 +00:00
- 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-11-05 22:02:44 +00:00
## Safety
Dioxus uses unsafe. The design of Dioxus *requires* unsafe (self-referential trees).
2021-11-05 22:02:44 +00:00
All of our test suite passes MIRI without errors.
2021-11-05 22:02:44 +00:00
Dioxus deals with arenas, lifetimes, asynchronous tasks, custom allocators, pinning, and a lot more foundational low-level work that is very difficult to implement with 0 unsafe.
2021-11-23 20:53:57 +00:00
If you don't want to use a crate that uses unsafe, then this crate is not for you.
2021-11-23 20:53:57 +00:00
However, we are always interested in decreasing the scope of the core VirtualDom to make it easier to review. We'd be happy to welcome PRs that can eliminate unsafe code while still upholding the numerous invariants required to execute certain features.
2021-11-23 20:53:57 +00:00