use zwire_core::{Atom, Computed, Scope};
pub trait Lens<S: 'static, A: 'static>: 'static {
fn view<'a>(&self, source: &'a S) -> &'a A;
fn view_mut<'a>(&self, source: &'a mut S) -> &'a mut A;
fn with<R>(&self, scope: &Scope, source: Atom<S>, read: impl FnOnce(&A) -> R) -> R {
scope.with(source, |s| read(self.view(s)))
}
fn update(&self, scope: &Scope, source: Atom<S>, write: impl FnOnce(&mut A))
where
S: Clone,
{
scope.mutate(source, |s| write(self.view_mut(s)));
}
fn signal(self, scope: &Scope, source: Atom<S>) -> Computed<A>
where
A: Clone + PartialEq,
Self: Sized,
{
scope.computed(move |cx| cx.with(source, |s| self.view(s).clone()))
}
}