Hotreloading of for/if/body, formatted strings, literals, component props, nested rsx, light CLI rewrite, cli TUI (#2258)
Hotreload the contents of for loops, if chains, component bodies, props, attributes, and any literals discovered in rsx! Add a TUI renderer to the CLI. Improve the CLI build system to be async and parallel. Refactor RSX to allow partial expansion of expressions. Merge autofmt implementations for consistency. Merge the representation of elements and components under the hood. Add a diagnostics system for rsx for improved error messages. Drop interprocess and move to websockets for communication between the CLI and the server. Assign IDs to nodes and attributes in a stable way to be used in non compiler contexts. Add hotreloading to any body of component/for loop/if chain/etc. --------- Co-authored-by: Evan Almloff <evanalmloff@gmail.com> Co-authored-by: Liam Mitchell <liamkarlmitchell@gmail.com>
This commit is contained in:
181 files changed
+12755
-10806
No files matched your search
@@ -1,5 +1,5 @@
|
||||
use crate::read_impls;
|
||||
use crate::{read::Readable, Memo, ReadableRef};
|
||||
use crate::{read_impls, GlobalKey};
|
||||
use dioxus_core::prelude::ScopeId;
|
||||
use generational_box::UnsyncStorage;
|
||||
use std::ops::Deref;
|
||||
@@ -11,20 +11,31 @@ use super::get_global_context;
|
||||
/// A signal that can be accessed from anywhere in the application and created in a static
|
||||
pub struct GlobalMemo<T: 'static> {
|
||||
selector: fn() -> T,
|
||||
key: GlobalKey<'static>,
|
||||
}
|
||||
|
||||
impl<T: PartialEq + 'static> GlobalMemo<T> {
|
||||
#[track_caller]
|
||||
/// Create a new global signal
|
||||
pub const fn new(selector: fn() -> T) -> GlobalMemo<T>
|
||||
where
|
||||
T: PartialEq,
|
||||
{
|
||||
GlobalMemo { selector }
|
||||
let key = std::panic::Location::caller();
|
||||
GlobalMemo {
|
||||
selector,
|
||||
key: GlobalKey::new(key),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the key for this global
|
||||
pub fn key(&self) -> GlobalKey<'static> {
|
||||
self.key.clone()
|
||||
}
|
||||
|
||||
/// Get the signal that backs this global.
|
||||
pub fn memo(&self) -> Memo<T> {
|
||||
let key = self as *const _ as *const ();
|
||||
let key = self.key();
|
||||
|
||||
let context = get_global_context();
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use dioxus_core::prelude::{provide_root_context, try_consume_context};
|
||||
use std::{any::Any, cell::RefCell, collections::HashMap, rc::Rc};
|
||||
use std::{any::Any, cell::RefCell, collections::HashMap, panic::Location, rc::Rc};
|
||||
|
||||
mod memo;
|
||||
pub use memo::*;
|
||||
@@ -7,12 +7,76 @@ pub use memo::*;
|
||||
mod signal;
|
||||
pub use signal::*;
|
||||
|
||||
use crate::Signal;
|
||||
|
||||
/// The context for global signals
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct GlobalSignalContext {
|
||||
signal: Rc<RefCell<HashMap<*const (), Box<dyn Any>>>>,
|
||||
pub struct GlobalSignalContext {
|
||||
signal: Rc<RefCell<HashMap<GlobalKey<'static>, Box<dyn Any>>>>,
|
||||
}
|
||||
|
||||
pub(crate) fn get_global_context() -> GlobalSignalContext {
|
||||
/// A key used to identify a signal in the global signal context
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct GlobalKey<'a> {
|
||||
// We create an extra wrapper around location so we can construct it manually for hot reloading
|
||||
file: &'a str,
|
||||
line: u32,
|
||||
column: u32,
|
||||
}
|
||||
|
||||
impl<'a> GlobalKey<'a> {
|
||||
/// Create a new key from a location
|
||||
pub const fn new(key: &'a Location<'a>) -> Self {
|
||||
GlobalKey {
|
||||
file: key.file(),
|
||||
line: key.line(),
|
||||
column: key.column(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new key from a static string
|
||||
#[allow(unused)]
|
||||
pub const fn new_from_str(key: &'a str) -> Self {
|
||||
GlobalKey {
|
||||
file: key,
|
||||
line: 0,
|
||||
column: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&'static str> for GlobalKey<'static> {
|
||||
fn from(key: &'static str) -> Self {
|
||||
Self::new_from_str(key)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&'static Location<'static>> for GlobalKey<'static> {
|
||||
fn from(key: &'static Location<'static>) -> Self {
|
||||
Self::new(key)
|
||||
}
|
||||
}
|
||||
|
||||
impl GlobalSignalContext {
|
||||
/// Get a signal with the given string key
|
||||
/// The key will be converted to a UUID with the appropriate internal namespace
|
||||
pub fn get_signal_with_key<T>(&self, key: &str) -> Option<Signal<T>> {
|
||||
let key = GlobalKey::new_from_str(key);
|
||||
|
||||
self.signal.borrow().get(&key).map(|f| {
|
||||
*f.downcast_ref::<Signal<T>>().unwrap_or_else(|| {
|
||||
panic!(
|
||||
"Global signal with key {:?} is not of the expected type. Keys are {:?}",
|
||||
key,
|
||||
self.signal.borrow().keys()
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the global context for signals
|
||||
pub fn get_global_context() -> GlobalSignalContext {
|
||||
match try_consume_context() {
|
||||
Some(context) => context,
|
||||
None => {
|
||||
@@ -23,3 +87,30 @@ pub(crate) fn get_global_context() -> GlobalSignalContext {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Test that keys of global signals are correctly generated and different from one another.
|
||||
/// We don't want signals to merge, but we also want them to use both string IDs and memory addresses.
|
||||
#[test]
|
||||
fn test_global_keys() {
|
||||
// we're using consts since it's harder than statics due to merging - these won't be merged
|
||||
const MYSIGNAL: GlobalSignal<i32> = GlobalSignal::new(|| 42);
|
||||
const MYSIGNAL2: GlobalSignal<i32> = GlobalSignal::new(|| 42);
|
||||
const MYSIGNAL3: GlobalSignal<i32> = GlobalSignal::with_key(|| 42, "custom-keyed");
|
||||
|
||||
let a = MYSIGNAL.key();
|
||||
let b = MYSIGNAL.key();
|
||||
let c = MYSIGNAL.key();
|
||||
assert_eq!(a, b);
|
||||
assert_eq!(b, c);
|
||||
|
||||
let d = MYSIGNAL2.key();
|
||||
assert_ne!(a, d);
|
||||
|
||||
let e = MYSIGNAL3.key();
|
||||
assert_ne!(a, e);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::write::Writable;
|
||||
use crate::{read::Readable, ReadableRef};
|
||||
use crate::{write::Writable, GlobalKey};
|
||||
use crate::{WritableRef, Write};
|
||||
use dioxus_core::prelude::ScopeId;
|
||||
use dioxus_core::{prelude::ScopeId, Runtime};
|
||||
use generational_box::UnsyncStorage;
|
||||
use std::ops::Deref;
|
||||
|
||||
@@ -12,18 +12,40 @@ use crate::Signal;
|
||||
/// A signal that can be accessed from anywhere in the application and created in a static
|
||||
pub struct GlobalSignal<T> {
|
||||
initializer: fn() -> T,
|
||||
key: GlobalKey<'static>,
|
||||
}
|
||||
|
||||
impl<T: 'static> GlobalSignal<T> {
|
||||
/// Create a new global signal with the given initializer.
|
||||
#[track_caller]
|
||||
pub const fn new(initializer: fn() -> T) -> GlobalSignal<T> {
|
||||
GlobalSignal { initializer }
|
||||
let key = std::panic::Location::caller();
|
||||
GlobalSignal {
|
||||
initializer,
|
||||
key: GlobalKey::new(key),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the signal that backs this global.
|
||||
/// Get the key for this global
|
||||
pub fn key(&self) -> GlobalKey<'static> {
|
||||
self.key.clone()
|
||||
}
|
||||
|
||||
/// Create this global signal with a specific key.
|
||||
/// This is useful for ensuring that the signal is unique across the application and accessible from
|
||||
/// outside the application too.
|
||||
pub const fn with_key(initializer: fn() -> T, key: &'static str) -> GlobalSignal<T> {
|
||||
GlobalSignal {
|
||||
initializer,
|
||||
key: GlobalKey::new_from_str(key),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the signal that backs this .
|
||||
pub fn signal(&self) -> Signal<T> {
|
||||
let key = self as *const _ as *const ();
|
||||
let key = self.key();
|
||||
let context = get_global_context();
|
||||
|
||||
let read = context.signal.borrow();
|
||||
|
||||
match read.get(&key) {
|
||||
@@ -44,6 +66,15 @@ impl<T: 'static> GlobalSignal<T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn maybe_with_rt<O>(&self, f: impl FnOnce(&T) -> O) -> O {
|
||||
if Runtime::current().is_none() {
|
||||
f(&(self.initializer)())
|
||||
} else {
|
||||
self.with(f)
|
||||
}
|
||||
}
|
||||
|
||||
/// Write this value
|
||||
pub fn write(&self) -> Write<'static, T, UnsyncStorage> {
|
||||
self.signal().try_write_unchecked().unwrap()
|
||||
|
||||
@@ -85,6 +85,7 @@ impl<T: 'static> Signal<T> {
|
||||
/// Global signals are generally not recommended for use in libraries because it makes it more difficult to allow multiple instances of components you define in your library.
|
||||
///
|
||||
/// </div>
|
||||
#[track_caller]
|
||||
pub const fn global(constructor: fn() -> T) -> GlobalSignal<T> {
|
||||
GlobalSignal::new(constructor)
|
||||
}
|
||||
@@ -116,6 +117,7 @@ impl<T: PartialEq + 'static> Signal<T> {
|
||||
/// Global memos are generally not recommended for use in libraries because it makes it more difficult to allow multiple instances of components you define in your library.
|
||||
///
|
||||
/// </div>
|
||||
#[track_caller]
|
||||
pub const fn global_memo(constructor: fn() -> T) -> GlobalMemo<T> {
|
||||
GlobalMemo::new(constructor)
|
||||
}
|
||||
@@ -209,6 +211,7 @@ impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
|
||||
let inner = self.inner.read();
|
||||
|
||||
// We cannot hold the subscribers lock while calling mark_dirty, because mark_dirty can run user code which may cause a new subscriber to be added. If we hold the lock, we will deadlock.
|
||||
#[allow(clippy::mutable_key_type)]
|
||||
let mut subscribers = std::mem::take(&mut *inner.subscribers.lock().unwrap());
|
||||
subscribers.retain(|reactive_context| reactive_context.mark_dirty());
|
||||
// Extend the subscribers list instead of overwriting it in case a subscriber is added while reactive contexts are marked dirty
|
||||
|
||||
Reference in new issue
Block a user