pub mod arc_signal;
pub mod callback;
pub mod component;
pub mod computed;
pub mod context;
pub mod effect;
pub mod owner;
pub mod prop;
pub mod resource;
pub mod runtime;
pub mod scheduler;
pub mod signal;
pub mod stored;
#[cfg(test)]
mod tests;
#[cfg(test)]
mod tests_loop_wedge;
#[cfg(test)]
mod tests_resource;
pub use arc_signal::{ArcReadSignal, ArcRwSignal, ArcWriteSignal, arc_signal};
pub use callback::Callback;
#[doc(hidden)]
pub use component::__reset_pending_mount_for_tests;
pub use component::{
MountId, RemountStats, flush_mounts, mount_component, mount_component_remountable,
on_component_root_attached, on_mount, owners_for_fn, remount_changed_components,
remount_components_for, unmount_component,
};
pub use computed::computed;
pub use context::{provide_context, use_context, with_context};
pub use effect::effect;
pub use owner::on_cleanup;
pub use prop::Signal;
pub use resource::{Resource, ResourceState, resource, resource_sync};
pub use runtime::{NodeId, Owner};
pub use scheduler::{flush, has_pending_work};
pub use signal::{ReadSignal, RwSignal, WriteSignal, signal};
pub use stored::StoredValue;
use std::cell::RefCell;
use runtime::ReactiveRuntime;
thread_local! {
static RUNTIME: RefCell<ReactiveRuntime> = RefCell::new(ReactiveRuntime::new());
}
pub(crate) fn with_runtime<R>(f: impl FnOnce(&mut ReactiveRuntime) -> R) -> R {
RUNTIME.with_borrow_mut(f)
}
pub(crate) fn try_with_runtime<R>(f: impl FnOnce(&mut ReactiveRuntime) -> R) -> Option<R> {
RUNTIME
.try_with(|runtime| runtime.try_borrow_mut().ok().map(|mut rt| f(&mut rt)))
.ok()
.flatten()
}
pub(crate) fn swap_runtime(runtime: &mut ReactiveRuntime) {
RUNTIME.with_borrow_mut(|active| std::mem::swap(active, runtime));
}
#[cfg(debug_assertions)]
pub(crate) fn warn_no_owner(context: &'static str) {
eprintln!(
"whisker-reactive: {context} called outside any owner scope; \
allocating in a detached owner. The node will leak until \
`__reset_for_tests` or manual disposal."
);
}
#[cfg(not(debug_assertions))]
pub(crate) fn warn_no_owner(_context: &'static str) {}
pub(crate) fn untrack<R>(f: impl FnOnce() -> R) -> R {
use runtime::NodeId;
struct Restore(Option<NodeId>);
impl Drop for Restore {
fn drop(&mut self) {
with_runtime(|rt| rt.current_tracker = self.0);
}
}
let prev = with_runtime(|rt| rt.current_tracker.take());
let _guard = Restore(prev);
f()
}
pub(crate) fn current_tracker() -> Option<runtime::NodeId> {
with_runtime(|rt| rt.current_tracker)
}
pub(crate) fn with_observer<R>(node: runtime::NodeId, f: impl FnOnce() -> R) -> R {
use runtime::NodeId;
struct Restore(Option<NodeId>);
impl Drop for Restore {
fn drop(&mut self) {
with_runtime(|rt| rt.current_tracker = self.0);
}
}
let prev = with_runtime(|rt| rt.current_tracker.replace(node));
let _guard = Restore(prev);
f()
}
#[doc(hidden)]
pub fn __reset_for_tests() {
RUNTIME.with(|r| *r.borrow_mut() = ReactiveRuntime::new());
}