use crate::reactive::{Derived, DirtyFlag, ReactiveGraph, Signal, State};
pub trait BlincContext {
fn use_state_keyed<T, F>(&self, key: &str, init: F) -> State<T>
where
T: Clone + Send + 'static,
F: FnOnce() -> T;
fn use_signal_keyed<T, F>(&self, key: &str, init: F) -> Signal<T>
where
T: Clone + Send + 'static,
F: FnOnce() -> T;
fn use_signal<T: Send + 'static>(&self, initial: T) -> Signal<T>;
fn get<T: Clone + 'static>(&self, signal: Signal<T>) -> Option<T>;
fn set<T: Send + 'static>(&self, signal: Signal<T>, value: T);
fn update<T: Clone + Send + 'static, F: FnOnce(T) -> T>(&self, signal: Signal<T>, f: F);
fn use_derived<T, F>(&self, compute: F) -> Derived<T>
where
T: Clone + Send + 'static,
F: Fn(&ReactiveGraph) -> T + Send + 'static;
fn get_derived<T: Clone + 'static>(&self, derived: Derived<T>) -> Option<T>;
fn batch<F, R>(&self, f: F) -> R
where
F: FnOnce(&mut ReactiveGraph) -> R;
fn dirty_flag(&self) -> DirtyFlag;
fn request_rebuild(&self);
fn width(&self) -> f32;
fn height(&self) -> f32;
fn scale_factor(&self) -> f64;
}
pub trait BlincContextExt: BlincContext {
#[track_caller]
fn use_state<T, F>(&self, init: F) -> State<T>
where
T: Clone + Send + 'static,
F: FnOnce() -> T,
{
let location = std::panic::Location::caller();
let key = format!(
"{}:{}:{}",
location.file(),
location.line(),
location.column()
);
self.use_state_keyed(&key, init)
}
#[track_caller]
fn use_signal_auto<T, F>(&self, init: F) -> Signal<T>
where
T: Clone + Send + 'static,
F: FnOnce() -> T,
{
let location = std::panic::Location::caller();
let key = format!(
"{}:{}:{}",
location.file(),
location.line(),
location.column()
);
self.use_signal_keyed(&key, init)
}
}
impl<T: BlincContext + ?Sized> BlincContextExt for T {}
#[cfg(test)]
mod tests {
}