use gpui::{App, Context};
use super::signal::Signal;
pub fn use_state<T: 'static>(cx: &mut App, initial: T) -> Signal<T> {
Signal::new(cx, initial)
}
pub fn watch<V: 'static, T: 'static>(cx: &mut Context<V>, signal: &Signal<T>) {
cx.observe(signal.entity(), |_view, _observed, cx| cx.notify())
.detach();
}
pub fn use_memo<T: 'static, U: 'static>(
cx: &mut App,
source: &Signal<T>,
f: impl Fn(&T) -> U + 'static,
) -> Signal<U> {
let derived = Signal::new(cx, f(source.read(cx)));
let out = derived.clone();
cx.observe(source.entity(), move |observed, cx| {
let next = f(observed.read(cx));
out.set(cx, next);
})
.detach();
derived
}
pub fn use_effect<T: Clone + 'static>(
cx: &mut App,
source: &Signal<T>,
f: impl Fn(&T, &mut App) + 'static,
) {
cx.observe(source.entity(), move |observed, cx| {
let value = observed.read(cx).clone();
f(&value, cx);
})
.detach();
}