From 58bcc4404e0083276576299217fe6e3187d0ef14 Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Wed, 11 Jan 2023 07:45:36 -0800 Subject: [PATCH] feat: provide `&Dependency` to `Lazy` widget `View` --- examples/lazy/src/main.rs | 2 +- lazy/src/lazy.rs | 12 ++++++++---- lazy/src/lib.rs | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/examples/lazy/src/main.rs b/examples/lazy/src/main.rs index 818c91bc11..6512106f23 100644 --- a/examples/lazy/src/main.rs +++ b/examples/lazy/src/main.rs @@ -167,7 +167,7 @@ impl Sandbox for App { } fn view(&self) -> Element { - let options = lazy(self.version, || { + let options = lazy(self.version, |_| { let mut items: Vec<_> = self.items.iter().cloned().collect(); items.sort_by(|a, b| match self.order { diff --git a/lazy/src/lazy.rs b/lazy/src/lazy.rs index 16709f8dff..933def96eb 100644 --- a/lazy/src/lazy.rs +++ b/lazy/src/lazy.rs @@ -16,7 +16,7 @@ use std::rc::Rc; #[allow(missing_debug_implementations)] pub struct Lazy<'a, Message, Renderer, Dependency, View> { dependency: Dependency, - view: Box View + 'a>, + view: Box View + 'a>, element: RefCell< Option>>>>, >, @@ -28,7 +28,10 @@ where Dependency: Hash + 'a, View: Into>, { - pub fn new(dependency: Dependency, view: impl Fn() -> View + 'a) -> Self { + pub fn new( + dependency: Dependency, + view: impl Fn(&Dependency) -> View + 'a, + ) -> Self { Self { dependency, view: Box::new(view), @@ -88,7 +91,8 @@ where self.dependency.hash(&mut hasher); let hash = hasher.finish(); - let element = Rc::new(RefCell::new(Some((self.view)().into()))); + let element = + Rc::new(RefCell::new(Some((self.view)(&self.dependency).into()))); (*self.element.borrow_mut()) = Some(element.clone()); @@ -109,7 +113,7 @@ where if current.hash != new_hash { current.hash = new_hash; - let element = (self.view)().into(); + let element = (self.view)(&self.dependency).into(); current.element = Rc::new(RefCell::new(Some(element))); (*self.element.borrow_mut()) = Some(current.element.clone()); diff --git a/lazy/src/lib.rs b/lazy/src/lib.rs index f49fe4b6e6..41a2877361 100644 --- a/lazy/src/lib.rs +++ b/lazy/src/lib.rs @@ -33,7 +33,7 @@ use std::hash::Hash; pub fn lazy<'a, Message, Renderer, Dependency, View>( dependency: Dependency, - view: impl Fn() -> View + 'a, + view: impl Fn(&Dependency) -> View + 'a, ) -> Lazy<'a, Message, Renderer, Dependency, View> where Dependency: Hash + 'a,