dioxus/packages/core/src/virtual_dom.rs

611 lines
23 KiB
Rust
Raw Normal View History

2021-05-16 06:06:02 +00:00
//! # VirtualDOM Implementation for Rust
2021-09-10 00:58:48 +00:00
//!
2021-05-16 06:06:02 +00:00
//! This module provides the primary mechanics to create a hook-based, concurrent VDOM for Rust.
//!
//! In this file, multiple items are defined. This file is big, but should be documented well to
2021-10-24 17:30:36 +00:00
//! navigate the inner workings of the Dom. We try to keep these main mechanics in this file to limit
2021-05-16 06:06:02 +00:00
//! the possible exposed API surface (keep fields private). This particular implementation of VDOM
//! is extremely efficient, but relies on some unsafety under the hood to do things like manage
2021-05-18 05:16:43 +00:00
//! micro-heaps for components. We are currently working on refactoring the safety out into safe(r)
//! abstractions, but current tests (MIRI and otherwise) show no issues with the current implementation.
2021-05-16 06:06:02 +00:00
//!
//! Included is:
//! - The [`VirtualDom`] itself
2021-10-24 17:30:36 +00:00
//! - The [`Scope`] object for managing component lifecycle
2021-05-16 06:06:02 +00:00
//! - The [`ActiveFrame`] object for managing the Scope`s microheap
//! - The [`Context`] object for exposing VirtualDOM API to components
2021-10-24 17:30:36 +00:00
//! - The [`NodeFactory`] object for lazily exposing the `Context` API to the nodebuilder API
2021-05-16 06:06:02 +00:00
//!
//! This module includes just the barebones for a complete VirtualDOM API.
//! Additional functionality is defined in the respective files.
2021-08-27 13:40:04 +00:00
2021-11-05 20:28:08 +00:00
use crate::innerlude::*;
2021-10-04 05:28:04 +00:00
use futures_channel::mpsc::{UnboundedReceiver, UnboundedSender};
use futures_util::{Future, StreamExt};
2021-11-05 20:28:08 +00:00
use fxhash::FxHashSet;
use indexmap::IndexSet;
use std::pin::Pin;
use std::task::Poll;
use std::{any::Any, collections::VecDeque};
2021-10-04 05:28:04 +00:00
2021-02-03 07:26:04 +00:00
/// An integrated virtual node system that progresses events and diffs UI trees.
///
2021-09-10 00:58:48 +00:00
/// Differences are converted into patches which a renderer can use to draw the UI.
///
2021-09-10 00:58:48 +00:00
/// If you are building an App with Dioxus, you probably won't want to reach for this directly, instead opting to defer
/// to a particular crate's wrapper over the [`VirtualDom`] API.
///
2021-09-10 00:58:48 +00:00
/// Example
/// ```rust
2021-10-16 21:37:28 +00:00
/// static App: FC<()> = |(cx, props)|{
2021-09-10 00:58:48 +00:00
/// cx.render(rsx!{
/// div {
/// "Hello World"
/// }
/// })
/// }
///
2021-09-10 00:58:48 +00:00
/// async fn main() {
/// let mut dom = VirtualDom::new(App);
/// let mut inital_edits = dom.rebuild();
/// initialize_screen(inital_edits);
///
2021-09-10 00:58:48 +00:00
/// loop {
/// let next_frame = TimeoutFuture::new(Duration::from_millis(16));
/// let edits = dom.run_with_deadline(next_frame).await;
/// apply_edits(edits);
/// render_frame();
/// }
/// }
/// ```
pub struct VirtualDom {
2021-08-09 21:09:33 +00:00
base_scope: ScopeId,
2021-03-29 16:31:47 +00:00
_root_caller: *mut dyn Fn(&Scope) -> Element,
2021-11-05 20:28:08 +00:00
2021-11-09 07:11:44 +00:00
pub(crate) scopes: Box<ScopeArena>,
2021-11-05 20:28:08 +00:00
2021-11-07 03:11:17 +00:00
receiver: UnboundedReceiver<SchedulerMsg>,
pub(crate) sender: UnboundedSender<SchedulerMsg>,
2021-11-05 20:28:08 +00:00
// Every component that has futures that need to be polled
2021-11-07 03:11:17 +00:00
pending_futures: FxHashSet<ScopeId>,
pending_messages: VecDeque<SchedulerMsg>,
dirty_scopes: IndexSet<ScopeId>,
2021-02-03 07:26:04 +00:00
}
2021-11-07 03:11:17 +00:00
// Methods to create the VirtualDom
impl VirtualDom {
/// Create a new VirtualDOM with a component that does not have special props.
2021-05-16 06:06:02 +00:00
///
/// # Description
2021-05-16 06:06:02 +00:00
///
/// Later, the props can be updated by calling "update" with a new set of props, causing a set of re-renders.
2021-05-16 06:06:02 +00:00
///
/// This is useful when a component tree can be driven by external state (IE SSR) but it would be too expensive
/// to toss out the entire tree.
2021-05-16 06:06:02 +00:00
///
///
/// # Example
/// ```
2021-08-27 13:40:04 +00:00
/// fn Example(cx: Context<()>) -> DomTree {
/// cx.render(rsx!( div { "hello world" } ))
2021-05-16 06:06:02 +00:00
/// }
///
/// let dom = VirtualDom::new(Example);
/// ```
///
/// Note: the VirtualDOM is not progressed, you must either "run_with_deadline" or use "rebuild" to progress it.
2021-06-23 05:44:48 +00:00
pub fn new(root: FC<()>) -> Self {
Self::new_with_props(root, ())
2021-02-03 07:26:04 +00:00
}
/// Create a new VirtualDOM with the given properties for the root component.
///
/// # Description
///
2021-02-03 07:26:04 +00:00
/// Later, the props can be updated by calling "update" with a new set of props, causing a set of re-renders.
///
/// This is useful when a component tree can be driven by external state (IE SSR) but it would be too expensive
/// to toss out the entire tree.
2021-05-16 06:06:02 +00:00
///
///
/// # Example
/// ```
2021-08-27 13:40:04 +00:00
/// #[derive(PartialEq, Props)]
/// struct SomeProps {
/// name: &'static str
/// }
///
/// fn Example(cx: Context<SomeProps>) -> DomTree {
/// cx.render(rsx!{ div{ "hello {cx.name}" } })
2021-05-16 06:06:02 +00:00
/// }
///
/// let dom = VirtualDom::new(Example);
/// ```
///
2021-08-27 13:40:04 +00:00
/// Note: the VirtualDOM is not progressed on creation. You must either "run_with_deadline" or use "rebuild" to progress it.
///
/// ```rust
/// let mut dom = VirtualDom::new_with_props(Example, SomeProps { name: "jane" });
/// let mutations = dom.rebuild();
/// ```
2021-10-01 06:07:12 +00:00
pub fn new_with_props<P: 'static + Send>(root: FC<P>, root_props: P) -> Self {
2021-10-04 05:28:04 +00:00
let (sender, receiver) = futures_channel::mpsc::unbounded::<SchedulerMsg>();
Self::new_with_props_and_scheduler(root, root_props, sender, receiver)
}
2021-11-05 22:02:44 +00:00
/// Launch the VirtualDom, but provide your own channel for receiving and sending messages into the scheduler
2021-10-04 05:28:04 +00:00
///
/// This is useful when the VirtualDom must be driven from outside a thread and it doesn't make sense to wait for the
2021-10-24 17:30:36 +00:00
/// VirtualDom to be created just to retrieve its channel receiver.
pub fn new_with_props_and_scheduler<P: 'static>(
2021-10-04 05:28:04 +00:00
root: FC<P>,
root_props: P,
sender: UnboundedSender<SchedulerMsg>,
receiver: UnboundedReceiver<SchedulerMsg>,
) -> Self {
let mut scopes = ScopeArena::new(sender.clone());
let caller = Box::new(move |f: &Scope| -> Element { root(f, &root_props) });
let caller_ref: *mut dyn Fn(&Scope) -> Element = Box::into_raw(caller);
let base_scope = scopes.new_with_key(root as _, caller_ref, None, 0, 0);
2021-11-05 20:28:08 +00:00
2021-03-11 17:27:01 +00:00
Self {
2021-11-09 07:11:44 +00:00
scopes: Box::new(scopes),
2021-11-05 22:02:44 +00:00
base_scope,
receiver,
// todo: clean this up manually?
_root_caller: caller_ref,
2021-11-07 03:11:17 +00:00
pending_messages: VecDeque::new(),
2021-11-05 22:02:44 +00:00
pending_futures: Default::default(),
dirty_scopes: Default::default(),
sender,
2021-03-11 17:27:01 +00:00
}
}
2021-02-03 07:26:04 +00:00
2021-11-07 03:11:17 +00:00
/// Get the [`ScopeState`] for the root component.
2021-09-01 19:52:38 +00:00
///
2021-10-24 17:30:36 +00:00
/// This is useful for traversing the tree from the root for heuristics or alternsative renderers that use Dioxus
2021-09-01 19:52:38 +00:00
/// directly.
2021-11-07 03:11:17 +00:00
///
/// # Example
pub fn base_scope(&self) -> &Scope {
2021-11-07 03:11:17 +00:00
self.get_scope(&self.base_scope).unwrap()
2021-08-06 02:23:41 +00:00
}
2021-11-07 03:11:17 +00:00
/// Get the [`ScopeState`] for a component given its [`ScopeId`]
///
/// # Example
///
///
///
pub fn get_scope<'a>(&'a self, id: &ScopeId) -> Option<&'a Scope> {
2021-11-07 14:58:19 +00:00
self.scopes.get_scope(id)
2021-08-06 02:23:41 +00:00
}
2021-11-07 03:11:17 +00:00
/// Get an [`UnboundedSender`] handle to the channel used by the scheduler.
///
/// # Example
2021-09-01 19:45:53 +00:00
///
2021-09-01 19:52:38 +00:00
///
2021-11-07 03:11:17 +00:00
///
pub fn get_scheduler_channel(&self) -> futures_channel::mpsc::UnboundedSender<SchedulerMsg> {
self.sender.clone()
}
/// Check if the [`VirtualDom`] has any pending updates or work to be done.
2021-09-01 19:52:38 +00:00
///
2021-11-07 03:11:17 +00:00
/// # Example
2021-09-01 19:52:38 +00:00
///
///
2021-11-07 03:11:17 +00:00
///
pub fn has_any_work(&self) -> bool {
!(self.dirty_scopes.is_empty() && self.pending_messages.is_empty())
2021-09-01 19:45:53 +00:00
}
2021-11-07 03:11:17 +00:00
/// Waits for the scheduler to have work
/// This lets us poll async tasks during idle periods without blocking the main thread.
pub async fn wait_for_work(&mut self) {
// todo: poll the events once even if there is work to do to prevent starvation
2021-11-07 04:06:00 +00:00
// if there's no futures in the virtualdom, just wait for a scheduler message and put it into the queue to be processed
if self.pending_futures.is_empty() {
self.pending_messages
.push_front(self.receiver.next().await.unwrap());
} else {
struct PollTasks<'a> {
pending_futures: &'a FxHashSet<ScopeId>,
scopes: &'a ScopeArena,
}
2021-11-07 03:11:17 +00:00
2021-11-07 04:06:00 +00:00
impl<'a> Future for PollTasks<'a> {
type Output = ();
fn poll(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Self::Output> {
let mut all_pending = true;
// Poll every scope manually
for fut in self.pending_futures.iter() {
let scope = self
.scopes
.get_scope(fut)
.expect("Scope should never be moved");
let mut items = scope.items.borrow_mut();
for task in items.tasks.iter_mut() {
let task = task.as_mut();
// todo: does this make sense?
// I don't usually write futures by hand
// I think the futures neeed to be pinned using bumpbox or something
// right now, they're bump allocated so this shouldn't matter anyway - they're not going to move
let unpinned = unsafe { Pin::new_unchecked(task) };
2021-11-07 14:58:19 +00:00
if unpinned.poll(cx).is_ready() {
2021-11-07 04:06:00 +00:00
all_pending = false
2021-11-07 03:11:17 +00:00
}
}
}
2021-11-07 04:06:00 +00:00
// Resolve the future if any singular task is ready
match all_pending {
true => Poll::Pending,
false => Poll::Ready(()),
}
2021-11-07 03:11:17 +00:00
}
}
2021-11-07 04:06:00 +00:00
// Poll both the futures and the scheduler message queue simulataneously
use futures_util::future::{select, Either};
2021-11-07 03:11:17 +00:00
2021-11-07 04:06:00 +00:00
let scheduler_fut = self.receiver.next();
let tasks_fut = PollTasks {
pending_futures: &self.pending_futures,
scopes: &self.scopes,
};
match select(tasks_fut, scheduler_fut).await {
// Futures don't generate work
Either::Left((_, _)) => {}
2021-11-07 03:11:17 +00:00
2021-11-07 04:06:00 +00:00
// Save these messages in FIFO to be processed later
Either::Right((msg, _)) => self.pending_messages.push_front(msg.unwrap()),
}
2021-09-02 03:22:34 +00:00
}
2021-08-08 19:15:16 +00:00
}
2021-08-06 02:23:41 +00:00
2021-08-09 21:09:33 +00:00
/// Run the virtualdom with a deadline.
2021-08-08 19:15:16 +00:00
///
/// This method will progress async tasks until the deadline is reached. If tasks are completed before the deadline,
/// and no tasks are pending, this method will return immediately. If tasks are still pending, then this method will
/// exhaust the deadline working on them.
///
/// This method is useful when needing to schedule the virtualdom around other tasks on the main thread to prevent
/// "jank". It will try to finish whatever work it has by the deadline to free up time for other work.
///
2021-08-09 17:17:19 +00:00
/// Due to platform differences in how time is handled, this method accepts a future that resolves when the deadline
/// is exceeded. However, the deadline won't be met precisely, so you might want to build some wiggle room into the
/// deadline closure manually.
2021-08-08 19:15:16 +00:00
///
2021-08-09 21:09:33 +00:00
/// The deadline is polled before starting to diff components. This strikes a balance between the overhead of checking
2021-08-08 19:15:16 +00:00
/// the deadline and just completing the work. However, if an individual component takes more than 16ms to render, then
/// the screen will "jank" up. In debug, this will trigger an alert.
///
2021-08-09 17:17:19 +00:00
/// If there are no in-flight fibers when this method is called, it will await any possible tasks, aborting early if
/// the provided deadline future resolves.
///
/// For use in the web, it is expected that this method will be called to be executed during "idle times" and the
/// mutations to be applied during the "paint times" IE "animation frames". With this strategy, it is possible to craft
/// entirely jank-free applications that perform a ton of work.
///
2021-08-08 19:15:16 +00:00
/// # Example
///
/// ```no_run
2021-10-16 21:37:28 +00:00
/// static App: FC<()> = |(cx, props)|rsx!(cx, div {"hello"} );
2021-11-07 04:06:00 +00:00
///
2021-08-09 17:17:19 +00:00
/// let mut dom = VirtualDom::new(App);
2021-11-07 04:06:00 +00:00
///
2021-08-08 19:15:16 +00:00
/// loop {
2021-11-07 04:06:00 +00:00
/// let mut timeout = TimeoutFuture::from_ms(16);
2021-11-07 14:58:19 +00:00
/// let deadline = move || (&mut timeout).now_or_never();
2021-11-07 04:06:00 +00:00
///
2021-08-08 19:15:16 +00:00
/// let mutations = dom.run_with_deadline(deadline).await;
2021-11-07 04:06:00 +00:00
///
2021-08-08 19:15:16 +00:00
/// apply_mutations(mutations);
/// }
/// ```
2021-08-09 21:09:33 +00:00
///
/// ## Mutations
///
/// This method returns "mutations" - IE the necessary changes to get the RealDOM to match the VirtualDOM. It also
/// includes a list of NodeRefs that need to be applied and effects that need to be triggered after the RealDOM has
/// applied the edits.
///
/// Mutations are the only link between the RealDOM and the VirtualDOM.
2021-11-07 14:58:19 +00:00
pub fn work_with_deadline(&mut self, mut deadline: impl FnMut() -> bool) -> Vec<Mutations> {
let mut committed_mutations = vec![];
2021-11-05 20:28:08 +00:00
2021-11-07 03:11:17 +00:00
while self.has_any_work() {
while let Ok(Some(msg)) = self.receiver.try_next() {
2021-11-07 03:23:56 +00:00
self.pending_messages.push_front(msg);
}
2021-11-07 04:06:00 +00:00
while let Some(msg) = self.pending_messages.pop_back() {
2021-11-07 03:11:17 +00:00
match msg {
2021-11-07 03:23:56 +00:00
SchedulerMsg::Immediate(id) => {
self.dirty_scopes.insert(id);
2021-11-07 03:11:17 +00:00
}
2021-11-07 03:23:56 +00:00
SchedulerMsg::UiEvent(event) => {
if let Some(element) = event.mounted_dom_id {
log::info!("Calling listener {:?}, {:?}", event.scope_id, element);
2021-11-07 04:06:00 +00:00
let scope = self.scopes.get_scope(&event.scope_id).unwrap();
2021-11-07 03:23:56 +00:00
// TODO: bubble properly here
scope.call_listener(event, element);
while let Ok(Some(dirty_scope)) = self.receiver.try_next() {
2021-11-07 04:06:00 +00:00
self.pending_messages.push_front(dirty_scope);
2021-11-07 03:23:56 +00:00
}
2021-11-07 04:06:00 +00:00
} else {
2021-11-07 14:58:19 +00:00
log::debug!("User event without a targetted ElementId. Not currently supported.\nUnsure how to proceed. {:?}", event);
2021-11-07 03:23:56 +00:00
}
2021-11-07 03:11:17 +00:00
}
}
}
2021-11-05 20:28:08 +00:00
2021-11-09 07:11:44 +00:00
let scopes = &self.scopes;
let mut diff_state = DiffState::new(scopes);
2021-11-05 20:28:08 +00:00
2021-11-07 04:06:00 +00:00
let mut ran_scopes = FxHashSet::default();
2021-11-05 20:28:08 +00:00
2021-11-07 04:06:00 +00:00
// todo: the 2021 version of rust will let us not have to force the borrow
2021-11-09 07:11:44 +00:00
// let scopes = &self.scopes;
2021-11-07 04:06:00 +00:00
// Sort the scopes by height. Theoretically, we'll de-duplicate scopes by height
self.dirty_scopes
.retain(|id| scopes.get_scope(id).is_some());
2021-11-05 20:28:08 +00:00
2021-11-07 04:06:00 +00:00
self.dirty_scopes.sort_by(|a, b| {
let h1 = scopes.get_scope(a).unwrap().height;
let h2 = scopes.get_scope(b).unwrap().height;
h1.cmp(&h2).reverse()
});
2021-11-05 20:28:08 +00:00
2021-11-07 04:06:00 +00:00
if let Some(scopeid) = self.dirty_scopes.pop() {
log::info!("handling dirty scope {:?}", scopeid);
2021-11-07 03:23:56 +00:00
2021-11-07 04:06:00 +00:00
if !ran_scopes.contains(&scopeid) {
ran_scopes.insert(scopeid);
2021-11-05 20:28:08 +00:00
2021-11-07 04:06:00 +00:00
log::debug!("about to run scope {:?}", scopeid);
2021-11-05 20:28:08 +00:00
if self.scopes.run_scope(&scopeid) {
2021-11-08 03:36:57 +00:00
let (old, new) = (
self.scopes.wip_head(&scopeid),
self.scopes.fin_head(&scopeid),
);
2021-11-07 04:06:00 +00:00
diff_state.stack.scope_stack.push(scopeid);
diff_state.stack.push(DiffInstruction::Diff { new, old });
}
}
}
2021-11-05 20:28:08 +00:00
2021-11-09 07:11:44 +00:00
// let scopes = &mut self.scopes;
let work_completed = diff_state.work(&mut deadline);
// let work_completed = crate::diff::work(&mut diff_state, &mut deadline);
// let work_completed = crate::diff::work(&mut diff_state, &mut deadline);
2021-11-05 20:28:08 +00:00
2021-11-07 04:06:00 +00:00
if work_completed {
let DiffState {
mutations,
seen_scopes,
stack,
..
} = diff_state;
2021-11-05 20:28:08 +00:00
2021-11-07 04:06:00 +00:00
for scope in seen_scopes {
self.dirty_scopes.remove(&scope);
2021-11-07 03:23:56 +00:00
}
2021-11-05 20:28:08 +00:00
2021-11-07 04:06:00 +00:00
// I think the stack should be empty at the end of diffing?
debug_assert_eq!(stack.scope_stack.len(), 0);
committed_mutations.push(mutations);
} else {
2021-11-07 14:58:19 +00:00
// leave the work in an incomplete state
log::debug!("don't have a mechanism to pause work (yet)");
2021-11-07 03:23:56 +00:00
return committed_mutations;
2021-11-05 20:28:08 +00:00
}
2021-11-07 03:23:56 +00:00
}
2021-11-05 20:28:08 +00:00
2021-11-07 03:23:56 +00:00
committed_mutations
}
2021-11-05 20:28:08 +00:00
2021-11-07 03:23:56 +00:00
/// Performs a *full* rebuild of the virtual dom, returning every edit required to generate the actual dom from scratch
///
/// The diff machine expects the RealDom's stack to be the root of the application.
///
/// Tasks will not be polled with this method, nor will any events be processed from the event queue. Instead, the
/// root component will be ran once and then diffed. All updates will flow out as mutations.
///
/// All state stored in components will be completely wiped away.
///
/// # Example
/// ```
/// static App: FC<()> = |(cx, props)| cx.render(rsx!{ "hello world" });
/// let mut dom = VirtualDom::new();
/// let edits = dom.rebuild();
///
/// apply_edits(edits);
/// ```
pub fn rebuild(&mut self) -> Mutations {
2021-11-09 07:11:44 +00:00
let mut diff_state = DiffState::new(&self.scopes);
2021-11-07 04:06:00 +00:00
2021-11-08 03:36:57 +00:00
let scope_id = self.base_scope;
if self.scopes.run_scope(&scope_id) {
2021-11-09 07:11:44 +00:00
todo!();
// diff_state
// .stack
// .create_node(self.scopes.fin_head(&scope_id), MountType::Append);
// diff_state.stack.scope_stack.push(scope_id);
let work_completed = diff_state.work(|| false);
// let scopes = &mut self.scopes;
//
// let work_completed = crate::diff::work(&mut diff_state, || false);
// self.scopes.work(&mut diff_machine, || false);
2021-11-08 03:36:57 +00:00
}
2021-11-09 07:11:44 +00:00
diff_state.mutations
2021-11-05 20:28:08 +00:00
}
2021-11-07 03:11:17 +00:00
/// Compute a manual diff of the VirtualDOM between states.
2021-11-05 20:28:08 +00:00
///
2021-11-07 03:11:17 +00:00
/// This can be useful when state inside the DOM is remotely changed from the outside, but not propagated as an event.
2021-11-05 20:28:08 +00:00
///
2021-11-07 03:11:17 +00:00
/// In this case, every component will be diffed, even if their props are memoized. This method is intended to be used
/// to force an update of the DOM when the state of the app is changed outside of the app.
2021-11-05 20:28:08 +00:00
///
///
2021-11-07 03:11:17 +00:00
/// # Example
/// ```rust
/// #[derive(PartialEq, Props)]
/// struct AppProps {
/// value: Shared<&'static str>,
/// }
///
/// static App: FC<AppProps> = |(cx, props)|{
/// let val = cx.value.borrow();
/// cx.render(rsx! { div { "{val}" } })
/// };
///
/// let value = Rc::new(RefCell::new("Hello"));
/// let mut dom = VirtualDom::new_with_props(
/// App,
/// AppProps {
/// value: value.clone(),
/// },
/// );
///
/// let _ = dom.rebuild();
///
/// *value.borrow_mut() = "goodbye";
///
/// let edits = dom.diff();
/// ```
2021-11-07 04:06:00 +00:00
pub fn hard_diff<'a>(&'a mut self, scope_id: &ScopeId) -> Option<Mutations<'a>> {
log::debug!("hard diff {:?}", scope_id);
2021-11-07 03:11:17 +00:00
if self.scopes.run_scope(scope_id) {
2021-11-09 07:11:44 +00:00
let mut diff_machine = DiffState::new(&self.scopes);
2021-11-05 20:28:08 +00:00
2021-11-07 04:06:00 +00:00
diff_machine.force_diff = true;
2021-11-05 20:28:08 +00:00
2021-11-09 07:11:44 +00:00
let work_completed = diff_machine.work(|| false);
// let scopes = &mut self.scopes;
// crate::diff::diff_scope(&mut diff_machine, scope_id);
// self.scopes.diff_scope(&mut diff_machine, scope_id);
// dbg!(&diff_machine.mutations);
// let DiffState {
// mutations,
// stack,
// seen_scopes,
// force_diff,
// ..
// } = mutations;
2021-11-05 20:28:08 +00:00
2021-11-09 07:11:44 +00:00
// let mutations = diff_machine.mutations;
2021-11-08 03:36:57 +00:00
2021-11-09 07:11:44 +00:00
// Some(unsafe { std::mem::transmute(mutations) })
todo!()
2021-11-05 20:28:08 +00:00
} else {
2021-11-07 04:06:00 +00:00
None
2021-11-05 20:28:08 +00:00
}
}
2021-09-13 23:07:15 +00:00
}
2021-11-07 03:23:56 +00:00
pub enum SchedulerMsg {
// events from the host
UiEvent(UserEvent),
// setstate
Immediate(ScopeId),
}
#[derive(Debug)]
pub struct UserEvent {
/// The originator of the event trigger
pub scope_id: ScopeId,
pub priority: EventPriority,
/// The optional real node associated with the trigger
pub mounted_dom_id: Option<ElementId>,
/// The event type IE "onclick" or "onmouseover"
///
/// The name that the renderer will use to mount the listener.
pub name: &'static str,
2021-11-07 06:01:22 +00:00
/// Event Data
2021-11-07 03:23:56 +00:00
pub event: Box<dyn Any + Send>,
}
/// Priority of Event Triggers.
///
/// Internally, Dioxus will abort work that's taking too long if new, more important work arrives. Unlike React, Dioxus
/// won't be afraid to pause work or flush changes to the RealDOM. This is called "cooperative scheduling". Some Renderers
/// implement this form of scheduling internally, however Dioxus will perform its own scheduling as well.
///
/// The ultimate goal of the scheduler is to manage latency of changes, prioritizing "flashier" changes over "subtler" changes.
///
/// React has a 5-tier priority system. However, they break things into "Continuous" and "Discrete" priority. For now,
/// we keep it simple, and just use a 3-tier priority system.
///
/// - NoPriority = 0
/// - LowPriority = 1
/// - NormalPriority = 2
/// - UserBlocking = 3
/// - HighPriority = 4
/// - ImmediatePriority = 5
///
/// We still have a concept of discrete vs continuous though - discrete events won't be batched, but continuous events will.
/// This means that multiple "scroll" events will be processed in a single frame, but multiple "click" events will be
/// flushed before proceeding. Multiple discrete events is highly unlikely, though.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, PartialOrd, Ord)]
pub enum EventPriority {
/// Work that must be completed during the EventHandler phase.
///
/// Currently this is reserved for controlled inputs.
Immediate = 3,
/// "High Priority" work will not interrupt other high priority work, but will interrupt medium and low priority work.
///
/// This is typically reserved for things like user interaction.
///
/// React calls these "discrete" events, but with an extra category of "user-blocking" (Immediate).
High = 2,
/// "Medium priority" work is generated by page events not triggered by the user. These types of events are less important
/// than "High Priority" events and will take precedence over low priority events.
///
/// This is typically reserved for VirtualEvents that are not related to keyboard or mouse input.
///
/// React calls these "continuous" events (e.g. mouse move, mouse wheel, touch move, etc).
Medium = 1,
/// "Low Priority" work will always be preempted unless the work is significantly delayed, in which case it will be
/// advanced to the front of the work queue until completed.
///
/// The primary user of Low Priority work is the asynchronous work system (Suspense).
///
/// This is considered "idle" work or "background" work.
Low = 0,
}